Author: mickw
Date: 2006-03-31 09:55:55 +0200 (Fri, 31 Mar 2006)
New Revision: 2674

Modified:
   
trunk/src/java/no/schibstedsok/front/searchportal/configuration/XMLSearchTabsCreator.java
   
trunk/src/java/no/schibstedsok/front/searchportal/query/run/RunningQueryImpl.java
   
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenEvaluatorFactoryImpl.java
   
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenPredicate.java
   
trunk/src/java/no/schibstedsok/front/searchportal/query/transform/PrefixRemoverTransformer.java
Log:
TokenPredicate class now enum. fast & compact :-8


Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/configuration/XMLSearchTabsCreator.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/configuration/XMLSearchTabsCreator.java
   2006-03-31 07:28:02 UTC (rev 2673)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/configuration/XMLSearchTabsCreator.java
   2006-03-31 07:55:55 UTC (rev 2674)
@@ -2,6 +2,12 @@
 package no.schibstedsok.front.searchportal.configuration;
 
 import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
+import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 import com.thoughtworks.xstream.io.xml.DomDriver;
 import java.util.HashMap;
 import java.util.Map;

Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/query/run/RunningQueryImpl.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/query/run/RunningQueryImpl.java
   2006-03-31 07:28:02 UTC (rev 2673)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/query/run/RunningQueryImpl.java
   2006-03-31 07:55:55 UTC (rev 2674)
@@ -120,7 +120,7 @@
 
     private List<TokenMatch> getTokenMatches(final String token) {
         final ReportingTokenEvaluator e
-                = (ReportingTokenEvaluator) 
tokenEvaluatorFactory.getEvaluator(TokenPredicate.valueOf(token));
+                = (ReportingTokenEvaluator) 
tokenEvaluatorFactory.getEvaluator(TokenPredicate.valueFor(token));
         return e.reportToken(token, queryStr);
     }
 

Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenEvaluatorFactoryImpl.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenEvaluatorFactoryImpl.java
        2006-03-31 07:28:02 UTC (rev 2673)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenEvaluatorFactoryImpl.java
        2006-03-31 07:55:55 UTC (rev 2674)
@@ -37,6 +37,8 @@
     private static final Log LOG = 
LogFactory.getLog(TokenEvaluatorFactoryImpl.class);
     private static final String ERR_FAST_EVALUATOR_CREATOR_INTERRUPTED = 
             "Interrupted waiting for FastEvaluatorCreator. Analysis on this 
query will fail.";
+    private static final String ERR_TOKENTYPE_WIHOUT_IMPL = "Token type not 
known or implemented. ";
+    private static final String ERR_GENERIC_TOKENTYPE_WIHOUT_IMPL = "Generic 
token type not known or implemented. ";
 
     private final Context context;
     private final Thread fastEvaluatorCreator;
@@ -70,25 +72,23 @@
      */
     public TokenEvaluator getEvaluator(final TokenPredicate token) {
 
-        if ( token == TokenPredicate.ALWAYSTRUE ) {
-            return ALWAYS_TRUE_EVALUATOR;
-        }  else if ( token instanceof TokenPredicate.FastTokenPredicate ) {
-            return getFastEvaluator();
-        }  else if ( token instanceof TokenPredicate.RegExpTokenPredicate ) {
-            return RegExpEvaluatorFactory.valueOf(context).getEvaluator(token);
-        } else if ( token instanceof TokenPredicate.JepTokenPredicate ){
-            return jedEvaluator;
+        switch( token.getType() ){
+            case GENERIC:
+                switch( token ){
+                    case ALWAYSTRUE:
+                        return ALWAYS_TRUE_EVALUATOR;
+                    default:
+                        throw new 
IllegalArgumentException(ERR_GENERIC_TOKENTYPE_WIHOUT_IMPL + token);
+                }
+            case FAST:
+                return getFastEvaluator();
+            case REGEX:
+                return 
RegExpEvaluatorFactory.valueOf(context).getEvaluator(token);
+            case JEP:
+                return jedEvaluator;
+            default:
+                throw new IllegalArgumentException(ERR_TOKENTYPE_WIHOUT_IMPL + 
token);
         }
-//        } else if (token == TokenPredicate.GEO ) { // shouldn't be called as 
it's a OrPredicate from AnalysisRules
-//            return getFastEvaluator();
-//        } else if (token == TokenPredicate.NAMELONGERTHANWIKIPEDIA ) { // 
FIXME where the hell is this used?
-//            return getFastEvaluator();
-//        } else if (token == TokenPredicate.EXACT_ ) { // FIXME where the 
hell is this used?
-//            return getFastEvaluator();
-//        } else if (token == TokenPredicate.PICTURE ) { // FIXME where the 
hell is this used?
-//            return getFastEvaluator();
-
-        throw new RuntimeException("Unknown token " + token);
     }
 
     public String getQueryString() {

Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenPredicate.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenPredicate.java
   2006-03-31 07:28:02 UTC (rev 2673)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/query/token/TokenPredicate.java
   2006-03-31 07:55:55 UTC (rev 2674)
@@ -2,6 +2,7 @@
 package no.schibstedsok.front.searchportal.query.token;
 
 
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
@@ -16,110 +17,138 @@
  * @author <a href="mailto:[EMAIL PROTECTED]">Magnus Eklund</a>
  * @version <tt>$Revision$</tt>
  */
-public class TokenPredicate implements Predicate, Comparable<TokenPredicate> {
+public enum TokenPredicate implements Predicate {
 
-    public static final class FastTokenPredicate extends TokenPredicate {
-        public  FastTokenPredicate(final String token) {
-            super(token);
-            FAST_TOKENS.add( this );
-        }
-    }
-    public static final class RegExpTokenPredicate extends TokenPredicate {
-        public  RegExpTokenPredicate(final String token) {
-            super(token);
-        }
-    }
-    public static final class JepTokenPredicate extends TokenPredicate {
-        public  JepTokenPredicate(final String token) {
-            super(token);
-        }
-    }
-
-    private static final Map<String,TokenPredicate> TOKEN_MAP = new 
Hashtable<String,TokenPredicate>();
-    private static final Set<TokenPredicate> FAST_TOKENS = new 
HashSet<TokenPredicate>();
-
     // Common predicates.
-    // [TODO] TokenPredicate should be turned into a Java5 enum object with 
this list.
+    ALWAYSTRUE (Type.GENERIC),
 
-    public static final TokenPredicate ALWAYSTRUE = new 
TokenPredicate("alwaysTrue");
-
     // Fast TokenPredicates
-    public static final FastTokenPredicate EXACTFIRST = new 
FastTokenPredicate("exact_firstname");
-    public static final FastTokenPredicate EXACTLAST = new 
FastTokenPredicate("exact_lastname");
-    public static final FastTokenPredicate TNS = new FastTokenPredicate("tns");
-    public static final FastTokenPredicate FIRSTNAME = new 
FastTokenPredicate("firstname");
-    public static final FastTokenPredicate LASTNAME = new 
FastTokenPredicate("lastname");
-    public static final FastTokenPredicate COMPANYENRICHMENT = new 
FastTokenPredicate("companyenrichment");
-    public static final FastTokenPredicate EXACTCOMPANYENRICHMENT = new 
FastTokenPredicate("exact_companyenrichment");
-    public static final FastTokenPredicate COMPANYRANK = new 
FastTokenPredicate("companyrank");
-    public static final FastTokenPredicate EXACTCOMPANYRANK = new 
FastTokenPredicate("exact_companyrank");
-    public static final FastTokenPredicate GEOLOCAL = new 
FastTokenPredicate("geolocal");
-    public static final FastTokenPredicate GEOGLOBAL = new 
FastTokenPredicate("geoglobal");
-    public static final FastTokenPredicate GEOLOCALEXACT = new 
FastTokenPredicate("exact_geolocal");
-    public static final FastTokenPredicate GEOGLOBALEXACT = new 
FastTokenPredicate("exact_geoglobal");
-    public static final FastTokenPredicate CATEGORY = new 
FastTokenPredicate("category");
-    public static final FastTokenPredicate PRIOCOMPANYNAME = new 
FastTokenPredicate("companypriority");
-    public static final FastTokenPredicate KEYWORD = new 
FastTokenPredicate("keyword");
-    public static final FastTokenPredicate FULLNAME = new 
FastTokenPredicate("fullname"); // ?
-    public static final FastTokenPredicate EXACTWIKI = new 
FastTokenPredicate("exact_wikino");
-    public static final FastTokenPredicate WIKIPEDIA = new 
FastTokenPredicate("wikino");
-    public static final FastTokenPredicate ENGLISHWORDS = new 
FastTokenPredicate("international");
-    public static final FastTokenPredicate TOP3EXACT = new 
FastTokenPredicate("exact_top3boosts");
-    public static final FastTokenPredicate EXACT_PPCTOPLIST = new 
FastTokenPredicate("exact_ppctoplist");
-
+    EXACTFIRST (Type.FAST, "exact_firstname"),
+    EXACTLAST (Type.FAST, "exact_lastname"),
+    TNS (Type.FAST, "tns"),
+    FIRSTNAME (Type.FAST, "firstname"),
+    LASTNAME (Type.FAST, "lastname"),
+    COMPANYENRICHMENT (Type.FAST, "companyenrichment"),
+    EXACTCOMPANYENRICHMENT (Type.FAST, "exact_companyenrichment"),
+    COMPANYRANK (Type.FAST, "companyrank"),
+    EXACTCOMPANYRANK (Type.FAST, "exact_companyrank"),
+    GEOLOCAL (Type.FAST, "geolocal"),
+    GEOGLOBAL (Type.FAST, "geoglobal"),
+    GEOLOCALEXACT (Type.FAST, "exact_geolocal"),
+    GEOGLOBALEXACT (Type.FAST, "exact_geoglobal"),
+    CATEGORY (Type.FAST, "category"),
+    PRIOCOMPANYNAME (Type.FAST, "companypriority"),
+    KEYWORD (Type.FAST, "keyword"),
+    FULLNAME (Type.FAST, "fullname"), // ?
+    EXACTWIKI (Type.FAST, "exact_wikino"),
+    WIKIPEDIA (Type.FAST, "wikino"),
+    ENGLISHWORDS (Type.FAST, "international"),
+    TOP3EXACT (Type.FAST, "exact_top3boosts"),
+    EXACT_PPCTOPLIST (Type.FAST, "exact_ppctoplist"),
+    CELEBRITY (Type.FAST, "kjendiser"),
+    ANIMAL (Type.FAST, "dyr"),
+    DISEASE (Type.FAST, "sykdommer"),
     
     // RegExp TokenPredicates
-    public static final RegExpTokenPredicate CATALOGUEPREFIX = new 
RegExpTokenPredicate("cataloguePrefix");
-    public static final RegExpTokenPredicate COMPANYSUFFIX = new 
RegExpTokenPredicate("companySuffix");
-    public static final RegExpTokenPredicate NEWSPREFIX = new 
RegExpTokenPredicate("newsPrefix");
-    public static final RegExpTokenPredicate ORGNR = new 
RegExpTokenPredicate("orgNr");
-    public static final RegExpTokenPredicate PICTUREPREFIX = new 
RegExpTokenPredicate("picturePrefix");
-    public static final RegExpTokenPredicate PHONENUMBER = new 
RegExpTokenPredicate("phoneNumber");
-    public static final RegExpTokenPredicate SITEPREFIX = new 
RegExpTokenPredicate("sitePrefix");
-    public static final RegExpTokenPredicate TVPREFIX = new 
RegExpTokenPredicate("tvPrefix");
-    public static final RegExpTokenPredicate WEATHERPREFIX = new 
RegExpTokenPredicate("weatherPrefix");
-    public static final RegExpTokenPredicate WIKIPEDIAPREFIX = new 
RegExpTokenPredicate("wikipediaPrefix");
-    public static final RegExpTokenPredicate SKIINFOPREFIX = new 
RegExpTokenPredicate("skiinfoPrefix");
-    public static final RegExpTokenPredicate ONLYSKIINFOPREFIX = new 
RegExpTokenPredicate("onlyskiinfoPrefix");
-    public static final RegExpTokenPredicate EMPTYQUERY = new 
RegExpTokenPredicate("emptyQuery");
+    CATALOGUEPREFIX (Type.REGEX),
+    COMPANYSUFFIX (Type.REGEX),
+    NEWSPREFIX (Type.REGEX),
+    ORGNR (Type.REGEX),
+    PICTUREPREFIX (Type.REGEX),
+    PHONENUMBER (Type.REGEX),
+    SITEPREFIX (Type.REGEX),
+    TVPREFIX (Type.REGEX),
+    WEATHERPREFIX (Type.REGEX),
+    WIKIPEDIAPREFIX (Type.REGEX),
+    SKIINFOPREFIX (Type.REGEX),
+    ONLYSKIINFOPREFIX (Type.REGEX),
+    EMPTYQUERY (Type.REGEX),
     
     // JepTokenPredicate
-    public static final JepTokenPredicate MATHPREDICATE = new 
JepTokenPredicate("mathExpression");
+    MATHPREDICATE (Type.JEP);
 
+    // The types of TokenPredicates that exist
+    public enum Type { GENERIC, FAST, REGEX, JEP }
+
+    /** Some WIERD-ARSE behavour to enum.
+     * Because the enum declarations must come first and they are static,
+     *      their constructor when referencing other static members are 
referencing them
+     *      before they themselves have been statically initialised.
+     * That is without the wrapping class declaration to FAST_TOKENS it would 
have a value
+     *      of null when referenced to in the constructor.
+     * By wrapping it inside an inner class because all static initialisors of 
the inner class are run first
+     *      it ensures FAST_TOKENS will not be null.
+     **/
+    private static final class Sets{
+        public static final Set<TokenPredicate> FAST_TOKENS = new 
HashSet<TokenPredicate>();
+    }
+    
+
     // instance fields
     private final String token;
+    private final Type type;
 
 
     private static final String ERR_ARG_NOT_TOKEN_EVALUATOR_FACTORY
             = "Argument to evuluate must be an instance of a 
TokenEvaluatorFactory";
+    private static final String ERR_TOKEN_NOT_FOUND = "Token argument not 
found ";
 
+
     /**
-     * Create a new TokenPredicate that will return true iff token occurs in 
the
+     * Create a new TokenPredicate that will return true if token occurs in the
      * query.
      *
+     * @param type     the token type.
+     */
+    TokenPredicate(final Type type) {
+        token = null;
+        this.type = type;
+        if( type == Type.FAST ){
+            Sets.FAST_TOKENS.add(this);
+        }
+    }
+
+    /**
+     * Create a new TokenPredicate that will return true if token occurs in the
+     * query.
+     * Only use for token of type FAST.
+     *
+     * @param type     the token type.
      * @param token     the token.
      */
-    protected TokenPredicate(final String token) {
+    TokenPredicate(final Type type, final String token) {
         this.token = token;
-        TOKEN_MAP.put(token, this);
+        this.type = type;
+        if( type == Type.FAST ){
+            Sets.FAST_TOKENS.add(this);
+        }
     }
 
+    public Type getType(){
+        return type;
+    }
+
     /** Public method to find the correct TokenPredicate given the Token's 
string.
      */
-    public static TokenPredicate valueOf(final String token) {
-        return TOKEN_MAP.get(token);
+    public static TokenPredicate valueFor(final String token) {
+        for(TokenPredicate tp : values()){
+            if( tp.token.equals(token) ){
+                return tp;
+            }
+        }
+        throw new IllegalArgumentException(ERR_TOKEN_NOT_FOUND + token);
     }
 
     /** Utility method to use all TokenPredicates in existance.
      */
     public static Collection<TokenPredicate> getTokenPredicates() {
-        return Collections.unmodifiableCollection(TOKEN_MAP.values());
+        return Collections.unmodifiableCollection(Arrays.asList(values()));
     }
 
     /** Utility method to use all FastTokenPredicates in existance.
      */
     public static Set<TokenPredicate> getFastTokenPredicates() {
-        return Collections.unmodifiableSet(FAST_TOKENS);
+        return Collections.unmodifiableSet(Sets.FAST_TOKENS);
     }
 
     /**
@@ -156,18 +185,4 @@
         return evaluator.evaluateToken(token, factory.getCurrentTerm(), query);
     }
 
-    /** [EMAIL PROTECTED]
-     */
-    public int compareTo(final TokenPredicate tp) {
-
-        return token.compareTo(tp.token);
-    }
-
-    /** [EMAIL PROTECTED]
-     */
-    public String toString() {
-        return "TokenPredicate: " + token;
-    }
-
-
 }

Modified: 
trunk/src/java/no/schibstedsok/front/searchportal/query/transform/PrefixRemoverTransformer.java
===================================================================
--- 
trunk/src/java/no/schibstedsok/front/searchportal/query/transform/PrefixRemoverTransformer.java
     2006-03-31 07:28:02 UTC (rev 2673)
+++ 
trunk/src/java/no/schibstedsok/front/searchportal/query/transform/PrefixRemoverTransformer.java
     2006-03-31 07:55:55 UTC (rev 2674)
@@ -24,7 +24,7 @@
 public final class PrefixRemoverTransformer extends AbstractQueryTransformer {
     
     private static final Collection<TokenPredicate> DEFAULT_PREFIXES = 
Collections.unmodifiableCollection(
-            Arrays.asList( (TokenPredicate) // cast the first item in the 
varargs to force generics
+            Arrays.asList( 
                 TokenPredicate.SITEPREFIX,
                 TokenPredicate.CATALOGUEPREFIX,
                 TokenPredicate.PICTUREPREFIX,

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to