You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2012/08/20 16:13:18 UTC

svn commit: r1375032 - /lucene/dev/branches/pforcodec_3892/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java

Author: jpountz
Date: Mon Aug 20 14:13:18 2012
New Revision: 1375032

URL: http://svn.apache.org/viewvc?rev=1375032&view=rev
Log:
LUCENE-3892: Add version checks to all PackedInts methods that expect a version number.

Modified:
    lucene/dev/branches/pforcodec_3892/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java

Modified: lucene/dev/branches/pforcodec_3892/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/pforcodec_3892/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java?rev=1375032&r1=1375031&r2=1375032&view=diff
==============================================================================
--- lucene/dev/branches/pforcodec_3892/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java (original)
+++ lucene/dev/branches/pforcodec_3892/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java Mon Aug 20 14:13:18 2012
@@ -65,6 +65,14 @@ public class PackedInts {
   public final static int VERSION_START = 0;
   public final static int VERSION_CURRENT = VERSION_START;
 
+  private static void checkVersion(int version) {
+    if (version < VERSION_START) {
+      throw new IllegalArgumentException("Version is too old, should be at least " + VERSION_START + " (got " + version + ")");
+    } else if (version > VERSION_CURRENT) {
+      throw new IllegalArgumentException("Version is too new, should be at most " + VERSION_CURRENT + " (got " + version + ")");
+    }
+  }
+
   /**
    * A format to write packed ints.
    *
@@ -675,9 +683,7 @@ public class PackedInts {
    * @return a decoder
    */
   public static Decoder getDecoder(Format format, int version, int bitsPerValue) {
-    if (version != VERSION_START) {
-      throw new IllegalArgumentException("only VERSION_START is valid (got " + version + ")");
-    }
+    checkVersion(version);
     return BulkOperation.of(format, bitsPerValue);
   }
 
@@ -690,9 +696,7 @@ public class PackedInts {
    * @return an encoder
    */
   public static Encoder getEncoder(Format format, int version, int bitsPerValue) {
-    if (version != VERSION_START) {
-      throw new IllegalArgumentException("only VERSION_START is valid (got " + version + ")");
-    }
+    checkVersion(version);
     return BulkOperation.of(format, bitsPerValue);
   }
 
@@ -714,6 +718,7 @@ public class PackedInts {
    */
   public static Reader getReaderNoHeader(DataInput in, Format format, int version,
       int valueCount, int bitsPerValue) throws IOException {
+    checkVersion(version);
     switch (format) {
       case PACKED_SINGLE_BLOCK:
         return Packed64SingleBlock.create(in, valueCount, bitsPerValue);
@@ -781,6 +786,7 @@ public class PackedInts {
    */
   public static ReaderIterator getReaderIteratorNoHeader(DataInput in, Format format, int version,
       int valueCount, int bitsPerValue, int mem) {
+    checkVersion(version);
     return new PackedReaderIterator(format, valueCount, bitsPerValue, in, mem);
   }
 
@@ -821,6 +827,7 @@ public class PackedInts {
    */
   public static Reader getDirectReaderNoHeader(IndexInput in, Format format,
       int version, int valueCount, int bitsPerValue) {
+    checkVersion(version);
     switch (format) {
       case PACKED:
         return new DirectPackedReader(bitsPerValue, valueCount, in);