lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rm...@apache.org
Subject svn commit: r902723 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/analysis/CharArraySet.java src/test/org/apache/lucene/analysis/TestCharArraySet.java
Date Mon, 25 Jan 2010 07:48:25 GMT
Author: rmuir
Date: Mon Jan 25 07:48:25 2010
New Revision: 902723

URL: http://svn.apache.org/viewvc?rev=902723&view=rev
Log:
LUCENE-2195: speedup chararrayset if set is empty

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/analysis/CharArraySet.java
    lucene/java/trunk/src/test/org/apache/lucene/analysis/TestCharArraySet.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=902723&r1=902722&r2=902723&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Mon Jan 25 07:48:25 2010
@@ -172,6 +172,9 @@
 * LUCENE-2188: Add a utility class for tracking deprecated overridden
   methods in non-final subclasses.
   (Uwe Schindler, Robert Muir)
+
+* LUCENE-2195: Speedup CharArraySet if set is empty.
+  (Simon Willnauer via Robert Muir)
    
 Build
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/analysis/CharArraySet.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/analysis/CharArraySet.java?rev=902723&r1=902722&r2=902723&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/analysis/CharArraySet.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/analysis/CharArraySet.java Mon Jan 25 07:48:25
2010
@@ -62,8 +62,7 @@
   private char[][] entries;
   private int count;
   private final boolean ignoreCase;
-  public static final CharArraySet EMPTY_SET = CharArraySet.unmodifiableSet(
-      new CharArraySet(Version.LUCENE_CURRENT, 0, false));
+  public static final CharArraySet EMPTY_SET = new EmptyCharArraySet();
   
   private final CharacterUtils charUtils;
   private final Version matchVersion;
@@ -482,7 +481,7 @@
    * the internal representation of a {@link CharArraySet} to a super
    * constructor and overrides all mutators. 
    */
-  private static final class UnmodifiableCharArraySet extends CharArraySet {
+  private static class UnmodifiableCharArraySet extends CharArraySet {
 
     private UnmodifiableCharArraySet(Version matchVersion, char[][] entries, boolean ignoreCase,
         int count) {
@@ -519,5 +518,37 @@
       throw new UnsupportedOperationException();
     }
   }
+  
+  /**
+   * Empty {@link UnmodifiableCharArraySet} optimized for speed.
+   * Contains checks will always return <code>false</code> or throw
+   * NPE if necessary.
+   */
+  private static final class EmptyCharArraySet extends UnmodifiableCharArraySet {
+
+    private EmptyCharArraySet() {
+      super(Version.LUCENE_CURRENT, new char[0][], false, 0);
+    }
+    
+    @Override
+    public boolean contains(char[] text, int off, int len) {
+      if(text == null)
+        throw new NullPointerException();
+      return false;
+    }
 
+    @Override
+    public boolean contains(CharSequence cs) {
+      if(cs == null)
+        throw new NullPointerException();
+      return false;
+    }
+
+    @Override
+    public boolean contains(Object o) {
+      if(o == null)
+        throw new NullPointerException();
+      return false;
+    }
+  }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/analysis/TestCharArraySet.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/analysis/TestCharArraySet.java?rev=902723&r1=902722&r2=902723&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/analysis/TestCharArraySet.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/analysis/TestCharArraySet.java Mon Jan 25
07:48:25 2010
@@ -448,4 +448,47 @@
       assertFalse(set.contains(string));  
     }
   }
+  
+  /**
+   * Tests a special case of {@link CharArraySet#copy(Version, Set)} where the
+   * set to copy is the {@link CharArraySet#EMPTY_SET}
+   */
+  public void testCopyEmptySet() {
+    assertSame(CharArraySet.EMPTY_SET, 
+        CharArraySet.copy(Version.LUCENE_CURRENT, CharArraySet.EMPTY_SET));
+  }
+
+  /**
+   * Smoketests the static empty set
+   */
+  public void testEmptySet() {
+    assertEquals(0, CharArraySet.EMPTY_SET.size());
+    
+    assertTrue(CharArraySet.EMPTY_SET.isEmpty());
+    for (String stopword : TEST_STOP_WORDS) {
+      assertFalse(CharArraySet.EMPTY_SET.contains(stopword));
+    }
+    assertFalse(CharArraySet.EMPTY_SET.contains((Object) "foo"));
+    assertFalse(CharArraySet.EMPTY_SET.contains((Object) "foo".toCharArray()));
+    assertFalse(CharArraySet.EMPTY_SET.contains("foo".toCharArray(),0,3));
+  }
+  
+  /**
+   * Test for NPE
+   */
+  public void testContainsWithNull() {
+    CharArraySet set = new CharArraySet(Version.LUCENE_CURRENT, 1, true);
+    try {
+      set.contains((char[]) null, 0, 10);
+      fail("null value must raise NPE");
+    } catch (NullPointerException e) {}
+    try {
+      set.contains((CharSequence) null);
+      fail("null value must raise NPE");
+    } catch (NullPointerException e) {}
+    try {
+      set.contains((Object) null);
+      fail("null value must raise NPE");
+    } catch (NullPointerException e) {}
+  }
 }



Mime
View raw message