Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

extend ctor checks to other Ref classes

Robert Muird70a0e040622
diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java b/lucene/core/src/java/org/apache/lucene/util/BytesRef.java
index 6cdcae29cdc..a3eddabf4ef 100644
--- a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java
+++ b/lucene/core/src/java/org/apache/lucene/util/BytesRef.java
@@ -342,7 +342,6 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {
* Performs internal consistency checks.
* Always returns true (or throws IllegalStateException)
*/
- // TODO: also for the other *Ref classes
public boolean isValid() {
if (bytes == null) {
throw new IllegalStateException("bytes is null");
@@ -351,13 +350,13 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {
throw new IllegalStateException("length is negative: " + length);
}
if (length > bytes.length) {
- throw new IllegalStateException("length is out of bounds: " + length + ", bytes.length=" + bytes.length);
+ throw new IllegalStateException("length is out of bounds: " + length + ",bytes.length=" + bytes.length);
}
if (offset < 0) {
throw new IllegalStateException("offset is negative: " + offset);
}
if (offset > bytes.length) {
- throw new IllegalStateException("offset out of bounds: " + offset + ", length=" + bytes.length);
+ throw new IllegalStateException("offset out of bounds: " + offset + ",bytes.length=" + bytes.length);
}
if (offset + length < 0) {
throw new IllegalStateException("offset+length is negative: offset=" + offset + ",length=" + length);
diff --git a/lucene/core/src/java/org/apache/lucene/util/CharsRef.java b/lucene/core/src/java/org/apache/lucene/util/CharsRef.java
index 30e95439c76..a57e73e0045 100644
--- a/lucene/core/src/java/org/apache/lucene/util/CharsRef.java
+++ b/lucene/core/src/java/org/apache/lucene/util/CharsRef.java
@@ -55,13 +55,10 @@ public final class CharsRef implements Comparable<CharsRef>, CharSequence, Clone
* length
*/
public CharsRef(char[] chars, int offset, int length) {
- assert chars != null;
- assert offset >= 0;
- assert length >= 0;
- assert chars.length >= offset + length;
this.chars = chars;
this.offset = offset;
this.length = length;
+ assert isValid();
}
/**
@@ -292,4 +289,33 @@ public final class CharsRef implements Comparable<CharsRef>, CharSequence, Clone
clone.copyChars(other);
return clone;
}
+
+ /**
+ * Performs internal consistency checks.
+ * Always returns true (or throws IllegalStateException)
+ */
+ public boolean isValid() {
+ if (chars == null) {
+ throw new IllegalStateException("chars is null");
+ }
+ if (length < 0) {
+ throw new IllegalStateException("length is negative: " + length);
+ }
+ if (length > chars.length) {
+ throw new IllegalStateException("length is out of bounds: " + length + ",chars.length=" + chars.length);
+ }
+ if (offset < 0) {
+ throw new IllegalStateException("offset is negative: " + offset);
+ }
+ if (offset > chars.length) {
+ throw new IllegalStateException("offset out of bounds: " + offset + ",chars.length=" + chars.length);
+ }
+ if (offset + length < 0) {
+ throw new IllegalStateException("offset+length is negative: offset=" + offset + ",length=" + length);
+ }
+ if (offset + length > chars.length) {
+ throw new IllegalStateException("offset+length out of bounds: offset=" + offset + ",length=" + length + ",chars.length=" + chars.length);
+ }
+ return true;
+ }
}
\ No newline at end of file
diff --git a/lucene/core/src/java/org/apache/lucene/util/IntsRef.java b/lucene/core/src/java/org/apache/lucene/util/IntsRef.java
index a657d255a62..5ea2aef1c24 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IntsRef.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IntsRef.java
@@ -50,13 +50,10 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {
* ints should not be null.
*/
public IntsRef(int[] ints, int offset, int length) {
- assert ints != null;
- assert offset >= 0;
- assert length >= 0;
- assert ints.length >= offset + length;
this.ints = ints;
this.offset = offset;
this.length = length;
+ assert isValid();
}
@Override
@@ -176,4 +173,33 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {
clone.copyInts(other);
return clone;
}
+
+ /**
+ * Performs internal consistency checks.
+ * Always returns true (or throws IllegalStateException)
+ */
+ public boolean isValid() {
+ if (ints == null) {
+ throw new IllegalStateException("ints is null");
+ }
+ if (length < 0) {
+ throw new IllegalStateException("length is negative: " + length);
+ }
+ if (length > ints.length) {
+ throw new IllegalStateException("length is out of bounds: " + length + ",ints.length=" + ints.length);
+ }
+ if (offset < 0) {
+ throw new IllegalStateException("offset is negative: " + offset);
+ }
+ if (offset > ints.length) {
+ throw new IllegalStateException("offset out of bounds: " + offset + ",ints.length=" + ints.length);
+ }
+ if (offset + length < 0) {
+ throw new IllegalStateException("offset+length is negative: offset=" + offset + ",length=" + length);
+ }
+ if (offset + length > ints.length) {
+ throw new IllegalStateException("offset+length out of bounds: offset=" + offset + ",length=" + length + ",ints.length=" + ints.length);
+ }
+ return true;
+ }
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/LongsRef.java b/lucene/core/src/java/org/apache/lucene/util/LongsRef.java
index 7236b9072ab..62f15b0eb6c 100644
--- a/lucene/core/src/java/org/apache/lucene/util/LongsRef.java
+++ b/lucene/core/src/java/org/apache/lucene/util/LongsRef.java
@@ -49,13 +49,10 @@ public final class LongsRef implements Comparable<LongsRef>, Cloneable {
/** This instance will directly reference longs w/o making a copy.
* longs should not be null */
public LongsRef(long[] longs, int offset, int length) {
- assert longs != null;
- assert offset >= 0;
- assert length >= 0;
- assert longs.length >= offset + length;
this.longs = longs;
this.offset = offset;
this.length = length;
+ assert isValid();
}
@Override
@@ -175,4 +172,33 @@ public final class LongsRef implements Comparable<LongsRef>, Cloneable {
clone.copyLongs(other);
return clone;
}
+
+ /**
+ * Performs internal consistency checks.
+ * Always returns true (or throws IllegalStateException)
+ */
+ public boolean isValid() {
+ if (longs == null) {
+ throw new IllegalStateException("longs is null");
+ }
+ if (length < 0) {
+ throw new IllegalStateException("length is negative: " + length);
+ }
+ if (length > longs.length) {
+ throw new IllegalStateException("length is out of bounds: " + length + ",longs.length=" + longs.length);
+ }
+ if (offset < 0) {
+ throw new IllegalStateException("offset is negative: " + offset);
+ }
+ if (offset > longs.length) {
+ throw new IllegalStateException("offset out of bounds: " + offset + ",longs.length=" + longs.length);
+ }
+ if (offset + length < 0) {
+ throw new IllegalStateException("offset+length is negative: offset=" + offset + ",length=" + length);
+ }
+ if (offset + length > longs.length) {
+ throw new IllegalStateException("offset+length out of bounds: offset=" + offset + ",length=" + length + ",longs.length=" + longs.length);
+ }
+ return true;
+ }
}