KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > ItemComparator


1 /*
2  * ItemComparator.java
3  *
4  * Version: $Revision: 1.7 $
5  *
6  * Date: $Date: 2005/04/20 14:22:34 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.content;
41
42 import java.util.Collections JavaDoc;
43 import java.util.Comparator JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Set JavaDoc;
47
48 import org.dspace.browse.Browse;
49
50 /**
51  * Compare two Items by their DCValues.
52  *
53  * The DCValues to be compared are specified by the element, qualifier and
54  * language parameters to the constructor. If the Item has more than one
55  * matching DCValue, then the max parameter to the constructor specifies whether
56  * the maximum or minimum lexicographic value will be used.
57  *
58  * @author Peter Breton
59  * @version $Revision: 1.7 $
60  */

61 public class ItemComparator implements Comparator JavaDoc
62 {
63     /** Dublin Core element */
64     private String JavaDoc element;
65
66     /** Dublin Core qualifier */
67     private String JavaDoc qualifier;
68
69     /** Language */
70     private String JavaDoc language;
71
72     /** Whether maximum or minimum value will be used */
73     private boolean max;
74
75     /**
76      * Constructor.
77      *
78      * @param element
79      * The Dublin Core element
80      * @param qualifier
81      * The Dublin Core qualifier
82      * @param language
83      * The language for the DCValues
84      * @param max
85      * If true, and there is more than one DCValue for element,
86      * qualifier and language, then use the maximum value
87      * lexicographically; otherwise use the minimum value.
88      */

89     public ItemComparator(String JavaDoc element, String JavaDoc qualifier, String JavaDoc language,
90             boolean max)
91     {
92         this.element = element;
93         this.qualifier = qualifier;
94         this.language = language;
95         this.max = max;
96     }
97
98     /**
99      * Compare two Items by checking their DCValues for element, qualifier, and
100      * language.
101      *
102      * <p>
103      * Return >= 1 if the first is lexicographically greater than the second; <=
104      * -1 if the second is lexicographically greater than the first, and 0
105      * otherwise.
106      * </p>
107      *
108      * @param first
109      * The first object to compare. Must be an object of type
110      * org.dspace.content.Item.
111      * @param second
112      * The second object to compare. Must be an object of type
113      * org.dspace.content.Item.
114      * @return >= 1 if the first is lexicographically greater than the second; <=
115      * -1 if the second is lexicographically greater than the first, and
116      * 0 otherwise.
117      */

118     public int compare(Object JavaDoc first, Object JavaDoc second)
119     {
120         if ((!(first instanceof Item)) || (!(second instanceof Item)))
121         {
122             throw new IllegalArgumentException JavaDoc("Arguments must be Items");
123         }
124
125         // Retrieve a chosen value from the array for comparison
126
String JavaDoc firstValue = getValue((Item) first);
127         String JavaDoc secondValue = getValue((Item) second);
128
129         if ((firstValue == null) && (secondValue == null))
130         {
131             return 0;
132         }
133
134         if ((firstValue != null) && (secondValue == null))
135         {
136             return 1;
137         }
138
139         if ((firstValue == null) && (secondValue != null))
140         {
141             return -1;
142         }
143
144         // See the javadoc for java.lang.String for an explanation
145
// of the return value.
146
return firstValue.compareTo(secondValue);
147     }
148
149     /**
150      * Return true if the object is equal to this one, false otherwise. Another
151      * object is equal to this one if it is also an ItemComparator, and has the
152      * same values for element, qualifier, language, and max.
153      *
154      * @param obj
155      * The object to compare to.
156      * @return True if the other object is equal to this one, false otherwise.
157      */

158     public boolean equals(Object JavaDoc obj)
159     {
160         if (!(obj instanceof ItemComparator))
161         {
162             return false;
163         }
164
165         ItemComparator other = (ItemComparator) obj;
166
167         return _equals(element, other.element)
168                 && _equals(qualifier, other.qualifier)
169                 && _equals(language, other.language) && (max == other.max);
170     }
171
172     /**
173      * Return true if the first string is equal to the second. Either or both
174      * may be null.
175      */

176     private boolean _equals(String JavaDoc first, String JavaDoc second)
177     {
178         if ((first == null) && (second == null))
179         {
180             return true;
181         }
182
183         if ((first != null) && (second == null))
184         {
185             return false;
186         }
187
188         if ((first == null) && (second != null))
189         {
190             return false;
191         }
192
193         return first.equals(second);
194     }
195
196     /**
197      * Choose the canonical value from an item for comparison. If there are no
198      * values, null is returned. If there is exactly one value, then it is
199      * returned. Otherwise, either the maximum or minimum lexicographical value
200      * is returned; the parameter to the constructor says which.
201      *
202      * @param item
203      * The item to check
204      * @return The chosen value, or null
205      */

206     private String JavaDoc getValue(Item item)
207     {
208         // The overall array and each element are guaranteed non-null
209
DCValue[] dcvalues = item.getDC(element, qualifier, language);
210
211         if (dcvalues.length == 0)
212         {
213             return null;
214         }
215
216         if (dcvalues.length == 1)
217         {
218             return normalizeTitle(dcvalues[0]);
219         }
220
221         // We want to sort using Strings, but also keep track of
222
// which DCValue the value came from.
223
Map JavaDoc values = new HashMap JavaDoc();
224
225         for (int i = 0; i < dcvalues.length; i++)
226         {
227             String JavaDoc value = dcvalues[i].value;
228
229             if (value != null)
230             {
231                 values.put(value, new Integer JavaDoc(i));
232             }
233         }
234
235         if (values.size() == 0)
236         {
237             return null;
238         }
239
240         Set JavaDoc valueSet = values.keySet();
241         String JavaDoc chosen = max ? (String JavaDoc) Collections.max(valueSet)
242                 : (String JavaDoc) Collections.min(valueSet);
243
244         int index = ((Integer JavaDoc) values.get(chosen)).intValue();
245
246         return normalizeTitle(dcvalues[index]);
247     }
248
249     /**
250      * Normalize the title of a DCValue.
251      */

252     private String JavaDoc normalizeTitle(DCValue value)
253     {
254         if (!"title".equals(element))
255         {
256             return value.value;
257         }
258
259         return Browse.getNormalizedTitle(value.value, value.language);
260     }
261 }
262
Popular Tags