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

jfxtras.labs.scene.control.ListSpinnerBigIntegerList Maven / Gradle / Ivy

There is a newer version: 9.0-r1
Show newest version
package jfxtras.labs.scene.control;

import java.math.BigInteger;

/**
 * Items for Spinner providing an BigInteger range without actually creating a list with all values.
 * Beware: because this is still based on the list inteface, the maximum size of the list is limited by the type used for the index in the list: an integer (Integer.MAX_VALUE).
 * So the difference between the from and to values (to-from) cannot be larger than Integer.MAX_VALUE.
 * What this class allows is that this range can be anywhere in the BigInteger's range. 
 */
public class ListSpinnerBigIntegerList extends java.util.AbstractList
{
	/**
	 * 
	 */
	public ListSpinnerBigIntegerList()
	{
		this( BigInteger.valueOf(Integer.MIN_VALUE / 2).add(BigInteger.ONE), BigInteger.valueOf(Integer.MAX_VALUE / 2), BigInteger.ONE);
	}
	
	/**
	 * 
	 * @param from
	 * @param to
	 */
	public ListSpinnerBigIntegerList(BigInteger from, BigInteger to)
	{
		this(from, to, from.compareTo(to) > 0 ? BigInteger.valueOf(-1) : BigInteger.ONE);
	}
	
	/**
	 * 
	 * @param from
	 * @param to
	 * @param step
	 */
	public ListSpinnerBigIntegerList(BigInteger from, BigInteger to, BigInteger step)
	{
		this.from = from;
		this.size = to.subtract(from).divide(step).add(BigInteger.ONE).intValue();
		if (this.size < 0) throw new IllegalArgumentException("This results in a negative size: " + from + ", " + to + "," + step);
		this.step = step;
	}
	private BigInteger from;
	private int size;
	private BigInteger step;
	
	
	// ===============================================================================
	// List interface
	
	@Override
	public BigInteger get(int index)
	{
		if (index < 0) throw new IllegalArgumentException("Index cannot be < 0: " + index);
		BigInteger lValue = this.from.add(BigInteger.valueOf(index).multiply(this.step));
		return lValue;
	}

	@Override
	public int indexOf(Object o)
	{
		// calculate the index
		BigInteger lValue = (BigInteger)o;
		BigInteger lIndexBigInteger = lValue.subtract(this.from).divide(this.step);
		int lIndex = lIndexBigInteger.intValue();
		if (lIndex > size) return -1;
		
		// check if that what is at the index matches with out value
		BigInteger lValueAtIndex = get(lIndex);
		if (o.equals(lValueAtIndex) == false) return -1;
		
		// found it
		return lIndex;
	}
	
	@Override
	public int size()
	{
		return this.size;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy