View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Util: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2006 Open World Ltd, Kevin Rogers
4   //
5   // This file is part of MillScript-Util.
6   //
7   // MillScript-Util is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Util is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Util; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.commons.util.map;
22  
23  import org.millscript.commons.util.EMap;
24  import org.millscript.commons.util.ETreeMap;
25  import org.millscript.commons.util.ETreeMapNode;
26  import org.millscript.commons.util.IMap;
27  import org.millscript.commons.util.IMapDefault;
28  import org.millscript.commons.util.MapIterator;
29  import org.millscript.commons.util.Maplet;
30  
31  /**
32   * 
33   */
34  public abstract class AbstractETreeMap< K, V, N extends ETreeMapNode< K, V > > extends AbstractUTreeMap< K, V, N > implements EMap< K, V >, ETreeMap< K, V, N > {
35  
36      /**
37       * 
38       */
39      public AbstractETreeMap() {
40          super();
41          // TODO Auto-generated constructor stub
42      }
43  
44      /**
45       * @param def
46       */
47      public AbstractETreeMap( IMapDefault< K, V > def ) {
48          super( def );
49          // TODO Auto-generated constructor stub
50      }
51  
52      /**
53       * @see org.millscript.commons.util.EMap#insert(org.millscript.commons.util.Maplet)
54       */
55      public void insert( final Maplet< ? extends K, ? extends V > entry ) {
56          this.insert( entry.getKey(), entry.getValue() );
57      }
58  
59      /**
60       * @see org.millscript.commons.util.EMap#insertAll(org.millscript.commons.util.IMap)
61       */
62      public void insertAll( final IMap< ? extends K, ? extends V > map ) {
63          // Iterate over all the mappings in the supplied map, inserting them
64          // into this one. NOTE - we want the iterator to share backing store
65          // here
66          final MapIterator< ? extends K, ? extends V > it = map.iterator( true );
67          while ( it.hasNext() ) {
68              this.insert( it.nextKey(), it.currentValue() );
69          }
70      }
71  
72      /**
73       * @see org.millscript.commons.util.EMap#remove(K, V)
74       */
75      public void remove( final K key, final V value ) {
76          final ETreeMapNode< K, V > node = this.find( key );
77          if ( node != null ) {
78              // This node matches, check the value to see if it matches the
79              // supplied one.
80              if ( value == null ? node.getValue() == null : value.equals( node.getValue() ) ) {
81                  // Ok, the node matches completely, delete!
82                  node.delete( (ETreeMap< K, V, ETreeMapNode< K, V >>) this );
83              }
84          }
85      }
86  
87      /**
88       * @see org.millscript.commons.util.EMap#remove(org.millscript.commons.util.Maplet)
89       */
90      public void remove( final Maplet< ? extends K, ? extends V > entry ) {
91          this.remove( entry.getKey(), entry.getValue() );
92      }
93  
94      /**
95       * @see org.millscript.commons.util.EMap#removeAll()
96       */
97      public void removeAll() {
98          this.setRootNode( null );
99      }
100 
101     /**
102      * @see org.millscript.commons.util.EMap#removeAll(org.millscript.commons.util.IMap)
103      */
104     public void removeAll( final IMap< ? extends K, ? extends V > map ) {
105         // Iterate over all the mappings in the supplied map, removing them
106         // from this one. NOTE - we want the iterator to share backing store
107         // here
108         final MapIterator< ? extends K, ? extends V > it = map.iterator( true );
109         while ( it.hasNext() ) {
110             this.remove( it.nextKey(), it.currentValue() );
111         }
112     }
113 
114     /**
115      * @see org.millscript.commons.util.EMap#removeKey(K)
116      */
117     public void removeKey( final K key ) {
118         final ETreeMapNode< K, V > node = this.find( key );
119         if ( node != null ) {
120             // This node matches, delete it
121             node.delete( (ETreeMap< K, V, ETreeMapNode< K, V >>) this );
122         }
123     }
124 
125     /**
126      * @see org.millscript.commons.util.EMap#removeValue(V)
127      */
128     public void removeValue( final V value ) {
129         ETreeMapNode< K, V > node = (ETreeMapNode< K, V >) this.getRootNode().getFirstInSequence();
130         while ( node != null ) {
131             if ( value == null ? node.getValue() == null : value.equals( node.getValue() ) ) {
132                 final ETreeMapNode< K, V > nextNode = (ETreeMapNode< K, V >) node.getNextInSequence();
133                 node.delete( (ETreeMap< K, V, ETreeMapNode< K, V >>) this );
134                 node = nextNode;
135             } else {
136                 node = (ETreeMapNode< K, V >) node.getNextInSequence();
137             }
138         }
139     }
140 
141 }