    001package com.hammurapi.extract;
    002
    003import java.util.Map;
    004import java.util.Set;
    005
    006/**
    007 * Utility class.
    008 * @author Pavel Vlasov
    009 *
    010 */
    011public class ExtractorUtil {
    012
    013        private ExtractorUtil() {
    014                // TODO Auto-generated constructor stub
    015        }
    016
    017        /**
    018         * Wraps extractor into predicate, if given extractor is not already a predicate.
    019         * @param <T>
    020         * @param <C>
    021         * @param extractor
    022         * @return
    023         */
    024        public static <T,C> Predicate<T,C> wrap(final Extractor<T,Boolean,C> extractor) {
    025                if (extractor instanceof Predicate) {
    026                        return (Predicate<T,C>) extractor;
    027                }
    028
    029                return new Predicate<T,C>() {
    030
    031                        @Override
    032                        public Boolean extract(
    033                                        C context,
    034                                        Map<C, Map<Extractor<T, ? super Boolean, C>, ? super Boolean>> cache,
    035                                        T... obj) {
    036                                return extractor.extract(context, cache, obj);
    037                        }
    038
    039                        @Override
    040                        public Set<Integer> parameterIndices() {
    041                                return extractor.parameterIndices();
    042                        }
    043
    044                        @Override
    045                        public boolean isContextDependent() {
    046                                return extractor.isContextDependent();
    047                        }
    048
    049                        @Override
    050                        public ComparisonResult compareTo(Extractor<T, Boolean, C> other) {
    051                                return extractor.compareTo(other);
    052                        }
    053
    054                        @Override
    055                        public double getCost() {
    056                                return extractor.getCost();
    057                        }
    058
    059                };
    060
    061        }
    062
    063        /**
    064         *
    065         * @param <T>
    066         * @param <V>
    067         * @param <C>
    068         * @param source Source extractor.
    069         * @return Instance of mappable if extractor is mappable or its parameter indices is empty. Null otherwise.
    070         */
    071        public static <T,V,C> Mappable<T,V,C> toMappable(final Extractor<T,V,C> source) {
    072                if (source==null) {
    073                        return null;
    074                }
    075                if (source instanceof Mappable) {
    076                        return (Mappable<T,V,C>) source;
    077                }
    078                if (source.parameterIndices().isEmpty()) {
    079                        return new Mappable<T,V,C>() {
    080
    081                                @Override
    082                                public Extractor<T, V, C> map(int[] map) {
    083                                        return source;
    084                                }
    085
    086                        };
    087                }
    088                return null;
    089        }
    090
    091}