001: package it.unimi.dsi.mg4j.index.payload;
002:
003: import it.unimi.dsi.bits.Fast;
004: import it.unimi.dsi.io.InputBitStream;
005: import it.unimi.dsi.io.OutputBitStream;
006:
007: import java.io.IOException;
008: import java.text.DateFormat;
009: import java.text.ParseException;
010: import java.util.Date;
011: import java.util.Locale;
012:
013: /*
014: * MG4J: Managing Gigabytes for Java
015: *
016: * Copyright (C) 2007 Paolo Boldi and Sebastiano Vigna
017: *
018: * This library is free software; you can redistribute it and/or modify it
019: * under the terms of the GNU Lesser General Public License as published by the Free
020: * Software Foundation; either version 2.1 of the License, or (at your option)
021: * any later version.
022: *
023: * This library is distributed in the hope that it will be useful, but
024: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
025: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
026: * for more details.
027: *
028: * You should have received a copy of the GNU Lesser General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
031: *
032: */
033:
034: /** A payload containing a {@linkplain Date date} expressed as seconds from the Epoch
035: * and stored using δ coding.
036: *
037: * <p>Note that since the date is stored in <em>seconds</em>, it is possible that
038: * setting a current value and getting it back does not given an equal {@link Date} object.
039: */
040:
041: public class DatePayload extends AbstractPayload {
042: private static final long serialVersionUID = 1L;
043: protected long secondsFromEpoch = Long.MAX_VALUE;
044:
045: public Date get() {
046: if (secondsFromEpoch == Long.MAX_VALUE)
047: throw new IllegalStateException();
048: return new Date(secondsFromEpoch * 1000);
049: }
050:
051: public void set(final Object date) {
052: secondsFromEpoch = ((Date) date).getTime() / 1000;
053: }
054:
055: public int read(final InputBitStream ibs) throws IOException {
056: final long readBits = ibs.readBits();
057: secondsFromEpoch = Fast.nat2int(ibs.readLongDelta());
058: return (int) (ibs.readBits() - readBits);
059: }
060:
061: public int write(final OutputBitStream obs) throws IOException {
062: if (secondsFromEpoch == Long.MAX_VALUE)
063: throw new IllegalStateException();
064: return obs.writeLongDelta(Fast.int2nat(secondsFromEpoch));
065: }
066:
067: public DatePayload copy() {
068: final DatePayload copy = new DatePayload();
069: copy.secondsFromEpoch = secondsFromEpoch;
070: return copy;
071: }
072:
073: public String toString() {
074: if (secondsFromEpoch != Long.MAX_VALUE)
075: return get().toString();
076: return "undefined";
077: }
078:
079: public boolean compatibleWith(final Payload payload) {
080: return payload.getClass() == DatePayload.class;
081: }
082:
083: public int compareTo(final Payload o) {
084: final long diff = secondsFromEpoch
085: - ((DatePayload) o).secondsFromEpoch;
086: return diff == 0 ? 0 : diff < 0 ? -1 : 1;
087: }
088:
089: public boolean equals(final Payload o) {
090: return ((o instanceof DatePayload) && ((DatePayload) o).secondsFromEpoch == secondsFromEpoch);
091: }
092:
093: public int hashCode() {
094: return (int) secondsFromEpoch;
095: }
096:
097: public Date parse(final String spec) throws ParseException {
098: return DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK)
099: .parse(spec);
100: }
101: }
|