KickJava   Java API By Example, From Geeks To Geeks.

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


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 LFUMaintenancePolicy, which determines
31  * whether one Item has been used more than a second Item, using said Items'
32  * count properties.
33  *
34  * @author Seth Fitzsimmons
35  */

36 public class CountComparator implements Comparator JavaDoc {
37     
38     private static final Log log = LogFactory.getLog(CountComparator.class);
39     
40     /**
41      * Compares two Item objects based on their count properties.
42      */

43     public int compare(final Object JavaDoc o1, final 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.getCount() < rh.getCount())
54                     retval = -1;
55                 
56                 if (lh.getCount() > rh.getCount())
57                     retval = 1;
58                 
59             }
60             
61         } else {
62             log.warn(Messages.getString("CountComparator.values_were_not_map_entry")); //$NON-NLS-1$
63
}
64         return retval;
65     }
66 }
67
Popular Tags