| 1 | package com.hammurapi.extract; |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Extractor which maps arguments to the target extractor. |
| 6 | * @author Pavel Vlasov |
| 7 | * |
| 8 | * @param <T> |
| 9 | * @param <V> |
| 10 | * @param <C> |
| 11 | */ |
| 12 | public class MappedPredicate<T, C> extends MappedExtractor<T, Boolean, C> implements Predicate<T, C> { |
| 13 | |
| 14 | /** |
| 15 | * @param target Target extractor |
| 16 | * @param map Index map, e.g. if it is {5, 3} then the first argument |
| 17 | * of this extractor becomes the sixth argument of the target extractor |
| 18 | * and the second argument of this extractor becomes the 4th argument |
| 19 | * of the target extractor. |
| 20 | */ |
| 21 | protected MappedPredicate(Predicate<T, C> target, int[] map) { |
| 22 | super(target, map); |
| 23 | } |
| 24 | |
| 25 | public static <T,C> Predicate<T,C> mapPredicate(Predicate<T,C> target, int[] map) { |
| 26 | if (ComparisonResult.isOneToOneMapping(map)) { |
| 27 | return target; |
| 28 | } |
| 29 | |
| 30 | // Target is not index-dependent (e.g. constant). |
| 31 | if (target.parameterIndices().isEmpty()) { |
| 32 | return target; |
| 33 | } |
| 34 | if (target instanceof Mappable) { |
| 35 | @SuppressWarnings("unchecked") |
| 36 | Extractor<T, Boolean, C> ret = ((Mappable<T,Boolean,C>) target).map(map); |
| 37 | if (ret!=null) { |
| 38 | return ExtractorUtil.wrap(ret); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return new MappedPredicate<T, C>(target, map); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | } |