/** * Serene (simple) - A PoC lisp to collect data on Serenes concepts * Copyright (C) 2019-2020 Sameer Rahmani * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ package serene.simple; import serene.simple.SereneException; public class SNumber { public static interface IOps { public IOps add(Object x); public IOps add(Double x); public IOps add(Long x); public IOps subtract(Object x); public IOps subtract(Double x); public IOps subtract(Long x); public Object value(); } public static class LongNumber implements IOps { private long v; public LongNumber() { this.v = 0; } public LongNumber(long v) { this.v = v; } public IOps add(Object x) throws SereneException { if (x instanceof Long) { return this.add((long) x); } else if (x instanceof Double) { return this.add((Double) x); } throw new SereneException("Can't cast anything beside Long and Double"); } public IOps add(Long x) { return new LongNumber(this.v + x); } public IOps add(Double x) { DoubleNumber y = new DoubleNumber(x); return y.add(this.v); } public IOps subtract(Object x) throws SereneException { if (x instanceof Long) { return this.subtract((long) x); } else if (x instanceof Double) { return this.subtract((Double) x); } throw new SereneException("Can't cast anything beside Long and Double"); } public IOps subtract(Long x) { return new LongNumber(this.v - x); } public IOps subtract(Double x) { DoubleNumber y = new DoubleNumber(x); return y.subtract(this.v); } public Object value() { return this.v; } } public static class DoubleNumber implements IOps { private Double v; public DoubleNumber(Double v) { this.v = v; } public IOps add(Object x) throws SereneException { if (x instanceof Long) { return this.add((long) x); } else if (x instanceof Double) { return this.add((Double) x); } throw new SereneException("Can't cast anything beside Long and Double"); } public IOps add(Double x) { return new DoubleNumber(this.v + x); } public IOps add(Long x) { return new DoubleNumber(this.v + x.doubleValue()); } public IOps subtract(Object x) throws SereneException { if (x instanceof Long) { return this.subtract((long) x); } else if (x instanceof Double) { return this.subtract((Double) x); } throw new SereneException("Can't cast anything beside Long and Double"); } public IOps subtract(Double x) { return new DoubleNumber(this.v - x); } public IOps subtract(Long x) { return new DoubleNumber(this.v - x.doubleValue()); } public Object value() { return this.v; } } public static IOps createNumber(Object num) { if (num instanceof Long) { return new SNumber.LongNumber((long) num); } else if (num instanceof Double) { return new SNumber.DoubleNumber((Double) num); } throw new SereneException("Can't cast anything beside Long and Double"); } }