KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map.Entry;
21
22 import org.apache.commons.collections.Predicate;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import com.whirlycott.cache.Item;
27
28 /**
29  * A predicate for filtering Collections of Items based on their expiration
30  * time.
31  *
32  * @author Seth Fitzsimmons
33  */

34 public class ExpirationTimePredicate implements Predicate {
35
36     /**
37      * Logger.
38      */

39     private static final Log log = LogFactory.getLog(ExpirationTimePredicate.class);
40
41     private final long currentTime;
42
43     /**
44      * Creates an ExpirationTimePredicate.
45      *
46      * @param currentTime
47      * Cache's notion of the "current time."
48      */

49     public ExpirationTimePredicate(final long currentTime) {
50         this.currentTime = currentTime;
51     }
52
53     /**
54      * Only Items with an expiration time that has passed will cause this to
55      * return true.
56      */

57     public boolean evaluate(final Object JavaDoc obj) {
58         boolean retval = false;
59
60         if (obj instanceof Entry) {
61             if (((Entry) obj).getValue() instanceof Item) {
62                 final Item item = (Item) ((Entry) obj).getValue();
63                 if (item.getExpiresAfter() > 0) {
64                     /*
65                      * log.debug("Current time: " + currentTime);
66                      * log.debug("Expiration time: " + (item.getAdded() +
67                      * item.getExpiresAfter()));
68                      */

69                     retval = ((item.getExpiresAfter() + item.getAdded()) < currentTime);
70                 }
71             }
72         }
73
74         return retval;
75     }
76 }
Popular Tags