1 /*
2  * JaLingo, http://jalingo.sourceforge.net/
3  *
4  * Copyright (c) 2002-2006 Oleksandr Shyshko
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 package ja.lingo.engine.searchindex;
22 
23 import ja.centre.util.arrays.ArrayUtil;
24 
25 class Node extends ANode implements IMutableNode {
26     protected INode[] children;
27     protected static final INode[] NO_CHILDREN = new INode[0];
28 
Node( char key, int value )29     private Node( char key, int value ) {
30         this.key = key;
31         this.value = value;
32         this.children = NO_CHILDREN;
33     }
34 
addChild( INode child )35     public void addChild( INode child ) {
36         INode[] newChildren = new INode[children.length + 1]; // TODO optimize: produces lots of garbage
37         System.arraycopy( children, 0, newChildren, 0, children.length );
38 
39         newChildren[newChildren.length - 1] = child;
40 
41         children = newChildren;
42     }
removeChild( INode child )43     public void removeChild( INode child ) {
44         int index = ArrayUtil.indexOf( children, child );
45         if ( index != -1 ) {
46             INode[] newChildren = new INode[children.length - 1]; // TODO optimize: produces lots of garbage
47 
48             if ( index != 0 ) {
49                 System.arraycopy( children, 0, newChildren, 0, index );
50             }
51 
52             if ( index != children.length - 1 ) {
53                 System.arraycopy( children, index + 1, newChildren, index, newChildren.length - index );
54             }
55 
56             children = newChildren;
57         }
58     }
59 
childrenCount()60     public int childrenCount() {
61         return children.length;
62     }
63 
getChild( int index )64     public INode getChild( int index ) {
65         return children[index];
66     }
67 
68 
69 
createRoot()70     public static IMutableNode createRoot() {
71         return new Node( '\u0000', 0 );
72     }
73 
create( char key, int value )74     public static IMutableNode create( char key, int value ) {
75         return new Node( key, value );
76     }
77 }
78