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

jasima.core.random.discrete.IntConst Maven / Gradle / Ivy

/*******************************************************************************
 * This file is part of jasima, v1.3, the Java simulator for manufacturing and 
 * logistics.
 *  
 * Copyright (c) 2015 		jasima solutions UG
 * Copyright (c) 2010-2015 Torsten Hildebrandt and jasima contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 *******************************************************************************/
package jasima.core.random.discrete;

import jasima.core.util.Pair;
import jasima.core.util.Util;

import java.util.Arrays;

/**
 * Returns a constant set of integer numbers, as passed to the constructor or
 * via {@link #setValues(int...)}. This value sequence is repeated if all values
 * are returned once. The sequence returned is not random at all, i.e. this
 * class does not use the inherited rndGen.
 * 
 * @author Torsten Hildebrandt
 * @version 
 *          "$Id: IntConst.java 753 2015-07-27 15:29:49Z [email protected] $"
 */
public class IntConst extends IntStream {

	private static final long serialVersionUID = -3297743869123820992L;

	private int[] values;
	private Double mean;

	private int next;

	public IntConst() {
		this(null);
	}

	public IntConst(int... vs) {
		super();
		setValues(vs);
	}

	@Override
	public int nextInt() {
		int v = values[next];
		if (++next == values.length)
			next = 0;
		return v;
	}

	public int[] getValues() {
		return values;
	}

	public void setValues(int... vs) {
		this.values = vs;
		this.mean = null;
	}

	@Override
	public double getNumericalMean() {
		// lazy initialization of "mean" upon first call
		if (mean == null) {
			if (values == null || values.length == 0)
				mean = Double.NaN;
			else
				mean = ((double) Util.sum(values)) / values.length;
		}

		return mean;
	}

	@Override
	public String toString() {
		return "IntConst" + Arrays.toString(values);
	}

	@Override
	public IntConst clone() throws CloneNotSupportedException {
		IntConst c = (IntConst) super.clone();

		if (values != null)
			c.values = values.clone();

		return c;
	}

	@Override
	public Pair getValueRange() {
		if (values == null || values.length == 0)
			return new Pair<>(Double.NaN, Double.NaN);

		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;

		for (int d : values) {
			if (d < min)
				min = d;
			if (d > max)
				max = d;
		}

		return new Pair<>((double) min, (double) max);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy