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 False<T, C> implements Predicate<T, C> { 017 018 private False() { 019 // Singleton 020 } 021 022 private static False<?, ?> INSTANCE = new False<Object, Object>(); 023 024 @SuppressWarnings("unchecked") 025 public static <T, C> False<T, C> getInstance() { 026 return (False<T, C>) INSTANCE; 027 } 028 029 public ComparisonResult compareTo(Extractor<T, Boolean, C> other) { 030 if (other instanceof False) { 031 return ComparisonResult.EQUAL_NM; 032 } 033 034 if (other instanceof True) { 035 return ComparisonResult.OPPOSITE_NM; 036 } 037 038 if (other == null) { 039 return ComparisonResult.NOT_EQUAL_NM; 040 } 041 042 if (other instanceof Constant) { 043 if (Boolean.FALSE.equals(((Constant) other).getValue())) { 044 return ComparisonResult.EQUAL_NM; 045 } 046 if (Boolean.TRUE.equals(((Constant) other).getValue())) { 047 return ComparisonResult.OPPOSITE_NM; 048 } 049 } 050 051 ComparisonResult cr = other.compareTo(this); 052 switch (cr.getType()) { 053 case EQUAL: 054 case NOT_EQUAL: 055 case OPPOSITE: 056 return cr; 057 case LESS_RESTRICTIVE: 058 return ComparisonResult.MORE_RESTRICTIVE_NM; 059 case MORE_RESTRICTIVE: 060 return ComparisonResult.LESS_RESTRICTIVE_NM; 061 case OPPOSITE_LESS_RESTRICTIVE: 062 return ComparisonResult.OPPOSITE_MORE_RESTRICTIVE_NM; 063 case OPPOSITE_MORE_RESTRICTIVE: 064 return ComparisonResult.OPPOSITE_LESS_RESTRICTIVE_NM; 065 default: 066 throw new IllegalArgumentException("Unexpected comparison type: "+cr.getType()); 067 } 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}