/*
 * @(#)Sequence.java
 *
 * Copyright (c) 2002-2003 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */
package com.jscape.util;

import java.io.Serializable;


/**
 * The integral objects sequence.
 *
 * @author Alex Usun
 */
public class Sequence
        implements Serializable {

    private Counter counter;

    /**
     * Creates a new counter instance with wrapping.
     *
     * @param lowerBound the counter lower bound
     * @param upperBound the counter upper bound
     */
    public Sequence(long lowerBound, long upperBound) {
        this.counter = new Counter(lowerBound, upperBound, true);
    }

    /**
     * Copy constructor.
     *
     * @param that counter to copy
     */
    public Sequence(Sequence that) {
        this(that.getLowerBound(), that.getUpperBound());
    }

    /**
     * Returns the lower counter bound.
     *
     * @return the lower counter bound
     */
    public long getLowerBound() {
        return this.counter.getLowerBound();
    }

    /**
     * Returns the upper counter bound.
     *
     * @return the upper counter bound
     */
    public long getUpperBound() {
        return this.counter.getUpperBound();
    }

    /**
     * Returns next sequence value.
     *
     * @return next sequence value
     */
    public synchronized long nextValue() {
        long value = this.counter.getValue();
        this.counter.increment();

        return value;
    }

    /**
     * Returns previous sequence value.
     *
     * @return previous sequence value
     */
    public synchronized long previousValue() {
        long value = this.counter.getValue();
        this.counter.decrement();

        return value;
    }

    /**
     * @see Object#equals(Object)
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || !getClass().equals(o.getClass())) {
            return false;
        }

        final Sequence that = (Sequence) o;

        if (!this.counter.equals(that.counter)) {
            return false;
        }

        return true;
    }

    /**
     * @see Object#hashCode()
     */
    public int hashCode() {
        return this.counter.hashCode();
    }

    /**
     * @see Object#toString()
     */
    public String toString() {
        return this.counter.toString();
    }

}
