01: /*
02: Copyright 2004 Philip Jacob <phil@whirlycott.com>
03: Seth Fitzsimmons <seth@note.amherst.edu>
04:
05: Licensed under the Apache License, Version 2.0 (the "License");
06: you may not use this file except in compliance with the License.
07: You may obtain a copy of the License at
08:
09: 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:
24: import com.whirlycott.cache.Item;
25:
26: /**
27: * A predicate for filtering Collections of Items based on their expiration time.
28: *
29: * @author Seth Fitzsimmons
30: */
31: public class ExpirationTimePredicate implements Predicate {
32:
33: private final long currentTime;
34:
35: /**
36: * Creates an ExpirationTimePredicate.
37: *
38: * @param currentTime Cache's notion of the "current time."
39: */
40: public ExpirationTimePredicate(final long currentTime) {
41: this .currentTime = currentTime;
42: }
43:
44: /**
45: * Only Items with an expiration time that has passed will cause this to return true.
46: */
47: public boolean evaluate(final Object obj) {
48: boolean retval = false;
49:
50: if (obj instanceof Entry) {
51: if (((Entry) obj).getValue() instanceof Item) {
52: final Item item = (Item) ((Entry) obj).getValue();
53: if (item.getExpiresAfter() > 0) {
54: /*
55: * log.debug("Current time: " + currentTime);
56: * log.debug("Expiration time: " + (item.getAdded() +
57: * item.getExpiresAfter()));
58: */
59: retval = ((item.getExpiresAfter() + item.getAdded()) < currentTime);
60: }
61: }
62: }
63:
64: return retval;
65: }
66: }
|