| 1 | package com.hammurapi.extract; |
| 2 | |
| 3 | import java.util.Collections; |
| 4 | import java.util.Map; |
| 5 | import java.util.Set; |
| 6 | |
| 7 | /** |
| 8 | * Constants don't extract any data from sources, but may extract |
| 9 | * data from context. |
| 10 | * @author Pavel Vlasov. |
| 11 | * |
| 12 | * @param <V> |
| 13 | * @param <C> |
| 14 | */ |
| 15 | public class Constant<T, V, C> extends ExtractorBase<T, V, C> { |
| 16 | |
| 17 | private V value; |
| 18 | |
| 19 | protected Constant() { |
| 20 | super(0,null); |
| 21 | } |
| 22 | |
| 23 | public V getValue() { |
| 24 | return value; |
| 25 | } |
| 26 | |
| 27 | public Constant(V value) { |
| 28 | super(0,null); |
| 29 | this.value = value; |
| 30 | } |
| 31 | |
| 32 | protected V extractInternal(C context, Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, T... obj) { |
| 33 | return value; |
| 34 | } |
| 35 | |
| 36 | public Set<Integer> parameterIndices() { |
| 37 | return Collections.emptySet(); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public int hashCode() { |
| 42 | final int prime = 31; |
| 43 | int result = 1; |
| 44 | result = prime * result + ((value == null) ? 0 : value.hashCode()); |
| 45 | return result; |
| 46 | } |
| 47 | |
| 48 | @SuppressWarnings("unchecked") |
| 49 | @Override |
| 50 | public boolean equals(Object obj) { |
| 51 | if (this == obj) |
| 52 | return true; |
| 53 | if (obj == null) |
| 54 | return false; |
| 55 | if (getClass() != obj.getClass()) |
| 56 | return false; |
| 57 | Constant other = (Constant) obj; |
| 58 | if (value == null) { |
| 59 | if (other.value != null) |
| 60 | return false; |
| 61 | } else if (!value.equals(other.value)) |
| 62 | return false; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | public boolean isContextDependent() { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return getClass().getName()+"("+(value==null ? "null" : value + "["+value.getClass()+"]")+")"; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public ComparisonResult compareTo(Extractor<T, V, C> other) { |
| 77 | if (other instanceof Constant) { |
| 78 | Object otherValue = ((Constant) other).value; |
| 79 | if (value==null) { |
| 80 | return otherValue==null ? ComparisonResult.EQUAL_NM : ComparisonResult.UNEQUAL_NM; |
| 81 | } |
| 82 | |
| 83 | // TODO - Number promotion and comparison. |
| 84 | |
| 85 | return value.equals(otherValue) ? ComparisonResult.EQUAL_NM : value instanceof Boolean ? ComparisonResult.OPPOSITE_NM : ComparisonResult.UNEQUAL_NM; |
| 86 | } |
| 87 | return super.compareTo(other); |
| 88 | } |
| 89 | |
| 90 | } |