001: package it.unimi.dsi.mg4j.index.payload;
002:
003: import it.unimi.dsi.bits.Fast;
004: import it.unimi.dsi.fastutil.HashCommon;
005: import it.unimi.dsi.io.InputBitStream;
006: import it.unimi.dsi.io.OutputBitStream;
007:
008: import java.io.IOException;
009:
010: /*
011: * MG4J: Managing Gigabytes for Java
012: *
013: * Copyright (C) 2007 Paolo Boldi and Sebastiano Vigna
014: *
015: * This library is free software; you can redistribute it and/or modify it
016: * under the terms of the GNU Lesser General Public License as published by the Free
017: * Software Foundation; either version 2.1 of the License, or (at your option)
018: * any later version.
019: *
020: * This library is distributed in the hope that it will be useful, but
021: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
022: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
023: * for more details.
024: *
025: * You should have received a copy of the GNU Lesser General Public License
026: * along with this program; if not, write to the Free Software
027: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
028: *
029: */
030:
031: /** A payload containing a long stored using δ coding.
032: *
033: * <p>The method {@link #set(Object)} will accept any {@link Number}
034: * that is in the range allowed by {@link Fast#int2nat(long)}.
035: */
036:
037: public class IntegerPayload extends AbstractPayload {
038: private static final long serialVersionUID = 1L;
039: /** Whether this payload has been ever set. */
040: protected boolean unset = true;
041: /** The current value of this payload, if {@link #unset} is false. */
042: protected long value;
043:
044: public Long get() {
045: if (unset)
046: throw new IllegalStateException();
047: return Long.valueOf(value);
048: }
049:
050: public long getLong() {
051: return value;
052: }
053:
054: /** Sets the current value of this payload.
055: *
056: * @param value the new value of this payload (must be
057: * in the range allowed by {@link Fast#int2nat(long)}).
058: */
059: public void set(final long value) {
060: // Range check for Fast.int2nat()
061: if (value < -(1 << 62) || value >= (1 << 62))
062: throw new IllegalArgumentException(Long.toString(value));
063: unset = false;
064: this .value = value;
065: }
066:
067: public void set(final Object value) {
068: set(((Number) value).longValue());
069: }
070:
071: public int read(final InputBitStream ibs) throws IOException {
072: final long readBits = ibs.readBits();
073: value = (int) Fast.nat2int(ibs.readLongDelta());
074: unset = false;
075: return (int) (ibs.readBits() - readBits);
076: }
077:
078: public int write(final OutputBitStream obs) throws IOException {
079: if (unset)
080: throw new IllegalStateException();
081: unset = false;
082: return obs.writeLongDelta(Fast.int2nat(value));
083: }
084:
085: public IntegerPayload copy() {
086: final IntegerPayload copy = new IntegerPayload();
087: copy.value = value;
088: return copy;
089: }
090:
091: public String toString() {
092: if (!unset)
093: return get().toString();
094: return "undefined";
095: }
096:
097: public boolean compatibleWith(final Payload payload) {
098: return payload.getClass() == IntegerPayload.class;
099: }
100:
101: public int compareTo(final Payload o) {
102: final long diff = value - ((IntegerPayload) o).value;
103: return diff == 0 ? 0 : diff < 0 ? -1 : 1;
104: }
105:
106: public boolean equals(final Payload o) {
107: return ((o instanceof IntegerPayload) && ((IntegerPayload) o).value == value);
108: }
109:
110: public int hashCode() {
111: return HashCommon.long2int(value);
112: }
113:
114: public Long parse(final String spec) {
115: return Long.valueOf(spec);
116: }
117:
118: }
|