KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > policy > AddedComparator


1 /*
2 Copyright 2004 Philip Jacob <phil@whirlycott.com>
3                     Seth Fitzsimmons <seth@note.amherst.edu>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */

17
18 package com.whirlycott.cache.policy;
19
20 import java.util.Comparator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import com.whirlycott.cache.Item;
27 import com.whirlycott.cache.Messages;
28
29 /**
30  * A comparison function, used by FIFOMaintenancePolicy, which determines
31  * whether one Item was added before a second Item.
32  *
33  * @author Phil Jacob
34  */

35 public class AddedComparator implements Comparator JavaDoc {
36     
37     private static final Log log = LogFactory.getLog(AddedComparator.class);
38     
39     /**
40      * Compares two Item objects based on their relative times added to the
41      * cache.
42      */

43     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
44         int retval = 0;
45         
46         if (o1 instanceof Map.Entry JavaDoc && o2 instanceof Map.Entry JavaDoc) {
47             
48             final Item lh = (Item) ((Map.Entry JavaDoc)o1).getValue();
49             final Item rh = (Item) ((Map.Entry JavaDoc)o2).getValue();
50             
51             if (lh != null && rh != null) {
52                 
53                 if (lh.getAdded() < rh.getAdded())
54                     retval = -1;
55                 
56                 if (lh.getAdded() > rh.getAdded())
57                     retval = 1;
58                 
59             }
60             
61         } else {
62             log.warn(Messages.getString("AddedComparator.values_were_not_map_entry")); //$NON-NLS-1$
63
}
64         return retval;
65     }
66 }
67
Popular Tags