Skip to content

Apache Lucene open-source search software

Introduce a serializable Parameter Class.

Christoph Goller3e955f8c7782
diff --git a/src/java/org/apache/lucene/document/Field.java b/src/java/org/apache/lucene/document/Field.java
index d95a6e6ac97..4979876c4ac 100644
--- a/src/java/org/apache/lucene/document/Field.java
+++ b/src/java/org/apache/lucene/document/Field.java
@@ -17,11 +17,13 @@ package org.apache.lucene.document;
*/
import java.io.Reader;
+import java.io.Serializable;
import java.util.Date;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Similarity;
+import org.apache.lucene.util.Parameter;
/**
A field is a section of a Document. Each field has two parts, a name and a
@@ -31,7 +33,7 @@ import org.apache.lucene.search.Similarity;
index, so that they may be returned with hits on the document.
*/
-public final class Field implements java.io.Serializable {
+public final class Field implements Serializable {
private String name = "body";
// the one and only data object for all different kind of field values
@@ -48,14 +50,10 @@ public final class Field implements java.io.Serializable {
private float boost = 1.0f;
- public static final class Store {
- private String name;
- private Store() {}
+ public static final class Store extends Parameter implements Serializable {
+
private Store(String name) {
- this.name = name;
- }
- public String toString() {
- return name;
+ super(name);
}
/** Store the original field value in the index in a compressed form. This is
@@ -74,24 +72,23 @@ public final class Field implements java.io.Serializable {
public static final Store NO = new Store("NO");
}
- public static final class Index {
- private String name;
- private Index() {}
+ public static final class Index extends Parameter implements Serializable {
+
private Index(String name) {
- this.name = name;
- }
- public String toString() {
- return name;
+ super(name);
}
+
/** Do not index the field value. This field can thus not be searched,
* but one can still access its contents provided it is
* {@link Field.Store stored}. */
public static final Index NO = new Index("NO");
+
/** Index the field's value so it can be searched. An Analyzer will be used
* to tokenize and possibly further normalize the text before its
* terms will be stored in the index. This is useful for common text.
*/
public static final Index TOKENIZED = new Index("TOKENIZED");
+
/** Index the field's value without using an Analyzer, so it can be searched.
* As no analyzer is used the value will be stored as a single term. This is
* useful for unique Ids like product numbers.
@@ -99,15 +96,10 @@ public final class Field implements java.io.Serializable {
public static final Index UN_TOKENIZED = new Index("UN_TOKENIZED");
}
- public static final class TermVector {
- private String name;
- private TermVector() {}
+ public static final class TermVector extends Parameter implements Serializable {
+
private TermVector(String name) {
- this.name = name;
- }
-
- public String toString() {
- return name;
+ super(name);
}
/** Do not store term vectors.
diff --git a/src/java/org/apache/lucene/search/BooleanClause.java b/src/java/org/apache/lucene/search/BooleanClause.java
index c298be2fd65..c2d44bfe437 100644
--- a/src/java/org/apache/lucene/search/BooleanClause.java
+++ b/src/java/org/apache/lucene/search/BooleanClause.java
@@ -1,7 +1,6 @@
package org.apache.lucene.search;
-import java.io.ObjectStreamException;
-import java.io.StreamCorruptedException;
+import org.apache.lucene.util.Parameter;
/**
* Copyright 2004 The Apache Software Foundation
@@ -22,59 +21,23 @@ import java.io.StreamCorruptedException;
/** A clause in a BooleanQuery. */
public class BooleanClause implements java.io.Serializable {
- public static final class Occur implements java.io.Serializable {
+ public static final class Occur extends Parameter implements java.io.Serializable {
- private int id;
- private String name;
- private static final int MUST_ID = 0;
- private static final int MUST_NOT_ID = 1;
- private static final int SHOULD_ID = 2;
-
- private Occur() {
+ private Occur(String name) {
// typesafe enum pattern, no public constructor
+ super(name);
}
-
- private Occur(int id, String name) {
- // typesafe enum pattern, no public constructor
- this.id = id;
- this.name = name;
- }
-
- public String toString() {
- return name;
- }
-
+
/** Use this operator for terms that <i>must</i> appear in the matching documents. */
- public static final Occur MUST = new Occur(MUST_ID, "MUST");
+ public static final Occur MUST = new Occur("MUST");
/** Use this operator for terms of which <i>should</i> appear in the
* matching documents. For a BooleanQuery with two <code>SHOULD</code>
* subqueries, at least one of the queries must appear in the matching documents. */
- public static final Occur SHOULD = new Occur(SHOULD_ID, "SHOULD");
+ public static final Occur SHOULD = new Occur("SHOULD");
/** Use this operator for terms that <i>must not</i> appear in the matching documents.
* Note that it is not possible to search for queries that only consist
* of a <code>MUST_NOT</code> query. */
- public static final Occur MUST_NOT = new Occur(MUST_NOT_ID, "MUST_NOT");
-
- /**
- * Resolves the deserialized instance to the local reference for accurate
- * equals() and == comparisons.
- *
- * @return a reference to Occur as resolved in the local VM
- * @throws ObjectStreamException
- */
- private Object readResolve() throws ObjectStreamException {
- int id = ((Occur) this).id;
- switch (id) {
- case MUST_ID :
- return Occur.MUST;
- case MUST_NOT_ID :
- return Occur.MUST_NOT;
- case SHOULD_ID:
- return Occur.SHOULD;
- default :
- throw new StreamCorruptedException("Unknown id " + id);
- }
- }
+ public static final Occur MUST_NOT = new Occur("MUST_NOT");
}
diff --git a/src/java/org/apache/lucene/util/Parameter.java b/src/java/org/apache/lucene/util/Parameter.java
new file mode 100644
index 00000000000..5778423c584
--- /dev/null
+++ b/src/java/org/apache/lucene/util/Parameter.java
@@ -0,0 +1,73 @@
+package org.apache.lucene.util;
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.io.StreamCorruptedException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A serializable Enum class.
+ */
+public abstract class Parameter implements Serializable
+{
+ static Map allParameters = new HashMap();
+
+ private String name;
+
+ private Parameter() {
+ // typesafe enum pattern, no public constructor
+ }
+
+ protected Parameter(String name) {
+ // typesafe enum pattern, no public constructor
+ this.name = name;
+ String key = makeKey(name);
+
+ if(allParameters.containsKey(key))
+ throw new IllegalArgumentException("Parameter name " + key + " already used!");
+
+ allParameters.put(key, this);
+ }
+
+ private String makeKey(String name){
+ return getClass() + " " + name;
+ }
+
+ public String toString() {
+ return name;
+ }
+
+ /**
+ * Resolves the deserialized instance to the local reference for accurate
+ * equals() and == comparisons.
+ *
+ * @return a reference to Parameter as resolved in the local VM
+ * @throws ObjectStreamException
+ */
+ private Object readResolve() throws ObjectStreamException {
+ Object par = allParameters.get(makeKey(name));
+
+ if(par == null)
+ throw new StreamCorruptedException("Unknown parameter value: " + name);
+
+ return par;
+ }
+
+ }