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

com.github.bloodshura.x.utilities.eval.operation.Operation Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
 *
 * 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 .
 *
 * https://www.github.com/BloodShura
 */

package com.github.bloodshura.x.utilities.eval.operation;

import com.github.bloodshura.x.collection.view.XSnapshotView;
import com.github.bloodshura.x.collection.view.XView;
import com.github.bloodshura.x.object.Base;
import com.github.bloodshura.x.utilities.eval.VariableMap;
import com.github.bloodshura.x.utilities.eval.enumeration.Operator;
import com.github.bloodshura.x.utilities.eval.enumeration.Type;
import com.github.bloodshura.x.utilities.eval.exception.ErrorCode;
import com.github.bloodshura.x.utilities.eval.exception.EvaluationException;
import com.github.bloodshura.x.worker.StringWorker;

import javax.annotation.Nonnull;
import java.math.BigDecimal;

public class Operation extends Base {
	private final XView operands;
	private final Operator operator;
	private final Type type;

	public Operation(@Nonnull Operator operator, @Nonnull Object... operands) {
		this(operator, operator.getResultType(), operands);
	}

	public Operation(@Nonnull Operator operator, @Nonnull Type type, @Nonnull Object... operands) {
		this.operands = new XSnapshotView<>(operands);
		this.operator = operator;
		this.type = type;
	}

	@Nonnull
	public BigDecimal evaluate(@Nonnull VariableMap map) throws EvaluationException {
		return getOperator().perform(getOperands().map(operand -> evaluateOperand(operand, map)).toArray());
	}

	@Nonnull
	public XView getOperands() {
		return operands;
	}

	@Nonnull
	public Operator getOperator() {
		return operator;
	}

	@Nonnull
	public Type getType() {
		return type;
	}

	@Nonnull
	@Override
	public String toString() {
		return getOperator().getIdentifier() + ' ' + StringWorker.toString(getOperands());
	}

	@Nonnull
	@Override
	protected Object[] stringValues() {
		return new Object[] { getType(), getOperator(), getOperands() };
	}

	@Nonnull
	protected static BigDecimal evaluateOperand(@Nonnull Object operand, @Nonnull VariableMap map) throws EvaluationException {
		if (operand instanceof BigDecimal) {
			return (BigDecimal) operand;
		}

		if (operand instanceof Operation) {
			Operation op = (Operation) operand;

			return op.evaluate(map);
		}

		if (operand instanceof String) {
			String string = (String) operand;

			if (map != null && map.containsKey(string)) {
				return map.get(string);
			}
		}

		throw new EvaluationException(ErrorCode.UNKNOWN_VALUE, operand, operand.getClass().getName());
	}
}