001package conexp.fx.core.collections;
002
003import java.util.Collection;
004
005/*
006 * #%L
007 * Concept Explorer FX
008 * %%
009 * Copyright (C) 2010 - 2023 Francesco Kriegel
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU General Public License as
013 * published by the Free Software Foundation, either version 3 of the
014 * License, or (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU General Public
022 * License along with this program.  If not, see
023 * <http://www.gnu.org/licenses/gpl-3.0.html>.
024 * #L%
025 */
026
027import java.util.Iterator;
028
029import com.google.common.base.Function;
030import com.google.common.base.Predicate;
031
032import javafx.beans.value.ObservableValue;
033
034public final class GuavaFunctions {
035
036  public static final <X> Predicate<X> toGuavaPredicate(final java.util.function.Predicate<X> p) {
037    return new Predicate<X>() {
038
039      @Override
040      public final boolean apply(final X x) {
041        return p.test(x);
042      }
043    };
044  }
045
046  public static final <X> java.util.function.Predicate<X> toJavaPredicate(final Predicate<X> p) {
047    return p::apply;
048  }
049
050  public static final <X, Y> Function<X, Y> toGuavaFunction(final java.util.function.Function<X, Y> f) {
051    return new Function<X, Y>() {
052
053      @Override
054      public final Y apply(final X x) {
055        return f.apply(x);
056      }
057    };
058  }
059
060  public static final <X, Y> java.util.function.Function<X, Y> toJavaFunction(final Function<X, Y> f) {
061    return f::apply;
062  }
063
064  public static final <T> Function<ObservableValue<T>, T> observableValueToCurrentValueFunction() {
065    return toGuavaFunction(ObservableValue::getValue);
066  }
067
068  public static final Function<Iterable<Double>, Double> sum     = toGuavaFunction(doubles -> {
069                                                                   double s = 0d;
070                                                                   for (Double d : doubles)
071                                                                     s += d;
072                                                                   return s;
073                                                                 });
074
075  public static final Function<Iterable<Double>, Double> product = toGuavaFunction(doubles -> {
076                                                                   double s = 1d;
077                                                                   for (Double d : doubles)
078                                                                     s *= d;
079                                                                   return s;
080                                                                 });
081
082  public static final Function<Iterable<Double>, Double> wsum(final Iterable<Double> weighs) {
083    return toGuavaFunction(doubles -> {
084      final Iterator<Double> weigh = weighs.iterator();
085      double s = 0d;
086      for (Double d : doubles)
087        s += weigh.next() * d;
088      return s;
089    });
090  }
091
092  public static final Function<Iterable<Double>, Double> wproduct(final Iterable<Double> weighs) {
093    return toGuavaFunction(doubles -> {
094      final Iterator<Double> weigh = weighs.iterator();
095      double s = 1d;
096      for (Double d : doubles)
097        s *= Math.pow(d, weigh.next());
098      return s;
099    });
100  }
101
102  public static final Function<Double, Double> power(final double p) {
103    return toGuavaFunction(x -> Math.pow(x, p));
104  }
105
106  public static final Function<Double, Double> root(final double p) {
107    return toGuavaFunction(x -> Math.pow(x, 1d / p));
108  }
109
110  public static final <E> Predicate<Collection<E>> isEmpty() {
111    return toGuavaPredicate(Collection::isEmpty);
112  }
113}