1 /* 2 The MIT License 3 4 Copyright (c) 2008 Tahseen Ur Rehman 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 */ 24 25 package ds.tree; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Represents a node of a Radix tree {@link RadixTreeImpl} 32 * 33 * @author Tahseen Ur Rehman 34 * @email tahseen.ur.rehman {at.spam.me.not} gmail.com 35 * @param <T> 36 */ 37 class RadixTreeNode<T> { 38 private String key; 39 40 private List<RadixTreeNode<T>> childern; 41 42 private boolean real; 43 44 private T value; 45 46 /** 47 * intailize the fields with default values to avoid null reference checks 48 * all over the places 49 */ RadixTreeNode()50 public RadixTreeNode() { 51 key = ""; 52 childern = new ArrayList<RadixTreeNode<T>>(); 53 real = false; 54 } 55 getValue()56 public T getValue() { 57 return value; 58 } 59 setValue(T data)60 public void setValue(T data) { 61 this.value = data; 62 } 63 getKey()64 public String getKey() { 65 return key; 66 } 67 setKey(String value)68 public void setKey(String value) { 69 this.key = value; 70 } 71 isReal()72 public boolean isReal() { 73 return real; 74 } 75 setReal(boolean datanode)76 public void setReal(boolean datanode) { 77 this.real = datanode; 78 } 79 getChildern()80 public List<RadixTreeNode<T>> getChildern() { 81 return childern; 82 } 83 setChildern(List<RadixTreeNode<T>> childern)84 public void setChildern(List<RadixTreeNode<T>> childern) { 85 this.childern = childern; 86 } 87 getNumberOfMatchingCharacters(String key)88 public int getNumberOfMatchingCharacters(String key) { 89 int numberOfMatchingCharacters = 0; 90 while (numberOfMatchingCharacters < key.length() && numberOfMatchingCharacters < this.getKey().length()) { 91 if (key.charAt(numberOfMatchingCharacters) != this.getKey().charAt(numberOfMatchingCharacters)) { 92 break; 93 } 94 numberOfMatchingCharacters++; 95 } 96 return numberOfMatchingCharacters; 97 } 98 99 @Override toString()100 public String toString() { 101 return key; 102 } 103 } 104