com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of concurrent-trees Show documentation
Show all versions of concurrent-trees Show documentation
Concurrent Radix Trees and Concurrent Suffix Trees for Java.
/**
* Copyright 2012-2013 Niall Gallagher
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.concurrenttrees.radix.node.concrete;
import com.googlecode.concurrenttrees.radix.node.Node;
import com.googlecode.concurrenttrees.radix.node.NodeFactory;
import com.googlecode.concurrenttrees.radix.node.concrete.chararray.*;
import com.googlecode.concurrenttrees.radix.node.concrete.voidvalue.VoidValue;
import com.googlecode.concurrenttrees.radix.node.util.NodeUtil;
import com.googlecode.concurrenttrees.common.CharSequences;
import java.util.List;
/**
* A {@link NodeFactory} which creates various implementations of {@link Node} objects all of which store incoming
* edge characters as a character array inside the node.
*
* Returns an optimal node implementation depending on arguments supplied, which will be one of:
*
* - {@link CharArrayNodeDefault} - contains all possible fields
* - {@link CharArrayNodeNonLeafNullValue} - does not store a value, returns {@code null} for value
* - {@link CharArrayNodeNonLeafVoidValue} - does not store a value, returns {@link VoidValue} for value
* - {@link CharArrayNodeLeafVoidValue} - does not store child edges or a value, returns {@link VoidValue} for value
* - {@link CharArrayNodeLeafWithValue} - does not store child edges, but does store a value
*
*
* When the application supplies {@link VoidValue} for a value, this factory will omit actually storing that value
* in the tree and will return one of the VoidValue-optimized nodes above which can reduce memory usage.
*
* @author Niall Gallagher
*/
public class DefaultCharArrayNodeFactory implements NodeFactory {
@Override
public Node createNode(CharSequence edgeCharacters, Object value, List childNodes, boolean isRoot) {
if (edgeCharacters == null) {
throw new IllegalStateException("The edgeCharacters argument was null");
}
if (!isRoot && edgeCharacters.length() == 0) {
throw new IllegalStateException("Invalid edge characters for non-root node: " + CharSequences.toString(edgeCharacters));
}
if (childNodes == null) {
throw new IllegalStateException("The childNodes argument was null");
}
NodeUtil.ensureNoDuplicateEdges(childNodes);
if (childNodes.isEmpty()) {
// Leaf node...
if (value instanceof VoidValue) {
return new CharArrayNodeLeafVoidValue(edgeCharacters);
}
else if (value != null) {
return new CharArrayNodeLeafWithValue(edgeCharacters, value);
}
else {
return new CharArrayNodeLeafNullValue(edgeCharacters);
}
}
else {
// Non-leaf node...
if (value instanceof VoidValue) {
return new CharArrayNodeNonLeafVoidValue(edgeCharacters, childNodes);
}
else if (value == null) {
return new CharArrayNodeNonLeafNullValue(edgeCharacters, childNodes);
}
else {
return new CharArrayNodeDefault(edgeCharacters, value, childNodes);
}
}
}
}