01: /*
02: * $Id: TimeMap.java 455983 2005-02-22 17:48:25Z jonl $ $Revision:
03: * 1.4 $ $Date: 2005-02-22 18:48:25 +0100 (Tue, 22 Feb 2005) $
04: *
05: * ==============================================================================
06: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
07: * use this file except in compliance with the License. You may obtain a copy of
08: * the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15: * License for the specific language governing permissions and limitations under
16: * the License.
17: */
18: package wicket.util.time;
19:
20: import java.util.HashMap;
21: import java.util.Iterator;
22: import java.util.Map;
23:
24: /**
25: * This class maps ITimeFrames to Objects. Since values are stored using
26: * ITimeFrameSource implementing objects, the value returned by the source may
27: * vary over time. For example, one implementation of ITimeFrameSource might
28: * return the start and end time of lunch on any given day.
29: * <p>
30: * To associate an object with a dynamic TimeFrame (via ITimeFrameSource), call
31: * put(ITimeFrameSource, Object). You can later retrieve the first object for a
32: * point in time with get(Time). The method get() is provided for convenience
33: * and is equivalent to get(Time.now()).
34: * <p>
35: * This class is not threadsafe.
36: *
37: * @author Jonathan Locke
38: */
39: public final class TimeMap {
40: /** Map from ITimeFrameSource implementing objects to Object values. */
41: private final Map sources = new HashMap();
42:
43: /**
44: * @return Object for the current time
45: */
46: public Object get() {
47: return get(Time.now());
48: }
49:
50: /**
51: * @param time
52: * The time
53: * @return Gets an Object for the given time value or null if none exists
54: */
55: public Object get(final Time time) {
56: for (final Iterator iterator = sources.keySet().iterator(); iterator
57: .hasNext();) {
58: final TimeFrame current = ((ITimeFrameSource) iterator
59: .next()).getTimeFrame();
60: if (current.contains(time)) {
61: return sources.get(current);
62: }
63: }
64:
65: return null;
66: }
67:
68: /**
69: * Associates an object with a dynamic time frame
70: *
71: * @param source
72: * A source that can produce a timeframe to compare a time value
73: * with
74: * @param o
75: * The object to be returned for the given dynamic timeframe
76: */
77: public void put(final ITimeFrameSource source, final Object o) {
78: final TimeFrame timeframe = source.getTimeFrame();
79:
80: for (final Iterator iterator = sources.keySet().iterator(); iterator
81: .hasNext();) {
82: final TimeFrame current = ((ITimeFrameSource) iterator
83: .next()).getTimeFrame();
84:
85: if (timeframe.overlaps(current)) {
86: throw new IllegalArgumentException("Timeframe "
87: + timeframe + " overlaps timeframe " + current);
88: }
89: }
90:
91: sources.put(source, o);
92: }
93: }
|