All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wicket.util.time.TimeMap Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.util.time;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * This class maps ITimeFrames to Objects. Since values are stored using
 * ITimeFrameSource implementing objects, the value returned by the source may vary
 * over time. For example, one implementation of ITimeFrameSource might return the
 * start and end time of lunch on any given day.
 * 

* To associate an object with a dynamic TimeFrame (via ITimeFrameSource), * call put(ITimeFrameSource, Object). You can later retrieve the first object for a * point in time with get(Time). The get method is provided for * convenience and is equivalent to get(Time.now()). *

* This class is not thread-safe. * * @author Jonathan Locke * @since 1.2.6 */ public final class TimeMap implements Serializable { private static final long serialVersionUID = 1L; /** * Map from ITimeFrameSource implementing objects to * Object values. */ private final Map sources = new ConcurrentHashMap<>(); /** * Retrieves an Object for the current Time value. * * @return Object for the current Time value */ public Object get() { return get(Time.now()); } /** * Retrieves an Object for the given Time value. * * @param time * the Time value * @return gets an Object for the given Time value or * null if none exists */ public Object get(final Time time) { for (ITimeFrameSource source : sources.keySet()) { final TimeFrame current = source.getTimeFrame(); if (current.contains(time)) { return sources.get(current); } } return null; } /** * Associates an Object with a dynamic TimeFrame. * * @param source * a source that can produce a TimeFrame with which to compare a * Time value * @param o * the Object to be returned for the given dynamic * TimeFrame */ public void put(final ITimeFrameSource source, final Object o) { final TimeFrame timeframe = source.getTimeFrame(); for (ITimeFrameSource tfs : sources.keySet()) { final TimeFrame current = tfs.getTimeFrame(); if (timeframe.overlaps(current)) { throw new IllegalArgumentException("Timeframe " + timeframe + " overlaps timeframe " + current); } } sources.put(source, o); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy