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

net.sf.jsimpletools.utils.Sequence Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
/*
 * #%L
 * jSimpleTools
 * %%
 * Copyright (C) 2011 - 2015 Eric-Karl Matteau 
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package net.sf.jsimpletools.utils;

import net.sf.jsimpletools.SimpleTestToolsException;

/**
 * A simple sequence for tests. The sequence if thread-safe.
 * 

* To create a sequence, use one of the create() static factory methods (constructor is * private). *

* Every methods share the same sequence and are merely casts of the internal long value. * Exceptions will be thrown if the max value of a particular type is reached. Convenience * methods are provided to get wrapped values, those will always return a new instance of the * wrapper. */ public class Sequence { private long nextVal; private final long step; /** * Creates a sequence generator starting at zero (0) with increments of one (1). */ public static Sequence create() { return new Sequence(0L, 1); } /** * Creates a sequence generator starting at provided value with increments of one (1). * * @param firstValue * The starting value of the sequence. */ public static Sequence createWithFirstValue(long firstValue) { return new Sequence(firstValue, 1); } /** * Creates a sequence generator starting at zero (0) with provided increments. * * @param step * The value to add to the sequence after each calls. */ public static Sequence createWithIncrementsOf(int step) { return new Sequence(0L, step); } /** * Creates a sequence generator with provided starting value and increments. * * @param firstValue * The starting value of the sequence. * @param step * The value to add to the sequence after each calls. */ public static Sequence createWithFirstValueAndIncrements(long firstValue, int step) { return new Sequence(firstValue, step); } private Sequence(long startValue, int step) { nextVal = startValue; this.step = step; } /** * Returns the next value from the sequence. * * @throws SimpleTestToolsException * if the sequence reached Long.MAX_VALUE; */ public synchronized long nextVal() { SequenceValidator.validateLong(nextVal, step); long value = nextVal; nextVal += step; return value; } /** * Returns the next value from the sequence in a Long wrapper object. Wrapper object is * guaranteed a new instance and is equivalent of calling * new Long(sequence.nextLong()); * * @throws SimpleTestToolsException * if the sequence reached Long.MAX_VALUE; */ public Long nextLongObject() { return new Long(nextLong()); } /** * Returns the next value from the sequence. * * @throws SimpleTestToolsException * if the sequence reached Long.MAX_VALUE; */ public long nextLong() { return nextVal(); } /** * Returns the next value from the sequence in a Integer wrapper object. Wrapper object is * guaranteed a new instance and is equivalent of calling * new Integer(sequence.nextInt()); */ public Integer nextIntegerObject() { return new Integer(nextInt()); } /** * Returns the next value from the sequence as an int. * * @throws SimpleTestToolsException * if the sequence reached Integer.MAX_VALUE; */ public int nextInt() { long val = nextVal(); SequenceValidator.validateInteger(val); return (int) val; } /** * Returns the next value from the sequence in a Short wrapper object. Wrapper object is * guaranteed a new instance and is equivalent of calling * new Short(sequence.nextShort()); */ public Short nextShortObject() { return new Short(nextShort()); } /** * Returns the next value from the sequence as a short. * * @throws SimpleTestToolsException * if the sequence reached Short.MAX_VALUE; */ public short nextShort() { long val = nextVal(); SequenceValidator.validateShort(val); return (short) val; } /** * Returns the next value from the sequence in a Byte wrapper object. Wrapper object is * guaranteed a new instance and is equivalent of calling * new Byte(sequence.nextByte()); */ public Byte nextByteObject() { return new Byte(nextByte()); } /** * Returns the next value from the sequence as a byte. * * @throws SimpleTestToolsException * if the sequence reached Byte.MAX_VALUE; */ public byte nextByte() { long val = nextVal(); SequenceValidator.validateByte(val); return (byte) val; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy