com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory 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.
The newest version!
/*
* 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.common.CharSequences;
import com.googlecode.concurrenttrees.radix.node.Node;
import com.googlecode.concurrenttrees.radix.node.NodeFactory;
import com.googlecode.concurrenttrees.radix.node.NodeList;
import com.googlecode.concurrenttrees.radix.node.concrete.bytearray.*;
import com.googlecode.concurrenttrees.radix.node.concrete.voidvalue.VoidValue;
import com.googlecode.concurrenttrees.radix.node.util.NodeUtil;
/**
* A {@link NodeFactory} which creates {@link Node} objects which store incoming edge characters as a byte array inside
* the node. This is similar to {@link DefaultCharArrayNodeFactory}, except nodes use a single byte to represent each
* character in UTF-8, instead of Java's default 2-byte UFT-16 encoding.
*
* This can reduce the memory overhead of storing character data by 50%, but supports only characters which can be
* represented as a single byte in UTF-8. Throws an exception if characters are encountered which cannot be represented
* as a single byte.
*
* @author Niall Gallagher
*/
public class DefaultByteArrayNodeFactory implements NodeFactory {
private static final long serialVersionUID = 1L;
private final boolean fast;
public DefaultByteArrayNodeFactory() {
fast = false;
}
DefaultByteArrayNodeFactory(boolean fast) {
this.fast = fast;
}
@Override
public Node createNode(CharSequence edgeCharacters, Object value, NodeList childNodes, boolean isRoot) {
assert edgeCharacters != null : "The edgeCharacters argument was null";
assert isRoot || edgeCharacters.length() > 0 : "Invalid edge characters for non-root node: " + CharSequences.toString(edgeCharacters);
assert childNodes != null : "The edgeCharacters argument was null";
assert NodeUtil.hasNoDuplicateEdges(childNodes) : "Duplicate edge detected in list of nodes supplied: " + childNodes;
byte[] incomingEdgeCharArray = ByteArrayCharSequence.toSingleByteUtf8Encoding(edgeCharacters, fast);
if (incomingEdgeCharArray == null) {
return null;
}
if (childNodes.isEmpty()) {
// Leaf node...
if (value instanceof VoidValue) {
return new ByteArrayNodeLeafVoidValue(incomingEdgeCharArray);
}
else if (value != null) {
return new ByteArrayNodeLeafWithValue(incomingEdgeCharArray, value);
}
else {
return new ByteArrayNodeLeafNullValue(incomingEdgeCharArray);
}
}
else {
// Non-leaf node...
if (value instanceof VoidValue) {
return new ByteArrayNodeNonLeafVoidValue(incomingEdgeCharArray, childNodes);
}
else if (value == null) {
return new ByteArrayNodeNonLeafNullValue(incomingEdgeCharArray, childNodes);
}
else {
return new ByteArrayNodeDefault(incomingEdgeCharArray, value, childNodes);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy