/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada Inc.
 * Copyright (C) 2017 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.input;

import com.jaamsim.basicsim.Entity;
import com.jaamsim.datatypes.DoubleVector;
import com.jaamsim.units.DimensionlessUnit;
import com.jaamsim.units.Unit;

public class ValueListInput extends ListInput<DoubleVector> {
	private Class<? extends Unit> unitType = DimensionlessUnit.class;
	private double minValue = Double.NEGATIVE_INFINITY;
	private double maxValue = Double.POSITIVE_INFINITY;
	private double sumValue = Double.NaN;
	private double sumTolerance = 1e-10d;
	private int[] validCounts = null; // valid list sizes not including units
	private int monotonic = 0;  // -1 = monotonically decreasing, +1 = monotonically increasing

	public ValueListInput(String key, String cat, DoubleVector def) {
		super(key, cat, def);
	}

	public void setUnitType(Class<? extends Unit> units) {
		if (units != unitType)
			this.reset();
		unitType = units;
	}

	@Override
	public void parse(Entity thisEnt, KeywordIndex kw)
	throws InputErrorException {
		DoubleVector temp = Input.parseDoubles(thisEnt.getJaamSimModel(), kw, minValue, maxValue, unitType);
		Input.assertCount(temp, validCounts);
		Input.assertCountRange(temp, minCount, maxCount);
		Input.assertMonotonic(temp, monotonic);
		if (!Double.isNaN(sumValue))
			Input.assertSumTolerance(temp, sumValue, sumTolerance);

		value = temp;
	}

	@Override
	public String getValidInputDesc() {
		if (unitType == DimensionlessUnit.class) {
			return Input.VALID_VALUE_LIST_DIMLESS;
		}
		return Input.VALID_VALUE_LIST;
	}

	@Override
	public int getListSize() {
		if (value == null)
			return 0;
		else
			return value.size();
	}

	public void setValidRange(double min, double max) {
		minValue = min;
		maxValue = max;
	}

	public void setValidSum(double sum, double tol) {
		sumValue = sum;
		sumTolerance = tol;
	}

	public void setValidCounts(int... list) {
		validCounts = list;
	}

	public void setMonotonic(int dir) {
		monotonic = dir;
	}

	@Override
	public String getDefaultString() {
		if (defValue == null || defValue.size() == 0)
			return "";

		StringBuilder tmp = new StringBuilder();
		for (int i = 0; i < defValue.size(); i++) {
			if (i > 0)
				tmp.append(SEPARATOR);
			tmp.append(defValue.get(i)/Unit.getDisplayedUnitFactor(unitType));
		}

		if (unitType != Unit.class) {
			tmp.append(SEPARATOR);
			tmp.append(Unit.getDisplayedUnit(unitType));
		}

		return tmp.toString();
	}
}

@@ -108,16 +108,21 @@
 (C) 2017
+-2019
  JaamSim
@@ -136,16 +136,16 @@
 re Inc.%0A
-
  *%0A * Li
@@ -1194,16 +1194,118 @@
 1e-10d;%0A
+%09private double sumMin = Double.NEGATIVE_INFINITY;%0A%09private double sumMax = Double.POSITIVE_INFINITY;%0A
 %09private
@@ -2101,16 +2101,62 @@
 erance);
+%0A%09%09Input.assertSumRange(temp, sumMin, sumMax);
 %0A%0A%09%09valu
@@ -2615,16 +2615,16 @@
  = sum;%0A
-
 %09%09sumTol
@@ -2637,24 +2637,116 @@
  = tol;%0A%09%7D%0A%0A
+%09public void setValidSumRange(double min, double max) %7B%0A%09%09sumMin = min;%0A%09%09sumMax = max;%0A%09%7D%0A%0A
 %09public void

add method 'setValidSumRange' to ValueListInput