All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jscsi.utils.SerialArithmeticNumber Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2012, University of Konstanz, Distributed Systems Group All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or other materials provided with the
 * distribution. * Neither the name of the University of Konstanz nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.jscsi.utils;

/**
 * 

SerialArithmeticNumber

*

* This class encapsulate the behavior of how to compare and increment a number in Serial Number Arithmetic, which is * defined in [RFC1982]. * * @author Volker Wildi, University of Konstanz */ public final class SerialArithmeticNumber implements Comparable { // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- /** * This is the wrap around divisor (2**32) of the modulo operation used by incrementing the sequence numbers. See * [RFC1982] for details. */ private static final long MAXIMUM_VALUE = 0xFFFFFFFFL; // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- /** The current value. */ private long serialNumber; // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- /** * Default constructor to create a new, empty SerialArithmeticNumber instance, which is initialized to * 0. */ public SerialArithmeticNumber () { } /** * Constructor to create a new SerialArithmeticNumber instance, which is initialized to * startValue. * * @param startValue The start value. */ public SerialArithmeticNumber (final int startValue) { if (startValue < 0 || startValue > MAXIMUM_VALUE) { throw new IllegalArgumentException(startValue + " is out of range."); } serialNumber = startValue; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- /** * Same as the compareTo method only with the exception of another parameter type. * * @param anotherSerialNumber The number to compare with. * @return a negative integer, zero, or a positive integer as this SerialArithmeticNumber is less than, * equal to, or greater than the given number. */ public final int compareTo (final int anotherSerialNumber) { return compareTo(new Integer(anotherSerialNumber)); } /** {@inheritDoc} */ public final synchronized int compareTo (final Integer anotherSerialNumber) { long diff = serialNumber - Utils.getUnsignedLong(anotherSerialNumber.intValue()); if (diff >= MAXIMUM_VALUE) { diff -= (MAXIMUM_VALUE + 1); } else if (diff < -MAXIMUM_VALUE) { diff += (MAXIMUM_VALUE + 1); } return (int) diff; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- /** * Returns the current value of this SerialArithmeticNumber instance. * * @return The current value of this SerialArithmeticNumber instance. */ public final synchronized int getValue () { return (int) serialNumber; } /** * Increments the value of this SerialArithmeticNumber instance. */ public final synchronized void increment () { if (serialNumber == MAXIMUM_VALUE) { serialNumber = 0; } else { serialNumber++; } } /** * Updates the value of this SerialArithmeticNumber instance to the given one. * * @param newValue The new value. */ public final synchronized void setValue (final int newValue) { if (newValue < 0 || newValue > MAXIMUM_VALUE) { throw new IllegalArgumentException(newValue + " is out of range."); } serialNumber = newValue; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy