lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject svn commit: r908457 - in /lucene/java/branches/flex_1458/src: java/org/apache/lucene/index/ java/org/apache/lucene/search/ test/org/apache/lucene/search/
Date Wed, 10 Feb 2010 11:45:30 GMT
Author: uschindler
Date: Wed Feb 10 11:45:29 2010
New Revision: 908457

URL: http://svn.apache.org/viewvc?rev=908457&view=rev
Log:
Refactor EmptyTermsEnum to be a singleton under abstract TermsEnum

Removed:
    lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/EmptyTermsEnum.java
Modified:
    lucene/java/branches/flex_1458/src/java/org/apache/lucene/index/TermsEnum.java
    lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/AutomatonQuery.java
    lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/NumericRangeQuery.java
    lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/PrefixQuery.java
    lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/TermRangeQuery.java
    lucene/java/branches/flex_1458/src/test/org/apache/lucene/search/TestAutomatonQuery.java

Modified: lucene/java/branches/flex_1458/src/java/org/apache/lucene/index/TermsEnum.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/flex_1458/src/java/org/apache/lucene/index/TermsEnum.java?rev=908457&r1=908456&r2=908457&view=diff
==============================================================================
--- lucene/java/branches/flex_1458/src/java/org/apache/lucene/index/TermsEnum.java (original)
+++ lucene/java/branches/flex_1458/src/java/org/apache/lucene/index/TermsEnum.java Wed Feb
10 11:45:29 2010
@@ -112,4 +112,40 @@
    *  method many times, so it's best to cache a single
    *  instance & reuse it. */
   public abstract BytesRef.Comparator getComparator() throws IOException;
+
+  /** An empty TermsEnum for quickly returning an empty instance e.g.
+   * in {@link org.apache.lucene.search.MultiTermQuery} */
+  public static final TermsEnum EMPTY = new TermsEnum() {    
+    @Override
+    public SeekStatus seek(BytesRef term) { return SeekStatus.END; }
+    
+    @Override
+    public SeekStatus seek(long ord) { return SeekStatus.END; }
+    
+    @Override
+    public BytesRef term() { return null; }
+
+    @Override
+    public BytesRef.Comparator getComparator() {
+      // return an unused dummy to prevent NPE
+      return BytesRef.getUTF8SortedAsUTF16Comparator();
+    }
+      
+    @Override
+    public int docFreq() { return -1; }
+      
+    @Override
+    public long ord() { return -1; }
+
+    @Override
+    public DocsEnum docs(Bits bits, DocsEnum reuse) { return null; }
+      
+    @Override
+    public DocsAndPositionsEnum docsAndPositions(Bits bits, DocsAndPositionsEnum reuse) {
+      return null;
+    }
+      
+    @Override
+    public BytesRef next() { return null; }
+  };
 }

Modified: lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/AutomatonQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/AutomatonQuery.java?rev=908457&r1=908456&r2=908457&view=diff
==============================================================================
--- lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/AutomatonQuery.java (original)
+++ lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/AutomatonQuery.java Wed
Feb 10 11:45:29 2010
@@ -73,15 +73,14 @@
   protected TermsEnum getTermsEnum(IndexReader reader) throws IOException {
     // matches nothing
     if (BasicOperations.isEmpty(automaton)) {
-      // nocommit -- should we just return null?  singleton?
-      return new EmptyTermsEnum();
+      return TermsEnum.EMPTY;
     }
     
     // matches all possible strings
     if (BasicOperations.isTotal(automaton)) {
       final Terms terms = MultiFields.getTerms(reader, getField());
       // nocommit -- should we just return null?  singleton?
-      return (terms != null) ? terms.iterator() : new EmptyTermsEnum();
+      return (terms != null) ? terms.iterator() : TermsEnum.EMPTY;
     }
     
     // matches a fixed string in singleton representation

Modified: lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/NumericRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/NumericRangeQuery.java?rev=908457&r1=908456&r2=908457&view=diff
==============================================================================
--- lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/NumericRangeQuery.java
(original)
+++ lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/NumericRangeQuery.java
Wed Feb 10 11:45:29 2010
@@ -304,7 +304,7 @@
   protected TermsEnum getTermsEnum(final IndexReader reader) throws IOException {
     // very strange: java.lang.Number itsself is not Comparable, but all subclasses used
here are
     return (min != null && max != null && ((Comparable<T>) min).compareTo(max)
> 0) ?
-      new EmptyTermsEnum() :
+      TermsEnum.EMPTY :
       new NumericRangeTermsEnum(reader);
   }
 

Modified: lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/PrefixQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/PrefixQuery.java?rev=908457&r1=908456&r2=908457&view=diff
==============================================================================
--- lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/PrefixQuery.java (original)
+++ lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/PrefixQuery.java Wed
Feb 10 11:45:29 2010
@@ -53,7 +53,7 @@
   protected TermsEnum getTermsEnum(IndexReader reader) throws IOException {
     if (prefix.text().length() == 0) {
       final Terms terms = MultiFields.getTerms(reader, getField());
-      return (terms != null) ? terms.iterator() : new EmptyTermsEnum();
+      return (terms != null) ? terms.iterator() : TermsEnum.EMPTY;
     }
     return new PrefixTermsEnum(reader, prefix);
   }

Modified: lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/TermRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/TermRangeQuery.java?rev=908457&r1=908456&r2=908457&view=diff
==============================================================================
--- lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/TermRangeQuery.java (original)
+++ lucene/java/branches/flex_1458/src/java/org/apache/lucene/search/TermRangeQuery.java Wed
Feb 10 11:45:29 2010
@@ -138,13 +138,13 @@
   @Override
   protected TermsEnum getTermsEnum(IndexReader reader) throws IOException {
     if (collator == null && lowerTerm != null && upperTerm != null &&
lowerTerm.compareTo(upperTerm) > 0) {
-      return new EmptyTermsEnum();
+      return TermsEnum.EMPTY;
     }
     if ((lowerTerm == null || (collator == null && includeLower && "".equals(lowerTerm)))
&& upperTerm == null) {
       // NOTE: debateably, the caller should never pass in a
       // multi reader...
       final Terms terms = MultiFields.getTerms(reader, field);
-      return (terms != null) ? terms.iterator() : new EmptyTermsEnum();
+      return (terms != null) ? terms.iterator() : TermsEnum.EMPTY;
     }
     return new TermRangeTermsEnum(reader, field,
         lowerTerm, upperTerm, includeLower, includeUpper, collator);

Modified: lucene/java/branches/flex_1458/src/test/org/apache/lucene/search/TestAutomatonQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/flex_1458/src/test/org/apache/lucene/search/TestAutomatonQuery.java?rev=908457&r1=908456&r2=908457&view=diff
==============================================================================
--- lucene/java/branches/flex_1458/src/test/org/apache/lucene/search/TestAutomatonQuery.java
(original)
+++ lucene/java/branches/flex_1458/src/test/org/apache/lucene/search/TestAutomatonQuery.java
Wed Feb 10 11:45:29 2010
@@ -25,6 +25,7 @@
 import org.apache.lucene.document.Field;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermsEnum;
 import org.apache.lucene.store.RAMDirectory;
 import org.apache.lucene.util.LuceneTestCase;
 import org.apache.lucene.util.Version;
@@ -218,7 +219,7 @@
         .makeEmpty());
     // not yet available: assertTrue(aq.getEnum(searcher.getIndexReader())
     // instanceof EmptyTermEnum);
-    assertTrue(aq.getTermsEnum(searcher.getIndexReader()) instanceof EmptyTermsEnum);
+    assertSame(TermsEnum.EMPTY, aq.getTermsEnum(searcher.getIndexReader()));
     assertEquals(0, automatonQueryNrHits(aq));
   }
 }



Mime
View raw message