    001package com.hammurapi.extract;
    002
    003import java.util.Collections;
    004import java.util.Map;
    005import java.util.Set;
    006
    007
    008/**
    009 * Predicate which always evaluates to true. This predicate is primarily used for
    010 * comparison
    011 * @author Pavel Vlasov
    012 *
    013 * @param <T>
    014 * @param <C>
    015 */
    016public class True<T, C> implements Predicate<T, C> {
    017
    018        private True() {
    019                // Singleton
    020        }
    021
    022        private static True<?, ?> INSTANCE = new True<Object, Object>();
    023
    024        @SuppressWarnings("unchecked")
    025        public static <T, C> True<T, C> getInstance() {
    026                return (True<T, C>) INSTANCE;
    027        }
    028
    029        public ComparisonResult compareTo(Extractor<T, Boolean, C> otherPredicate) {
    030                if (otherPredicate instanceof True) {
    031                        return ComparisonResult.EQUAL_NM;
    032                }
    033
    034                if (otherPredicate instanceof False) {
    035                        return ComparisonResult.OPPOSITE_NM;
    036                }
    037
    038
    039                if (otherPredicate == null) {
    040                        return ComparisonResult.NOT_EQUAL_NM;
    041                }
    042
    043                if (otherPredicate instanceof Constant) {
    044                        if (Boolean.TRUE.equals(((Constant) otherPredicate).getValue())) {
    045                                return ComparisonResult.EQUAL_NM;
    046                        }
    047                        if (Boolean.FALSE.equals(((Constant) otherPredicate).getValue())) {
    048                                return ComparisonResult.OPPOSITE_NM;
    049                        }
    050                }
    051
    052                ComparisonResult cr = otherPredicate.compareTo(this);
    053                switch (cr.getType()) {
    054                case EQUAL:
    055                case NOT_EQUAL:
    056                case OPPOSITE:
    057                        return cr;
    058                case LESS_RESTRICTIVE:
    059                        return ComparisonResult.MORE_RESTRICTIVE_NM;
    060                case MORE_RESTRICTIVE:
    061                        return ComparisonResult.LESS_RESTRICTIVE_NM;
    062                case OPPOSITE_LESS_RESTRICTIVE:
    063                        return ComparisonResult.OPPOSITE_MORE_RESTRICTIVE_NM;
    064                case OPPOSITE_MORE_RESTRICTIVE:
    065                        return ComparisonResult.OPPOSITE_LESS_RESTRICTIVE_NM;
    066                default:
    067                        throw new IllegalArgumentException("Unexpected comparison type: "+cr.getType());
    068                }
    069        }
    070
    071        public Boolean extract(
    072                        C context,
    073                        Map<C, Map<Extractor<T, ? super Boolean, C>, ? super Boolean>> cache,
    074                        T... obj) {
    075                return Boolean.TRUE;
    076        }
    077
    078        public boolean isContextDependent() {
    079                return false;
    080        }
    081
    082        public Set<Integer> parameterIndices() {
    083                return Collections.emptySet();
    084        }
    085
    086        @Override
    087        public String toString() {
    088                return getClass().getName();
    089        }
    090
    091        @Override
    092        public double getCost() {
    093                return 0;
    094        }
    095
    096}