You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/01/03 19:45:29 UTC
svn commit: r1054705 - in /tapestry/tapestry5/trunk/tapestry-func/src:
main/java/org/apache/tapestry5/func/ test/java/org/apache/tapestry5/func/
Author: hlship
Date: Mon Jan 3 18:45:28 2011
New Revision: 1054705
URL: http://svn.apache.org/viewvc?rev=1054705&view=rev
Log:
TAP5-1390: Convert the F.eq(), F.gteq(), etc. methods to use Comparable, not Number
Modified:
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper.java
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Predicate.java
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java
Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java?rev=1054705&r1=1054704&r2=1054705&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java (original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java Mon Jan 3 18:45:28 2011
@@ -1,4 +1,4 @@
-// Copyright 2010 The Apache Software Foundation
+// Copyright 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -44,81 +44,92 @@ public class F
}
/**
- * A Predicate factory for equality against a specified value.
+ * A Predicate factory for equality of q Comparable element from a flow against a specified
+ * value.
*/
public static <T> Predicate<T> eql(final T value)
{
return new Predicate<T>()
{
- public boolean accept(T object)
+ public boolean accept(T element)
{
- return object.equals(value);
+ return element.equals(value);
};
};
}
/**
- * A Predicate factory for comparison of a Number against a fixed value; true
- * if the Number equals the value.
+ * A Predicate factory for comparison of a Comparable element from a flow against a fixed value.
*/
- public static Predicate<Number> eq(final long value)
+ public static <T extends Comparable<T>> Predicate<T> eq(final T value)
{
- return new Predicate<Number>()
+ return new Predicate<T>()
{
- public boolean accept(Number object)
+ public boolean accept(T element)
{
- return object.longValue() == value;
+ return element.compareTo(value) == 0;
}
};
}
/**
- * A Predicate factory for comparison of a Number against a fixed value; true
- * if the Number does not equal the value.
+ * A Predicate factory for comparison of a Comparable element against a fixed value.
*/
- public static Predicate<Number> neq(long value)
+ public static <T extends Comparable<T>> Predicate<T> neq(final T value)
{
- return eq(value).invert();
+ return new Predicate<T>()
+ {
+ public boolean accept(T object)
+ {
+ return object.compareTo(value) != 0;
+ }
+ };
}
/**
- * A Predicate factory for comparison of a Number against a fixed value; true
- * if the number is greater than the value.
+ * A Predicate factory for comparison of a Comparable against a fixed value; true
+ * if the flow element is greater than the provided value.
*/
- public static Predicate<Number> gt(final long value)
+ public static <T extends Comparable<T>> Predicate<T> gt(final T value)
{
- return new Predicate<Number>()
+ return new Predicate<T>()
{
- public boolean accept(Number object)
+ public boolean accept(T element)
{
- return object.longValue() > value;
+ return element.compareTo(value) > 0;
}
};
}
/**
- * A Predicate factory for comparison of a Number against a fixed value; true
- * if the Number is greater than or equal to the value.
+ * A Predicate factory for comparison of a Comparable against a fixed value; true
+ * if the flow element is greater than or equal to the value.
*/
- public static Predicate<Number> gteq(long value)
+ public static <T extends Comparable<T>> Predicate<T> gteq(final T value)
{
- return eq(value).or(gt(value));
+ return new Predicate<T>()
+ {
+ public boolean accept(T element)
+ {
+ return element.compareTo(value) >= 0;
+ }
+ };
}
/**
- * A Predicate factory for comparison of a Number against a fixed value; true
- * if the Number is less than the value.
+ * A Predicate factory for comparison of a Comparable against a fixed value; true
+ * if the element is less than the value.
*/
- public static Predicate<Number> lt(long value)
+ public static <T extends Comparable<T>> Predicate<T> lt(T value)
{
return gteq(value).invert();
}
/**
- * A Predicate factory for comparison of a Number against a fixed value; true
- * if the Number is less than or equal to the value.
+ * A Predicate factory for comparison of a Comprable element against a fixed value; true
+ * if the element is less than or equal to the value.
*/
- public static Predicate<Number> lteq(long value)
+ public static <T extends Comparable<T>> Predicate<T> lteq(T value)
{
return gt(value).invert();
}
@@ -130,9 +141,9 @@ public class F
{
return new Predicate<T>()
{
- public boolean accept(T object)
+ public boolean accept(T element)
{
- return object == null;
+ return element == null;
}
};
}
Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper.java?rev=1054705&r1=1054704&r2=1054705&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper.java (original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper.java Mon Jan 3 18:45:28 2011
@@ -1,4 +1,4 @@
-// Copyright 2010 The Apache Software Foundation
+// Copyright 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -20,11 +20,18 @@ package org.apache.tapestry5.func;
* to another (or otherwise transformed).
*
* @since 5.2.0
+ * @param <S>
+ * type of source flow
+ * @param <T>
+ * type of target (output) flow
*/
public abstract class Mapper<S, T>
{
- /** Implemented in subclasses to map a source value to a target value. */
- public abstract T map(S value);
+ /**
+ * Implemented in subclasses to map an element from the source flow to an element of the target
+ * flow.
+ */
+ public abstract T map(S element);
/**
* Combines this mapper (S -->T) with another mapper (T -->X) to form
@@ -33,17 +40,16 @@ public abstract class Mapper<S, T>
public final <X> Mapper<S, X> combine(final Mapper<T, X> other)
{
assert other != null;
-
+
final Mapper<S, T> stMapper = this;
return new Mapper<S, X>()
{
- public X map(S value)
+ public X map(S element)
{
+ T tElement = stMapper.map(element);
- T tValue = stMapper.map(value);
-
- return other.map(tValue);
+ return other.map(tElement);
}
};
}
Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Predicate.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Predicate.java?rev=1054705&r1=1054704&r2=1054705&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Predicate.java (original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Predicate.java Mon Jan 3 18:45:28 2011
@@ -1,4 +1,4 @@
-// Copyright 2010 The Apache Software Foundation
+// Copyright 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -14,7 +14,6 @@
package org.apache.tapestry5.func;
-
/**
* Used when filtering a collection of objects of a given type; the predicate is passed
* each object in turn, and returns true to include the object in the result collection.
@@ -30,12 +29,15 @@ public abstract class Predicate<T>
/**
* This method is overridden in subclasses to define which objects the Predicate will accept
* and which it will reject.
+ *
+ * @param element
+ * the element from the flow to be evaluated by the Predicate
*/
- public abstract boolean accept(T object);
+ public abstract boolean accept(T element);
/**
- * Combines this Predicate with another compatible Predicate to form a new Predicate, which is returned. The
- * new Predicate is true only if both of the combined Predicates are true.
+ * Combines this Predicate with another compatible Predicate to form a new Predicate, which is
+ * returned. The new Predicate is true only if both of the combined Predicates are true.
*/
public final Predicate<T> and(final Predicate<? super T> other)
{
@@ -53,7 +55,8 @@ public abstract class Predicate<T>
}
/**
- * Combines this Predicate with another compatible Predicate to form a new Predicate, which is returned. The
+ * Combines this Predicate with another compatible Predicate to form a new Predicate, which is
+ * returned. The
* new Predicate is true if either of the combined Predicates are true.
*/
public final Predicate<T> or(final Predicate<? super T> other)
@@ -72,7 +75,8 @@ public abstract class Predicate<T>
}
/**
- * Inverts this Predicate, returning a new Predicate that inverts the value returned from {@link #accept}.
+ * Inverts this Predicate, returning a new Predicate that inverts the value returned from
+ * {@link #accept}.
*/
public final Predicate<T> invert()
{
Modified: tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java?rev=1054705&r1=1054704&r2=1054705&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java Mon Jan 3 18:45:28 2011
@@ -1,4 +1,4 @@
-// Copyright 2010 The Apache Software Foundation
+// Copyright 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -205,9 +205,29 @@ public class FuncTest extends BaseFuncTe
{
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
- List<Integer> output = F.flow(input).filter(evenp.and(F.gt(3))).toList();
+ List<Integer> output = F.flow(input).filter(F.gt(2).and(F.lt(5))).toList();
- assertListsEquals(output, 4, 6);
+ assertListsEquals(output, 3, 4);
+ }
+
+ @Test
+ public void combine_predicate_with_or()
+ {
+ List<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
+
+ List<Integer> output = F.flow(input).filter(F.lt(3).or(F.gt(5))).toList();
+
+ assertListsEquals(output, 1, 2, 6, 7);
+ }
+
+ @Test
+ public void eql_predicate()
+ {
+ List<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
+
+ List<Integer> output = F.flow(input).filter(F.eql(4)).toList();
+
+ assertListsEquals(output, 4);
}
@Test