001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.time;
018
019import java.io.Serializable;
020import java.util.Map;
021import java.util.concurrent.ConcurrentHashMap;
022
023/**
024 * This class maps <code>ITimeFrame</code>s to <code>Object</code>s. Since values are stored using
025 * <code>ITimeFrameSource</code> implementing objects, the value returned by the source may vary
026 * over time. For example, one implementation of <code>ITimeFrameSource</code> might return the
027 * start and end time of lunch on any given day.
028 * <p>
029 * To associate an object with a dynamic <code>TimeFrame</code> (via <code>ITimeFrameSource</code>),
030 * call <code>put(ITimeFrameSource, Object)</code>. You can later retrieve the first object for a
031 * point in time with <code>get(Time)</code>. The <code>get</code> method is provided for
032 * convenience and is equivalent to <code>get(Time.now())</code>.
033 * <p>
034 * This class is not thread-safe.
035 * 
036 * @author Jonathan Locke
037 * @since 1.2.6
038 * 
039 * @deprecated Since Wicket 9 this class is obsolete and no more used. It will be removed in Wicket 10
040 */
041@Deprecated
042public final class TimeMap implements Serializable
043{
044        private static final long serialVersionUID = 1L;
045
046        /**
047         * <code>Map</code> from <code>ITimeFrameSource</code> implementing objects to
048         * <code>Object</code> values.
049         */
050        private final Map<ITimeFrameSource, Object> sources = new ConcurrentHashMap<>();
051
052        /**
053         * Retrieves an <code>Object</code> for the current <code>Time</code> value.
054         * 
055         * @return <code>Object</code> for the current <code>Time</code> value
056         */
057        public Object get()
058        {
059                return get(Time.now());
060        }
061
062        /**
063         * Retrieves an <code>Object</code> for the given <code>Time</code> value.
064         * 
065         * @param time
066         *            the <code>Time</code> value
067         * @return gets an <code>Object</code> for the given <code>Time</code> value or
068         *         <code>null</code> if none exists
069         */
070        public Object get(final Time time)
071        {
072                for (ITimeFrameSource source : sources.keySet())
073                {
074                        final TimeFrame current = source.getTimeFrame();
075                        if (current.contains(time))
076                        {
077                                return sources.get(current);
078                        }
079                }
080
081                return null;
082        }
083
084        /**
085         * Associates an <code>Object</code> with a dynamic <code>TimeFrame</code>.
086         * 
087         * @param source
088         *            a source that can produce a <code>TimeFrame</code> with which to compare a
089         *            <code>Time</code> value
090         * @param o
091         *            the <code>Object</code> to be returned for the given dynamic
092         *            <code>TimeFrame</code>
093         */
094        public void put(final ITimeFrameSource source, final Object o)
095        {
096                final TimeFrame timeframe = source.getTimeFrame();
097
098                for (ITimeFrameSource tfs : sources.keySet())
099                {
100                        final TimeFrame current = tfs.getTimeFrame();
101
102                        if (timeframe.overlaps(current))
103                        {
104                                throw new IllegalArgumentException("Timeframe " + timeframe +
105                                        " overlaps timeframe " + current);
106                        }
107                }
108
109                sources.put(source, o);
110        }
111}