extend ctor checks to other Ref classes

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1439255 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-01-28 05:09:40 +00:00
parent af59f5bf56
commit d70a0e0406
4 changed files with 92 additions and 15 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}