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

eu.europa.esig.dss.util.TimeDependentValues Maven / Gradle / Ivy

There is a newer version: 6.0.d4j.2
Show newest version
package eu.europa.esig.dss.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Immutable list of time-dependent values, with the latest value first.
 * 
 * @author jdvorak
 * @param 
 */
public class TimeDependentValues implements Iterable, Serializable {

	protected final List list = new LinkedList();
	private final List immutableList = Collections.unmodifiableList(list);

	/**
	 * Empty list of values.
	 */
	public TimeDependentValues() {
		super();
	}

	/**
	 * Copy constructor.
	 * 
	 * @param srcList
	 */
	public TimeDependentValues(final Iterable srcList) {
		for (final T x : srcList) {
			list.add(x);
		}
	}

	@Override
	public Iterator iterator() {
		return immutableList.iterator();
	}

	public T getLatest() {
		return (list.isEmpty()) ? null : list.get(0);
	}

	public T getCurrent(final Date d) {
		for (final T x : list) {
			if (x.getStartDate().compareTo(d) <= 0) {
				final Date endDate = x.getEndDate();
				if (endDate == null || endDate.compareTo(d) > 0) {
					return x;
				}
			}
		}
		return null;
	}

	public List getAfter(Date notBefore) {
		List result = new ArrayList();
		for (final T x : list) {
			Date endDate = x.getEndDate();
			if (endDate == null || (endDate.compareTo(notBefore) >= 0)) {
				result.add(x);
			}
		}
		return result;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy