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 unary operation for implementations over different output domains
017 * @since 2.0
018 */
019public interface UnaryOperation extends IOperation {
020
021        /**
022         * @param a
023         * @return op(a)
024         */
025        boolean booleanOperate(long a);
026
027        /**
028         * @param a
029         * @return op(a)
030         */
031        long longOperate(long a);
032
033        /**
034         * @param a
035         * @return op(a)a
036         */
037        double doubleOperate(double a);
038
039        /**
040         * @param out holds op(ra, ia)
041         * @param ra
042         * @param ia
043         */
044        void complexOperate(double[] out, double ra, double ia);
045
046        /**
047         * @param a
048         * @return string to represent output
049         */
050        String toString(String a);
051
052        /**
053         * Stub class where only two methods need to be overridden:
054         *  {@link #complexOperate(double[], double, double)},
055         *  {@link #toString(String)}
056         */
057        public static class Stub implements UnaryOperation {
058                double[] z = new double[2];
059
060                @Override
061                public double doubleOperate(double a) {
062                        complexOperate(z, a, 0);
063                        return z[0];
064                }
065
066                @Override
067                public boolean booleanOperate(long a) {
068                        return doubleOperate(a) != 0;
069                }
070
071                private static long toLong(double d) {
072                        if (Double.isInfinite(d) || Double.isNaN(d))
073                                return 0;
074                        return (long) d;
075                }
076
077                @Override
078                public long longOperate(long a) {
079                        return toLong(doubleOperate(a));
080                }
081
082                /**
083                 * Override this
084                 */
085                @Override
086                public void complexOperate(double[] out, double ra, double ia) {
087                }
088
089                /**
090                 * Override this
091                 */
092                @Override
093                public String toString(String a) {
094                        return null;
095                }
096        }
097}