kevinross 2003/07/15 08:19:22
Modified: java/src/org/apache/xindice/core/data Value.java Log: format, use XindiceRuntimeException to retain root cause instead of IllegalStateException Revision Changes Path 1.4 +159 -161 xml-xindice/java/src/org/apache/xindice/core/data/Value.java Index: Value.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/data/Value.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- Value.java 31 Oct 2002 06:59:56 -0000 1.3 +++ Value.java 15 Jul 2003 15:19:22 -0000 1.4 @@ -65,6 +65,8 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import org.apache.xindice.util.XindiceRuntimeException; + /** * Value is the primary base class for all data storing objects. * The content window of Value objects are immutable, but the @@ -72,165 +74,161 @@ */ public class Value implements Comparable { - protected byte[] data = null; - protected int pos = 0; - protected int len = -1; - - private Value() { - } - - public Value(Value value) { - data = value.data; - pos = value.pos; - len = value.len; - } - - public Value(byte[] data) { - this.data = data; - len = data.length; - } - - public Value(byte[] data, int pos, int len) { - this.data = data; - this.pos = pos; - this.len = len; - } - - public Value(String data) { - - try { - - this.data = data.getBytes("utf-8"); - this.len = this.data.length; - - } catch (UnsupportedEncodingException e) { - - throw new IllegalStateException("Java doesn't support " - + "UTF-8 encoding"); - } - } - - /** - * getData retrieves the data being stored by the Value as a byte array. - * - * @return The Data - */ - public final byte[] getData() { - if ( len != data.length ) { - byte[] b = new byte[len]; - System.arraycopy(data, pos, b, 0, len); - return b; - } - else - return data; - } - - /** - * getLength retrieves the length of the data being stored by the Value. - * - * @return The Value length - */ - public final int getLength() { - return len; - } - - /** - * getInputStream returns an InputStream for the Value. - * - * @return An InputStream - */ - public final InputStream getInputStream() { - return new ByteArrayInputStream(data, pos, len); - } - - /** - * streamTo streams the content of the Value to an OutputStream. - * - * @param out the OutputStream - */ - public final void streamTo(OutputStream out) throws IOException { - out.write(data, pos, len); - } - - public final void copyTo(byte[] tdata, int tpos) { - System.arraycopy(data, pos, tdata, tpos, len); - } - - public final String toString() { - try { - - return new String(getData(), "utf-8"); - } catch (UnsupportedEncodingException e) { - - throw new IllegalStateException("Java doesn't seem to support UTF-8!"); - } - } - - public int hashCode() { - return toString().hashCode(); - } - - public boolean equals(Value value) { - return len == value.len ? compareTo(value) == 0 - : false; - } - - public boolean equals(Object obj) { - if ( this == obj ) - return true; - if ( obj instanceof Value ) - return equals((Value)obj); - else - return equals(new Value(obj.toString())); - } - - public final int compareTo(Value value) { - byte[] ddata = value.data; - int dpos = value.pos; - int dlen = value.len; - - int stop = len > dlen ? dlen - : len; - - for ( int i = 0; i < stop; i++ ) { - byte b1 = data[pos+i]; - byte b2 = ddata[dpos+i]; - - if ( b1 == b2 ) - continue; - else { - short s1 = (short)(b1 >>> 0); - short s2 = (short)(b2 >>> 0); - return s1 > s2 ? (i+1) - : -(i+1); - } - } - - if ( len == dlen ) - return 0; - else - return len > dlen ? stop+1 - : -(stop+1); - } - - public final int compareTo(Object obj) { - if ( obj instanceof Value ) - return compareTo((Value)obj); - else - return compareTo(new Value(obj.toString())); - } - - public final boolean startsWith(Value value) { - if ( len < value.len ) - return false; - - byte[] ddata = value.data; - int dpos = value.pos; - - for ( int i = 0; i < value.len; i++ ) - if ( data[i+pos] != ddata[i+dpos] ) + protected byte[] data = null; + protected int pos = 0; + protected int len = -1; + + private Value() {} + + public Value(Value value) { + data = value.data; + pos = value.pos; + len = value.len; + } + + public Value(byte[] data) { + this.data = data; + len = data.length; + } + + public Value(byte[] data, int pos, int len) { + this.data = data; + this.pos = pos; + this.len = len; + } + + public Value(String data) { + + try { + + this.data = data.getBytes("utf-8"); + this.len = this.data.length; + + } + catch (UnsupportedEncodingException e) { + + throw new XindiceRuntimeException("Java doesn't support UTF-8 encoding", e); + } + } + + /** + * getData retrieves the data being stored by the Value as a byte array. + * + * @return The Data + */ + public final byte[] getData() { + if (len != data.length) { + byte[] b = new byte[len]; + System.arraycopy(data, pos, b, 0, len); + return b; + } + else + return data; + } + + /** + * getLength retrieves the length of the data being stored by the Value. + * + * @return The Value length + */ + public final int getLength() { + return len; + } + + /** + * getInputStream returns an InputStream for the Value. + * + * @return An InputStream + */ + public final InputStream getInputStream() { + return new ByteArrayInputStream(data, pos, len); + } + + /** + * streamTo streams the content of the Value to an OutputStream. + * + * @param out the OutputStream + */ + public final void streamTo(OutputStream out) throws IOException { + out.write(data, pos, len); + } + + public final void copyTo(byte[] tdata, int tpos) { + System.arraycopy(data, pos, tdata, tpos, len); + } + + public final String toString() { + try { + + return new String(getData(), "utf-8"); + } + catch (UnsupportedEncodingException e) { + + throw new XindiceRuntimeException("Java doesn't seem to support UTF-8!", e); + } + } + + public int hashCode() { + return toString().hashCode(); + } + + public boolean equals(Value value) { + return len == value.len ? compareTo(value) == 0 : false; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj instanceof Value) + return equals((Value) obj); + else + return equals(new Value(obj.toString())); + } + + public final int compareTo(Value value) { + byte[] ddata = value.data; + int dpos = value.pos; + int dlen = value.len; + + int stop = len > dlen ? dlen : len; + + for (int i = 0; i < stop; i++) { + byte b1 = data[pos + i]; + byte b2 = ddata[dpos + i]; + + if (b1 == b2) + continue; + else { + short s1 = (short) (b1 >>> 0); + short s2 = (short) (b2 >>> 0); + return s1 > s2 ? (i + 1) : - (i + 1); + } + } + + if (len == dlen) + return 0; + else + return len > dlen ? stop + 1 : - (stop + 1); + } + + public final int compareTo(Object obj) { + if (obj instanceof Value) + return compareTo((Value) obj); + else + return compareTo(new Value(obj.toString())); + } + + public final boolean startsWith(Value value) { + if (len < value.len) return false; - - return true; - } + + byte[] ddata = value.data; + int dpos = value.pos; + + for (int i = 0; i < value.len; i++) + if (data[i + pos] != ddata[i + dpos]) + return false; + + return true; + } }