org.metafacture.commons.tries.SimpleTrie Maven / Gradle / Ivy
Show all versions of metafacture-commons Show documentation
/*
* Copyright 2013, 2014 Deutsche Nationalbibliothek
*
* 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 org.metafacture.commons.tries;
/**
* A simple Trie, nothing fancy at all.
*
* @param type of value stored
* @author Markus Michael Geipel
*/
public final class SimpleTrie
{
private final Node
root = new Node<>(null);
/**
* Creates an instance of {@link SimpleTrie}.
*/
public SimpleTrie() {
}
/**
* Adds a value for the key.
*
* @param key the name of the key
* @param value the value
*/
public void put(final String key, final P value) {
Node
node = root;
Node
next;
final int length = key.length();
for (int i = 0; i < length - 1; ++i) {
next = node.getNext(key.charAt(i));
if (next == null) {
next = node.addNext(key.charAt(i));
}
node = next;
}
next = node.getNext(key.charAt(length - 1));
if (next == null) {
next = node.addNext(key.charAt(length - 1), value);
}
else {
throw new IllegalStateException("Value '" + value + "' already in trie");
}
}
/**
* Gets the value of a key.
*
* @param key the name of the key
* @return the value
*/
public P get(final String key) {
Node
node = root;
final int length = key.length();
for (int i = 0; i < length; ++i) {
node = node.getNext(key.charAt(i));
if (node == null) {
return null;
}
}
return node.getValue();
}
/**
* Node in the trie.
*
* @param
type of the value associated with this node.
*/
private static final class Node
{
private final P value;
private final CharMap> links = new CharMap>();
Node(final P value) {
this.value = value;
}
public Node addNext(final char key) {
return addNext(key, null);
}
public Node
addNext(final char key, final P currentValue) {
final Node
next = new Node
(currentValue);
links.put(key, next);
return next;
}
public P getValue() {
return value;
}
public Node
getNext(final char key) {
return links.get(key);
}
}
}