001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: IntList.java,v 1.3 2004/09/22 14:35:05 jesper Exp $
023: package net.infonode.util;
024:
025: import java.io.IOException;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028:
029: /**
030: * A single linked list of positive int's.
031: *
032: * @author $Author: jesper $
033: * @version $Revision: 1.3 $
034: */
035: public class IntList {
036: /**
037: * The empty list.
038: */
039: public static final IntList EMPTY_LIST = new IntList(-1, null);
040:
041: private int value;
042: private IntList next;
043:
044: /**
045: * Constructor.
046: *
047: * @param value the int value
048: * @param next the next list element
049: */
050: public IntList(int value, IntList next) {
051: this .value = value;
052: this .next = next;
053: }
054:
055: /**
056: * @return
057: */
058: public int getValue() {
059: return value;
060: }
061:
062: public IntList getNext() {
063: return next;
064: }
065:
066: public boolean isEmpty() {
067: return this == EMPTY_LIST;
068: }
069:
070: public boolean equals(Object object) {
071: return object instanceof IntList && equals((IntList) object);
072: }
073:
074: public boolean equals(IntList list) {
075: return value == list.value
076: && (next == null ? list.next == null : next
077: .equals(list.next));
078: }
079:
080: public int hashCode() {
081: int result = 17;
082: result = 37 * result + value;
083:
084: if (next != null)
085: result = 37 * result + next.hashCode();
086:
087: return result;
088: }
089:
090: public void write(ObjectOutputStream out) throws IOException {
091: out.writeInt(value);
092:
093: if (next != null)
094: next.write(out);
095: }
096:
097: public static IntList decode(ObjectInputStream in)
098: throws IOException {
099: int i = in.readInt();
100: return i == -1 ? EMPTY_LIST : new IntList(i, decode(in));
101: }
102:
103: public String toString() {
104: return value + (next == null ? "" : ", " + next.toString());
105: }
106:
107: }
|