View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.datastore;
20  
21  import java.util.Comparator;
22  import java.util.Iterator;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.TreeMap;
26  
27  /***
28  * This implementation of DataIndex wraps a TreeMap and
29  * adds the ability to contain objects with duplicate keys.
30  */
31  public class DefaultDataIndex implements DataIndex
32  {
33  	static final long serialVersionUID = -3759982714240822885L;
34  
35      protected String name;
36      protected String property;
37      private TreeMap treeMap;    
38      private Comparator comparator;
39      
40      public DefaultDataIndex()
41      {
42          comparator = new DefaultComparator();
43          setTreeMap( new TreeMap( new DefaultComparator() ) );
44      }
45      
46      public DefaultDataIndex( String aName, String aProperty )
47      {
48          this();
49      	setName( aName );
50  		setProperty( aProperty );
51      }
52  
53      // included for xml serialization    
54      public Comparator getComparator() { return comparator; }
55      public void setComparator( Comparator aComparator ) 
56      { 
57          comparator = aComparator; 
58          // set comparator and copy contents
59          TreeMap map = getTreeMap();
60          setTreeMap( new TreeMap( comparator ) );
61          getTreeMap().putAll( map );
62          
63      }
64      
65      public String getName() { return name; };
66      public void setName( String aName ) { name = aName; }
67      public String getProperty() { return property; };
68      public void setProperty( String aProperty ) { property = aProperty; }
69      public TreeMap getTreeMap() { return treeMap; }
70      public void setTreeMap( TreeMap aMap ) { treeMap = aMap; }
71      
72      public List query( Object beginValue, Object endValue )
73      {
74  //System.out.println( "DefaultDataIndex.query: " + beginValue + " : " + endValue );
75  		List result = new LinkedList();
76      	if ( endValue == null )
77  	    {
78  	    	if ( beginValue == null )
79  		    {
80  		    	// begin and end are null, return entire set
81  		    	populateListFromIterator( result, treeMap.values().iterator() );
82  			    return result;
83  		    }
84  		    
85  		    // only end is null, return all starting from beginValue
86  		    populateListFromIterator( result,
87  		    	treeMap.tailMap( beginValue ).values().iterator() );
88  			return result;
89  	    }
90  	    else
91  	    if ( beginValue == null )
92  	    {
93  	    	// only begin is null, return all ending with endValue
94              populateListFromIterator( result,
95                  treeMap.headMap( endValue ).values().iterator() );
96  	    }
97  	    else
98  	    {
99      		// begin and end are specified, return all inclusive
100             populateListFromIterator( result,
101                 treeMap.subMap( beginValue, endValue ).values().iterator() );
102 		}
103 		
104 		// append endValue results, so it's inclusive
105     	Object o = treeMap.get( endValue );
106 	    if ( o != null )
107 	    {
108             if ( o instanceof DuplicateList )
109             {
110                 populateListFromIterator( result,
111                     ((DuplicateList)o).iterator() );
112             }
113             else
114             {
115                 result.add( o );
116             }
117 		}
118 		
119 		// return complete result
120 		return result;
121 	}
122     
123     protected void populateListFromIterator( List aList, Iterator it )
124     {
125     	Object o;
126     	while ( it.hasNext() )
127 	    {
128 	    	o = it.next();
129 		    if ( o instanceof DuplicateList )
130 		    {
131 		    	populateListFromIterator( 
132 			    	aList, ((DuplicateList)o).iterator() );
133 		    }
134 		    else
135 		    {
136 		    	aList.add( o );
137 		    }
138 	    }
139     }
140     
141     public Object addObject( Object anObject, Object newValue )
142     {
143     	Object o = treeMap.get( newValue );
144         if ( o != null )
145         {
146             if ( o instanceof DuplicateList )
147             {
148                 ((DuplicateList)o).add( anObject );
149                 return anObject;
150             }
151 
152             DuplicateList list = new DuplicateList();
153             list.add( o );
154             list.add( anObject );
155             anObject = list;
156 
157         }
158 if ( anObject == null ) new RuntimeException().printStackTrace();
159 
160     	treeMap.put( newValue, anObject );
161 	    return anObject;
162     }
163     
164     public Object updateObject( Object anObject, 
165     	Object oldValue, Object newValue )
166     {
167     	removeObject( anObject, oldValue );
168 		return addObject( anObject, newValue );
169     }
170     
171     public Object removeObject( Object anObject, Object oldValue )
172     {
173     	Object o = treeMap.get( oldValue );
174         if ( o != null )
175         {
176             if ( o instanceof DuplicateList )
177             {
178                 // remove this item from the list
179                 DuplicateList list = (DuplicateList) o;
180                 list.remove( anObject );
181 		
182 				// if there are still duplicates, return
183                 if ( list.size() > 1 )
184                     return anObject;
185 
186                 // else, list size must be one
187                 if ( list.size() == 0 )
188                 {
189                     System.out.println( "DefaultDataIndex.deleteObject: " + oldValue
190                         + " : list size is 1 : this should never happen." );
191 					return null;
192                 }
193 
194                 // replace existing list with remaining item from list
195                 treeMap.remove( oldValue );
196 	            treeMap.put( oldValue, list.getFirst() );
197     	        return anObject;
198             }
199 
200 			// otherwise, proceed normally
201             treeMap.remove( oldValue );
202         }
203 		return anObject;
204     }
205     
206     public void clear()
207     {
208     	treeMap.clear();
209     }    
210     
211     public String toString() 
212     {
213        return "DefaultDataIndex: " + name + " : " + property + " : " + treeMap.toString();
214     }
215         
216 }
217 
218 /*
219  * $Log$
220  * Revision 1.2  2006/02/19 16:26:19  cgruber
221  * Move non-unit-test code to tests project
222  * Fix up code to work with proper imports
223  * Fix maven dependencies.
224  *
225  * Revision 1.1  2006/02/16 13:18:56  cgruber
226  * Check in all sources in eclipse-friendly maven-enabled packages.
227  *
228  * Revision 1.2  2003/08/14 19:29:38  chochos
229  * minor cleanup (imports, static method calls, etc)
230  *
231  * Revision 1.1.1.1  2000/12/21 15:47:11  mpowers
232  * Contributing wotonomy.
233  *
234  * Revision 1.3  2000/12/20 16:25:36  michael
235  * Added log to all files.
236  *
237  *
238  */
239