001package com.hammurapi.extract;
002
003import java.util.Collections;
004import java.util.Map;
005import java.util.Set;
006
007/**
008 * Constants don't extract any data from sources, but may extract
009 * data from context.
010 * @author Pavel Vlasov.
011 *
012 * @param <V>
013 * @param <C>
014 */
015public class Constant<T, V, C> extends ExtractorBase<T, V, C> {
016        
017        private V value;
018        
019        protected Constant() {
020                super(0,null);
021        }
022
023        public V getValue() {
024                return value;
025        }
026
027        public Constant(V value) {
028                super(0,null);
029                this.value = value;
030        }
031
032        protected V extractInternal(C context, Map<C, Map<Extractor<T, ? super V, C>, ? super V>> cache, T... obj) {
033                return value;
034        }
035
036        public Set<Integer> parameterIndices() {          
037                return Collections.emptySet();
038        }
039
040        @Override
041        public int hashCode() {
042                final int prime = 31;
043                int result = 1;
044                result = prime * result + ((value == null) ? 0 : value.hashCode());
045                return result;
046        }
047
048        @SuppressWarnings("unchecked")
049        @Override
050        public boolean equals(Object obj) {
051                if (this == obj)
052                        return true;
053                if (obj == null)
054                        return false;
055                if (getClass() != obj.getClass())
056                        return false;
057                Constant other = (Constant) obj;
058                if (value == null) {
059                        if (other.value != null)
060                                return false;
061                } else if (!value.equals(other.value))
062                        return false;
063                return true;
064        }
065
066        public boolean isContextDependent() {
067                return false;
068        }
069        
070        @Override
071        public String toString() {
072                return getClass().getName()+"("+(value==null ? "null" : value + "["+value.getClass()+"]")+")";
073        }
074        
075        @Override
076        public ComparisonResult compareTo(Extractor<T, V, C> other) {
077                if (other instanceof Constant) {
078                        Object otherValue = ((Constant) other).value;
079                        if (value==null) {
080                                return otherValue==null ? ComparisonResult.EQUAL_NM : ComparisonResult.UNEQUAL_NM; 
081                        }
082                        
083                        // TODO - Number promotion and comparison.
084                        
085                        return value.equals(otherValue) ? ComparisonResult.EQUAL_NM : value instanceof Boolean ? ComparisonResult.OPPOSITE_NM : ComparisonResult.UNEQUAL_NM;
086                }
087                return super.compareTo(other);
088        }
089
090}