    001package com.hammurapi.extract;
    002
    003import java.util.concurrent.TimeUnit;
    004
    005
    006/**
    007 * Numeric add.
    008 * @author Pavel Vlasov
    009 *
    010 * @param <T>
    011 * @param <C>
    012 */
    013public class Add<T, C> extends ArithmeticExtractor<T, Object, C> {
    014
    015        public Add(double initialCost, TimeUnit costUnit, Extractor<T, Object, C> leftExtractor, Extractor<T, Object, C> rightExtractor) {
    016                super(initialCost, costUnit, leftExtractor, rightExtractor, false);
    017        }
    018
    019        @Override
    020        protected Object doOp(Object o1, Object o2) {
    021                if (o1 instanceof String || o2 instanceof String) {
    022                        return ""+o1+o2;
    023                }
    024                throw new IllegalArgumentException("Can't add "+o1+" and "+o2);
    025        }
    026
    027        @Override
    028        protected Object doOp(int n1, int n2) {
    029                return n1+n2;
    030        }
    031
    032        @Override
    033        protected Object doOp(long n1, long n2) {
    034                return n1+n2;
    035        }
    036
    037        @Override
    038        protected Object doOp(float n1, float n2) {
    039                return n1+n2;
    040        }
    041
    042        @Override
    043        protected Object doOp(double n1, double n2) {
    044                return n1+n2;
    045        }
    046
    047        @Override
    048        protected Extractor<T, Object, C> newInstance(Extractor<T, Object, C> leftExtractor, Extractor<T, Object, C> rightExtractor) {
    049                return new Add<T,C>(initialCost, costUnit, leftExtractor, rightExtractor);
    050        }
    051}