lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From DM Smith <dmsmith...@gmail.com>
Subject Parameter class and Java 5 Enums
Date Mon, 19 Oct 2009 21:19:12 GMT
Should the Parameter class be replaced with Java 5 enums? My only 
concern is backward compatibility. I noticed that Parameter is 
serializable. Is this used by Lucene? I wasn't able to see any place 
that depended on it. The only public method, Parameter.toString() 
results in the same value as a Java 5 Enum.

It seems that an advanced form of enums would be helpful, too. I'm 
seeing a lot of "switch" statements on their value:
e.g.
In AbstractField:
     if (store == Field.Store.YES){
       this.isStored = true;
     }
     else if (store == Field.Store.NO){
       this.isStored = false;
     }
     else
       throw new IllegalArgumentException("unknown store parameter " + 
store);

     if (index == Field.Index.NO) {
       this.isIndexed = false;
       this.isTokenized = false;
     } else if (index == Field.Index.ANALYZED) {
       this.isIndexed = true;
       this.isTokenized = true;
     } else if (index == Field.Index.NOT_ANALYZED) {
       this.isIndexed = true;
       this.isTokenized = false;
     } else if (index == Field.Index.NOT_ANALYZED_NO_NORMS) {
       this.isIndexed = true;
       this.isTokenized = false;
       this.omitNorms = true;
     } else if (index == Field.Index.ANALYZED_NO_NORMS) {
       this.isIndexed = true;
       this.isTokenized = true;
       this.omitNorms = true;
     } else {
       throw new IllegalArgumentException("unknown index parameter " + 
index);
     }

This could be reduced to:
this.stored = store.isStored();
this.isIndexed = index.isIndexed();
this.isTokenized = index.isTokenized();
this.omitNorms = index.omitNorms();

With the following:
public enum Store {
   YES   { public boolean isStored() { return true; } },
   NO    { public boolean isStored() { return false; } };

   // Determine whether this is stored or not
   abstract boolean isStored();
}

public enum Index {
ANALYZED {
    public boolean isIndexed() { return true; }
    public boolean isTokenized() { return true; }
    public boolean omitNorms() { return false; }
    ...
},
...

abstract boolean isIndexed();
abstract boolean isTokenized();
abstract boolean omitNorms();
...
}

What I like about this pattern is that it clearly documents what each 
member does. As it is it is spread around in the files.

One can add a "picker" method to these to serve as a factory. E.g. given 
indexed = true, tokenized = false, ... what is the appropriate value 
from the Index enum.



-- DM


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message