    001package com.hammurapi.extract;
    002
    003import java.util.Collections;
    004import java.util.Map;
    005import java.util.Set;
    006import java.util.concurrent.TimeUnit;
    007
    008/**
    009 * This predicate evaluates to true for <code>timeInterval</code> after construction.
    010 * Then it evaluates to false.
    011 * @author Pavel Vlasov
    012 *
    013 * @param <T>
    014 * @param <C>
    015 */
    016public class TimeIntervalPredicate<T,C> implements Predicate<T,C> {
    017
    018        private boolean nanos;
    019        long end;
    020
    021        public TimeIntervalPredicate(TimeUnit timeUnit, long interval) {
    022                nanos = TimeUnit.MILLISECONDS.compareTo(timeUnit)>0;
    023                long start = nanos ? System.nanoTime() : System.currentTimeMillis();
    024                end = start + (nanos ? TimeUnit.NANOSECONDS : TimeUnit.MILLISECONDS).convert(interval, timeUnit);
    025        }
    026
    027        @Override
    028        public Boolean extract(
    029                        C context,
    030                        Map<C, Map<Extractor<T, ? super Boolean, C>, ? super Boolean>> cache,
    031                        T... obj) {
    032                return (nanos ? System.nanoTime() : System.currentTimeMillis()) < end;
    033        }
    034
    035        @Override
    036        public Set<Integer> parameterIndices() {
    037                return Collections.emptySet();
    038        }
    039
    040        @Override
    041        public boolean isContextDependent() {
    042                return false;
    043        }
    044
    045        @Override
    046        public ComparisonResult compareTo(Extractor<T, Boolean, C> other) {
    047                if (equals(other)) {
    048                        return ComparisonResult.EQUAL_NM;
    049                }
    050                return ComparisonResult.NOT_EQUAL_NM;
    051        }
    052
    053        @Override
    054        public double getCost() {
    055                return 0;
    056        }
    057
    058        @Override
    059        public int hashCode() {
    060                final int prime = 31;
    061                int result = 1;
    062                result = prime * result + (int) (end ^ (end >>> 32));
    063                result = prime * result + (nanos ? 1231 : 1237);
    064                return result;
    065        }
    066
    067        @Override
    068        public boolean equals(Object obj) {
    069                if (this == obj)
    070                        return true;
    071                if (obj == null)
    072                        return false;
    073                if (getClass() != obj.getClass())
    074                        return false;
    075                TimeIntervalPredicate other = (TimeIntervalPredicate) obj;
    076                if (end != other.end)
    077                        return false;
    078                if (nanos != other.nanos)
    079                        return false;
    080                return true;
    081        }
    082
    083}