package com.hammurapi.extract;

import java.util.concurrent.TimeUnit;


/**
 * Numeric add.
 * @author Pavel Vlasov
 *
 * @param <T>
 * @param <C>
 */
public class Add<T, C> extends ArithmeticExtractor<T, Object, C> {

	public Add(double initialCost, TimeUnit costUnit, Extractor<T, Object, C> leftExtractor, Extractor<T, Object, C> rightExtractor) {
		super(initialCost, costUnit, leftExtractor, rightExtractor, false);
	}

	@Override
	protected Object doOp(Object o1, Object o2) {
		if (o1 instanceof String || o2 instanceof String) {
			return ""+o1+o2;
		}
		throw new IllegalArgumentException("Can't add "+o1+" and "+o2);
	}

	@Override
	protected Object doOp(int n1, int n2) {
		return n1+n2;
	}

	@Override
	protected Object doOp(long n1, long n2) {
		return n1+n2;
	}

	@Override
	protected Object doOp(float n1, float n2) {
		return n1+n2;
	}

	@Override
	protected Object doOp(double n1, double n2) {
		return n1+n2;
	}

	@Override
	protected Extractor<T, Object, C> newInstance(Extractor<T, Object, C> leftExtractor, Extractor<T, Object, C> rightExtractor) {
		return new Add<T,C>(initialCost, costUnit, leftExtractor, rightExtractor);
	}
}
