-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExamples.java
More file actions
115 lines (99 loc) · 4.71 KB
/
Examples.java
File metadata and controls
115 lines (99 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
* 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);
}
}