Java tutorial
/******************************************************************************* * Copyright 2014 Carlos Macasaet * * Licensed 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 com.macasaet.lambda.fluent; import static com.google.common.collect.Collections2.filter; import static com.google.common.collect.Lists.transform; import static com.macasaet.lambda.fluent.FluentLambda.forMethod; import static com.macasaet.lambda.fluent.FluentLambda.ofClass; import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Collection; import java.util.List; import org.apache.commons.math.linear.AnyMatrix; import org.apache.commons.math.linear.Array2DRowRealMatrix; import org.apache.commons.math.linear.ArrayRealVector; import org.apache.commons.math.linear.OpenMapRealVector; import org.apache.commons.math.linear.RealVector; import org.junit.Test; import com.google.common.base.Function; import com.google.common.base.Predicate; /** * Example usage of the {@link FluentLambda} class. * * <p>Copyright © 2014 Carlos Macasaet</p> * * @author Carlos Macasaet <cmacasaet@edmunds.com> */ public class Examples { private final Array2DRowRealMatrix squareMatrix = new Array2DRowRealMatrix( new double[][] { { 0, 1 }, { 2, 3 } }); private final Array2DRowRealMatrix nonSquareMatrix = new Array2DRowRealMatrix( new double[][] { { 0, 1, 3 }, { 4, 5, 6 } }); private final Collection<? extends AnyMatrix> matrices = asList(squareMatrix, nonSquareMatrix); private final RealVector threeFour = new ArrayRealVector(new double[] { 3, 4 }); private final RealVector sixEight = new OpenMapRealVector(new double[] { 6, 8 }); private final List<? extends RealVector> vectors = asList(threeFour, sixEight); /** * This is the standard way of defining {@link Predicate Predicates} in Guava. Notice that even in this simple case * of a single method invocation, the Predicate definition uses five lines. */ @Test public final void definePredicateWithAnonymousInnerClass() { final Predicate<? super AnyMatrix> matrixIsSquare = new Predicate<AnyMatrix>() { public boolean apply(final AnyMatrix input) { return input.isSquare(); } }; final Collection<? extends AnyMatrix> result = filter(matrices, matrixIsSquare); assertTrue(result.contains(squareMatrix)); assertFalse(result.contains(nonSquareMatrix)); } /** * This is a more concise and declarative way of defining a {@link Predicate}. It uses an example method invocation * to create the functor. */ @Test public final void definePredicateWithFluentInterface() { final Predicate<? super AnyMatrix> matrixIsSquare = forMethod(ofClass(AnyMatrix.class).isSquare()); final Collection<? extends AnyMatrix> result = filter(matrices, matrixIsSquare); assertTrue(result.contains(squareMatrix)); assertFalse(result.contains(nonSquareMatrix)); } /** * This is the standard way of defining {@link Function Functions} in Guava. Even though it only invokes a single * method on the input, it still requires five lines. */ @Test public final void defineFunctionWithAnonymousInnerClass() { final Function<? super RealVector, ? extends Double> normCalculator = new Function<RealVector, Double>() { public Double apply(final RealVector input) { return input.getNorm(); } }; final List<? extends Double> result = transform(vectors, normCalculator); assertEquals(asList(5.0d, 10.0d), result); } /** * This is a more concise and declarative way of defining a {@link Function}. It uses an example invocation to build * the functor. */ @Test public final void defineFunctionWithFluentInterface() { final Function<? super RealVector, ? extends Double> normCalculator = forMethod( ofClass(RealVector.class).getNorm()); final List<? extends Double> result = transform(vectors, normCalculator); assertEquals(asList(5.0d, 10.0d), result); } }