All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.brooklyn.util.math.MathFunctions Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 1.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.brooklyn.util.math;

import javax.annotation.Nullable;

import org.apache.brooklyn.util.guava.Functionals;
import org.apache.brooklyn.util.text.Strings;

import com.google.common.base.Function;

public class MathFunctions {

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function plusOld(final int addend) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Integer apply(@Nullable Number input) {
                if (input==null) return null;
                return input.intValue() + addend;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function plusOld(final long addend) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Long apply(@Nullable Number input) {
                if (input==null) return null;
                return input.longValue() + addend;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function plusOld(final double addend) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Double apply(@Nullable Number input) {
                if (input==null) return null;
                return input.doubleValue() + addend;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function timesOld(final int multiplicand) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Integer apply(@Nullable Number input) {
                if (input==null) return null;
                return input.intValue() * multiplicand;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function timesOld(final long multiplicand) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Long apply(@Nullable Number input) {
                if (input==null) return null;
                return input.longValue() * multiplicand;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function timesOld(final double multiplicand) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Double apply(@Nullable Number input) {
                if (input==null) return null;
                return input.doubleValue() * multiplicand;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function divideOld(final double divisor) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Double apply(@Nullable Number input) {
                if (input==null) return null;
                return input.doubleValue() / divisor;
            }
        };
    }

    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static  Function divideOld(final Function input, final double divisor) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public Double apply(@Nullable T input2) {
                if (input==null) return null;
                Number n = input.apply(input2);
                if (n==null) return null;
                return n.doubleValue() / divisor;
            }
        };
    }

    /** returns a string of up to maxLen length (longer in extreme cases) also capped at significantDigits significantDigits */
    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function readableStringOld(final int significantDigits, final int maxLen) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public String apply(@Nullable Number input) {
                if (input==null) return null;
                return Strings.makeRealString(input.doubleValue(), maxLen, significantDigits, 0);
            }
        };
    }

    /** returns a string where the input number is expressed as percent, with given number of significant digits */
    /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
    @SuppressWarnings("unused") @Deprecated 
    private static Function percentOld(final int significantDigits) {
        // TODO PERSISTENCE WORKAROUND
        return new Function() {
            @Override
            public String apply(@Nullable Number input) {
                if (input==null) return null;
                return readableString(significantDigits, significantDigits+3).apply(input.doubleValue() * 100d)+"%";
            }
        };
    }

    public static Function plus(int addend) {
        return new PlusInt(addend);
    }

    protected static class PlusInt implements Function {
        private final int addend;

        public PlusInt(int addend) {
            this.addend = addend;
        }
        @Override
        public Integer apply(@Nullable Number input) {
            if (input==null) return null;
            return input.intValue() + addend;
        }
    }

    public static Function plus(long addend) {
        return new PlusLong(addend);
    }

    protected static class PlusLong implements Function {
        private final long addend;

        public PlusLong(long addend) {
            this.addend = addend;
        }
        @Override
        public Long apply(@Nullable Number input) {
            if (input==null) return null;
            return input.longValue() + addend;
        }
    }

    public static Function plus(final double addend) {
        return new PlusDouble(addend);
    }

    protected static class PlusDouble implements Function {
        private final double addend;

        public PlusDouble(double addend) {
            this.addend = addend;
        }
        @Override
        public Double apply(@Nullable Number input) {
            if (input==null) return null;
            return input.doubleValue() + addend;
        }
    }

    public static Function times(final int multiplicand) {
        return new TimesInt(multiplicand);
    }

    protected static class TimesInt implements Function {
        private final int multiplicand;

        public TimesInt(int multiplicand) {
            this.multiplicand = multiplicand;
        }
        @Override
        public Integer apply(@Nullable Number input) {
            if (input==null) return null;
            return input.intValue() * multiplicand;
        }
    }

    public static Function times(long multiplicand) {
        return new TimesLong(multiplicand);
    }

    protected static class TimesLong implements Function {
        private final long multiplicand;

        public TimesLong(long multiplicand) {
            this.multiplicand = multiplicand;
        }
        @Override
        public Long apply(@Nullable Number input) {
            if (input==null) return null;
            return input.longValue() * multiplicand;
        }
    }

    public static Function times(final double multiplicand) {
        return new TimesDouble(multiplicand);
    }

    protected static class TimesDouble implements Function {
        private final double multiplicand;

        public TimesDouble(double multiplicand) {
            this.multiplicand = multiplicand;
        }
        @Override
        public Double apply(@Nullable Number input) {
            if (input==null) return null;
            return input.doubleValue() * multiplicand;
        }
    }

    public static Function divide(double divisor) {
        return new DivideDouble(divisor);
    }

    protected static class DivideDouble implements Function {
        private final double divisor;

        public DivideDouble(double divisor) {
            this.divisor = divisor;
        }
        @Override
        public Double apply(@Nullable Number input) {
            if (input==null) return null;
            return input.doubleValue() / divisor;
        }
    }

    /** returns a string of up to maxLen length (longer in extreme cases) also capped at significantDigits significantDigits */
    public static Function readableString(int significantDigits, int maxLen) {
        return new ReadableString(significantDigits, maxLen);
    }

    protected static class ReadableString implements Function {
        private final int significantDigits;
        private final int maxLen;
        public ReadableString(int significantDigits, int maxLen) {
            this.significantDigits = significantDigits;
            this.maxLen = maxLen;
        }
        @Override
        public String apply(@Nullable Number input) {
            if (input==null) return null;
            return Strings.makeRealString(input.doubleValue(), maxLen, significantDigits, 0);
        }
    };

    /** returns a string where the input number is expressed as percent, with given number of significant digits */
    public static Function percent(int significantDigits) {
        return new Percent(significantDigits);
    }

    private static class Percent implements Function {
        final int significantDigits;
        public Percent(int significantDigits) {
            this.significantDigits = significantDigits;
        }

        @Override
        public String apply(@Nullable Number input) {
            if (input==null) return null;
            return readableString(significantDigits, significantDigits+3).apply(input.doubleValue() * 100d)+"%";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy