KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > DjList


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Comparator JavaDoc;
36
37 import com.genimen.djeneric.language.Messages;
38 import com.genimen.djeneric.repository.exceptions.CanNotDeleteException;
39 import com.genimen.djeneric.repository.exceptions.DjenericException;
40 import com.genimen.djeneric.util.DjLogger;
41
42 public class DjList extends ArrayList JavaDoc
43 {
44   private static final long serialVersionUID = 4048790148093654840L;
45
46   DjExtent _storedType = null;
47
48   public DjList(DjExtent storedType)
49   {
50     super();
51     _storedType = storedType;
52   }
53
54   public DjList()
55   {
56     super();
57   }
58
59   public DjList(Collection JavaDoc c)
60   {
61     super(c);
62   }
63
64   public DjObject getDjenericObjectAt(int idx)
65   {
66     return (DjObject) get(idx);
67   }
68
69   public synchronized Object JavaDoc[] toArray()
70   {
71     DjObject[] result = new DjObject[size()];
72     super.toArray(result);
73     return result;
74   }
75
76   // Returns the value that is the closest to the supplied value
77
// If no value is found, null is returned
78
public DjObject looselyMatchValue(String JavaDoc value)
79   {
80     // first match strict..
81
for (int i = 0; i < size(); i++)
82     {
83       if (getDjenericObjectAt(i).toString().equals(value)) return getDjenericObjectAt(i);
84     }
85     // match first part
86
for (int i = 0; i < size(); i++)
87     {
88       if (getDjenericObjectAt(i).toString().startsWith(value)) return getDjenericObjectAt(i);
89     }
90     // match ignore case
91
for (int i = 0; i < size(); i++)
92     {
93       if (getDjenericObjectAt(i).toString().equalsIgnoreCase(value)) return getDjenericObjectAt(i);
94     }
95     // match first part ignore case
96
for (int i = 0; i < size(); i++)
97     {
98       if (getDjenericObjectAt(i).toString().toLowerCase().startsWith(value.toLowerCase())) return getDjenericObjectAt(i);
99     }
100     return null;
101   }
102
103   public void sort()
104   {
105     if (getStoredType() != null) Collections.sort(this, new DjenericObjectComparator(getStoredType()
106         .getPropertySortIndices()));
107     else Collections.sort(this, new DjenericObjectComparator());
108   }
109
110   public void sort(int[] propertyIdxList)
111   {
112     if (propertyIdxList == null || propertyIdxList.length == 0) sort();
113     else Collections.sort(this, new DjenericObjectComparator(propertyIdxList));
114   }
115
116   public void sort(DjExtent extent)
117   {
118     int[] sorts = extent.getPropertySortIndices();
119     if (sorts == null || sorts.length == 0) return;
120
121     sort(sorts);
122   }
123
124   class DjenericObjectComparator implements Comparator JavaDoc
125   {
126     int[] _propertyIdxList = null;
127
128     public DjenericObjectComparator()
129     {
130     }
131
132     public DjenericObjectComparator(int[] propertyIdxList)
133     {
134       _propertyIdxList = propertyIdxList;
135     }
136
137     public int compare(Object JavaDoc o1, Object JavaDoc o2)
138     {
139       if (!(o1 instanceof DjObject && o1 instanceof DjObject)) return -1;
140       DjObject dv1 = (DjObject) o1;
141       DjObject dv2 = (DjObject) o2;
142
143       if (_propertyIdxList == null || _propertyIdxList.length == 0)
144       {
145         String JavaDoc str1 = dv1.getDescriptor();
146         if (str1 == null) str1 = "";
147         String JavaDoc str2 = dv2.getDescriptor();
148         if (str2 == null) str2 = "";
149
150         return str1.toLowerCase().compareTo(str2.toLowerCase());
151       }
152
153       try
154       {
155         // Complex compare, based on (possibly) multiple properties:
156
for (int i = 0; i < _propertyIdxList.length; i++)
157         {
158           int theIndex = Math.abs(_propertyIdxList[i]);
159           Object JavaDoc val1 = dv1.get(theIndex);
160           Object JavaDoc val2 = dv2.get(theIndex);
161
162           if (val1 instanceof Integer JavaDoc)
163           {
164             int c = ((Integer JavaDoc) val1).compareTo((Integer JavaDoc) val2);
165             if (_propertyIdxList[i] < 0) c = -c;
166             if (c != 0) return c;
167           }
168           else if (val1 instanceof Long JavaDoc)
169           {
170             int c = ((Long JavaDoc) val1).compareTo((Long JavaDoc) val2);
171             if (_propertyIdxList[i] < 0) c = -c;
172             if (c != 0) return c;
173           }
174           else
175           {
176             if (val1 == null && val2 != null) return -1;
177             if (val1 != null && val2 == null) return 1;
178             if (val1 == null || val2 == null) return 0;
179
180             int c = val1.toString().toLowerCase().compareTo(val2.toString().toLowerCase());
181             // Descending order sort?
182
if (_propertyIdxList[i] < 0) c = -c;
183             if (c != 0) return c;
184           }
185         }
186         // If we get here the objects are identical; return 0 then
187
return 0;
188       }
189       catch (DjenericException dex)
190       {
191         DjLogger.log(dex);
192         return 0;
193       }
194     }
195   }
196
197   public DjExtent getStoredType()
198   {
199     return _storedType;
200   }
201
202   public void setStoredTypeName(DjExtent storedType)
203   {
204     _storedType = storedType;
205   }
206
207   public void checkType(DjExtent someType) throws DjenericException
208   {
209     if (_storedType != null && someType != null)
210     {
211       if (!someType.isInstanceof(_storedType)) throw new DjenericException(Messages
212           .getString("DjList.wrongType", someType.getTypeName(), _storedType.getTypeName()));
213     }
214   }
215
216   public void markAllDeleted() throws CanNotDeleteException, DjenericException
217   {
218     for (int i = 0; i < size(); i++)
219       getDjenericObjectAt(i).markForDelete();
220     clear();
221   }
222
223   public void markAllDeletedExcluding(ArrayList JavaDoc lst) throws CanNotDeleteException, DjenericException
224   {
225     for (int i = 0; i < size(); i++)
226       if (!lst.contains(getDjenericObjectAt(i))) getDjenericObjectAt(i).markForDelete();
227   }
228
229   public void purgeDeleted()
230   {
231     int i = 0;
232     while (i < size())
233     {
234       if (getDjenericObjectAt(i).isMarkedForDelete()) remove(i);
235       else i++;
236     }
237   }
238
239 }
Popular Tags