    001/*-
    002 *******************************************************************************
    003 * Copyright (c) 2016 Diamond Light Source Ltd.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the Eclipse Public License v1.0
    006 * which accompanies this distribution, and is available at
    007 * http://www.eclipse.org/legal/epl-v10.html
    008 *
    009 * Contributors:
    010 *    Peter Chang - initial API and implementation and/or initial documentation
    011 *******************************************************************************/
    012
    013package org.eclipse.january.dataset;
    014
    015/**
    016 * Interface to represent a binary operation for implementations over different output domains
    017 */
    018public interface BinaryOperation extends IOperation {
    019
    020        /**
    021         * @param a
    022         * @param b
    023         * @return a op b
    024         */
    025        boolean booleanOperate(long a, long b);
    026
    027        /**
    028         * @param a
    029         * @param b
    030         * @return a op b
    031         */
    032        long longOperate(long a, long b);
    033
    034        /**
    035         * @param a
    036         * @param b
    037         * @return a op b
    038         */
    039        double doubleOperate(double a, double b);
    040
    041        /**
    042         * @param out holds (ra, ia) op (rb, ib)
    043         * @param ra
    044         * @param ia
    045         * @param rb
    046         * @param ib
    047         */
    048        void complexOperate(double[] out, double ra, double ia, double rb, double ib);
    049
    050        /**
    051         * @param a
    052         * @param b
    053         * @return string to represent output
    054         * @since 2.0
    055         */
    056        String toString(String a, String b);
    057
    058        /**
    059         * Stub class where only three methods need to be overridden:
    060         *  {@link #complexOperate(double[], double, double, double, double)},
    061         *  {@link #toString(String, String)}
    062         */
    063        public static class Stub implements BinaryOperation {
    064                double[] z = new double[2];
    065
    066                @Override
    067                public double doubleOperate(double a, double b) {
    068                        complexOperate(z, a, 0, b, 0);
    069                        return z[0];
    070                }
    071
    072                @Override
    073                public boolean booleanOperate(long a, long b) {
    074                        return doubleOperate(a, b) != 0;
    075                }
    076
    077                private static long toLong(double d) {
    078                        if (Double.isInfinite(d) || Double.isNaN(d))
    079                                return 0;
    080                        return (long) d;
    081                }
    082
    083                @Override
    084                public long longOperate(long a, long b) {
    085                        return toLong(doubleOperate(a, b));
    086                }
    087
    088                /**
    089                 * override this
    090                 */
    091                @Override
    092                public void complexOperate(double[] out, double ra, double ia, double rb, double ib) {
    093                }
    094
    095                /**
    096                 * override this
    097                 */
    098                @Override
    099                public String toString(String string, String string2) {
    100                        return null;
    101                }
    102        }
    103}