hg: lambda/lambda/jdk: Update initial lambda APIs and tests to use new syntax.
Stuart Marks
stuart.marks at oracle.com
Sun Sep 11 22:02:33 PDT 2011
Hi Rémi,
Thanks for generating this patch. In general, the generic signature changes
look pretty good. I had to make some adjustments in order to get things to
compile. I've appended and attached an additional patch to be applied on top of
yours. Please check it over. With this additional patch applied, everything
compiles and the relevant tests pass.
(I'm attaching the patch as well as appending it in the message body, since
including it in the message body subjects it to line wrapping and whitespace
mangling. At least Rémi might receive the attachment intact, even if the rest
of the mailing list doesn't.)
There were several places where a type parameter was removed. Unfortunately in
some cases the method body had declared local variables using that type
parameter. I've redeclared those locals to have the expanded type, usually
something like Predicate<? super T>. This simplifies the method signature at
the expense of making the method body more complicated. I think this is the
right tradeoff, though, as it buries the use of wildcards within the library
implementation instead of exposing it to all clients.
In the case of Mappers.instantiate(), I had to restore the 'final' declaration
on the 'constructor' local variable. Without this declaration the compiler
complained that this variable isn't effectively final. Hm, if code compiles and
works with a variable declared 'final', and then the code is changed only by
removing 'final', shouldn't that variable be effectively final? Maybe it's a
compiler bug.
Rémi, I think you have push access to the repos, so go ahead and apply my patch
and do a push. If you have any trouble, please let me know and I can do it for you.
Thanks again.
s'marks
[patch appended below]
On 9/11/11 4:52 AM, Rémi Forax wrote:
> Hi all,
> I've tried to fix some bugs of the current implementation of
> java.util.functions.
>
> - some constants are still public
> (BTW, when the compiler will be able to generate the correct bytecode
> for a lambda, these constants will be not needed any more
> because constant lambda will be constant by default)
> - a lot of generic signature aren't right and there is inconsistency
> between the files.
> - some casts are wrong, you can't cast a Predicate<? super T> to
> a Predicate<T>, it's not safe.
> - some methods return a Predicate<Object> instead of a
> Predicate<T> so it will not work if the predicate is used by example
> in a ?:
> - some short cuts in the implementation are wrong (by example,
> testing if there is an element in an iterable before creating
> the lambda is wrong because the iterable can be populated
> before the lambda is called.
> - fix some implementations, BTW I think the methods xor should be removed
> for the same reason there is no ^^ in C
> (nobody use xor in test between booleans)
>
> cheers,
> Rémi
diff -r f6aef0811357 src/share/classes/java/util/functions/Blocks.java
--- a/src/share/classes/java/util/functions/Blocks.java Sun Sep 11 21:30:34
2011 -0700
+++ b/src/share/classes/java/util/functions/Blocks.java Sun Sep 11 21:44:53
2011 -0700
@@ -87,7 +87,7 @@
* @return a Block which performs in sequence the {@code first} and
* {@code second} Blocks
*/
- public static Block<T> chain(
+ public static <T> Block<T> chain(
Block<? super T> first, Block<? super T> second) {
Objects.requireNonNull(first);
Objects.requireNonNull(second);
@@ -119,7 +119,7 @@
}
return t -> {
- for (B block : sequence)
+ for (Block<? super T> block : sequence)
block.apply(t);
};
}
@@ -143,7 +143,7 @@
return t -> {
first.apply(t);
- for (B block : sequence)
+ for (Block<? super T> block : sequence)
block.apply(t);
};
}
@@ -163,7 +163,7 @@
Objects.requireNonNull(sequence);
return t -> {
- for (B block : sequence)
+ for (Block<? super T> block : sequence)
block.apply(t);
};
}
@@ -189,7 +189,7 @@
return t -> {
first.apply(t);
- for (B block : sequence)
+ for (Block<? super T> block : sequence)
block.apply(t);
};
}
diff -r f6aef0811357 src/share/classes/java/util/functions/Mappers.java
--- a/src/share/classes/java/util/functions/Mappers.java Sun Sep 11 21:30:34
2011 -0700
+++ b/src/share/classes/java/util/functions/Mappers.java Sun Sep 11 21:44:53
2011 -0700
@@ -154,7 +154,7 @@
Objects.requireNonNull(clazzT);
Objects.requireNonNull(clazzU);
- Constructor<? extends U> constructor;
+ final Constructor<? extends U> constructor;
try {
constructor = clazzU.getConstructor(clazzT);
} catch(NoSuchMethodException noConstructor) {
diff -r f6aef0811357 src/share/classes/java/util/functions/Predicates.java
--- a/src/share/classes/java/util/functions/Predicates.java Sun Sep 11 21:30:34
2011 -0700
+++ b/src/share/classes/java/util/functions/Predicates.java Sun Sep 11 21:44:53
2011 -0700
@@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.Collection;
+import java.util.Iterator;
import java.util.Objects;
/**
@@ -70,6 +71,7 @@
* Returns a predicate that evaluates to {@code true} if the reference
* being tested is {@code null}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @return a predicate that evaluates to {@code true} if the reference
* being tested is {@code null}
*/
@@ -81,6 +83,7 @@
* Returns a predicate that evaluates to {@code true} if the reference
* being tested is non-{@code null}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @return a predicate that evaluates to {@code true} if the reference
* being tested is is non-{@code null}
*/
@@ -91,6 +94,7 @@
/**
* Returns a predicate that always evaluates to {@code false}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @return a predicate that always evaluates to {@code false}.
*/
public static <T> Predicate<T> alwaysFalse() {
@@ -100,6 +104,7 @@
/**
* Returns a predicate that always evaluates to {@code true}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @return a predicate that always evaluates to {@code true}.
*/
public static <T> Predicate<T> alwaysTrue() {
@@ -111,6 +116,7 @@
* tested is an instance of the provided class. If the object being tested
* is {@code null} this predicate evaluates to {@code false}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @param clazz The target class to be matched by the predicate.
* @return a predicate that evaluates to {@code true} if the object being
* tested is an instance of the provided class
@@ -122,6 +128,7 @@
/**
* Returns a predicate that who's result is {@code target == object}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @param target The target value to be compared for identity equality.
* @return a predicate that who's result is {@code target == object}.
*/
@@ -133,6 +140,7 @@
* Returns a predicate who's result matches
* {@code Objects.equals(target, t)}.
*
+ * @param <T> the type of values evaluated by the predicate.
* @param t The target value to be compared for equality.
* @return a predicate who's result matches {@code Objects.equals(target, t)}
*/
@@ -175,13 +183,11 @@
* predicate evaluates to {@code false}
*
* @param <T> the type of values evaluated by the predicate.
- * @param <P> type of predicates.
* @param predicate The predicate to be evaluated.
* @return A predicate who's result is the logical inverse of the provided
* predicate.
*/
- public static <T> Predicate<T> negate(
- Predicate<? super T> predicate) {
+ public static <T> Predicate<T> negate(Predicate<? super T> predicate) {
return t -> !predicate.eval(t);
}
@@ -192,7 +198,6 @@
* {@code false} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param first initial component predicate to be evaluated.
* @param second additional component predicate to be evaluated.
* @return A predicate who's result is {@code true} iff all component
@@ -213,7 +218,6 @@
* {@code false} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} iff all component
* predicates are {@code true}.
@@ -223,7 +227,7 @@
Objects.requireNonNull(predicates);
return t -> {
- for (P predicate : predicates) {
+ for (Predicate<? super T> predicate : predicates) {
if (!predicate.eval(t)) {
return false;
}
@@ -239,14 +243,13 @@
* {@code false} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param first An initial predicate to be evaluated before the others.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} iff all component
* predicates are {@code true}.
*/
static <T> Predicate<T> and(
- P first, Iterable<? extends Predicate<? super T>> predicates) {
+ Predicate<? super T> first, Iterable<? extends Predicate<? super
T>> predicates) {
Objects.requireNonNull(first);
Objects.requireNonNull(predicates);
@@ -254,7 +257,7 @@
if (!first.eval(t)) {
return false;
}
- for (P predicate : predicates) {
+ for (Predicate<? super T> predicate : predicates) {
if (!predicate.eval(t)) {
return false;
}
@@ -270,7 +273,6 @@
* {@code false} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} iff all component
* predicates are {@code true}.
@@ -287,8 +289,7 @@
* {@code false} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
- * @param first An initial predicate to be evaluated.
+ * @param first An initial predicate to be evaluated.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} iff all component
* predicates are {@code true}.
@@ -306,7 +307,6 @@
* {@code true} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param first initial component predicate to be evaluated.
* @param second additional component predicate to be evaluated.
* @return A predicate who's result is {@code true} if any component
@@ -327,7 +327,6 @@
* {@code true} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} if any component
* predicate's result is {@code true}.
@@ -337,7 +336,7 @@
Objects.requireNonNull(predicates);
return t -> {
- for (P predicate : predicates) {
+ for (Predicate<? super T> predicate : predicates) {
if (predicate.eval(t)) {
return true;
}
@@ -353,7 +352,6 @@
* {@code true} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} if any component
* predicate's result is {@code true}.
@@ -367,7 +365,7 @@
if (first.eval(t)) {
return true;
}
- for (P predicate : predicates) {
+ for (Predicate<? super T> predicate : predicates) {
if (predicate.eval(t)) {
return true;
}
@@ -383,7 +381,6 @@
* {@code true} predicate.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return A predicate who's result is {@code true} if any component
* predicate's result is {@code true}.
@@ -418,7 +415,6 @@
* fails to match the first predicate's result.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param first initial component predicate to be evaluated.
* @param second additional component predicate to be evaluated.
* @return a predicate that evaluates to {@code false} if all or none of
@@ -439,7 +435,6 @@
* fails to match the first predicate's result.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param components The predicates to be evaluated.
* @return a predicate that evaluates to {@code false} if all or none of
* the component predicates evaluate to {@code true}
@@ -450,7 +445,7 @@
return t -> {
- Iterator<T> iterator = predicates.iterator();
+ Iterator<? extends Predicate <? super T>> iterator =
predicates.iterator();
if (!iterator.hasNext()) {
return false;
}
@@ -460,7 +455,7 @@
return true;
}
}
- return value;
+ return false;
};
}
@@ -471,19 +466,18 @@
* fails to match the first predicate's result.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return a predicate that evaluates to {@code false} if all or none of the
* component predicates evaluate to {@code true}
*/
- static Predicate<T> xor(
- P first, Iterable<? extends Predicate<? super T>> predicates) {
+ static <T> Predicate<T> xor(
+ Predicate<? super T> first, Iterable<? extends Predicate<? super
T>> predicates) {
Objects.requireNonNull(first);
Objects.requireNonNull(predicates);
return t -> {
boolean initial = first.eval(t);
- for (P predicate : predicates) {
+ for (Predicate<? super T> predicate : predicates) {
if (!(initial ^ predicate.eval(t))) {
return true;
}
@@ -499,7 +493,6 @@
* fails to match the first predicate's result.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return a predicate that evaluates to {@code false} if all or none of the
* component predicates evaluate to {@code true}
@@ -516,14 +509,13 @@
* fails to match the first predicate's result.
*
* @param <T> the type of values evaluated by the predicates.
- * @param <P> type of predicates.
* @param predicates The predicates to be evaluated.
* @return a predicate that evaluates to {@code false} if all or none of the
* component predicates evaluate to {@code true}
*/
@SafeVarargs
static <T> Predicate<T> xor(
- P first, Predicate<? super T>... predicates) {
+ Predicate<? super T> first, Predicate<? super T>... predicates) {
return xor(first, Arrays.asList(predicates));
}
}
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: remi2.pch.txt
Url: http://mail.openjdk.java.net/pipermail/lambda-dev/attachments/20110911/7324bb01/attachment-0001.txt
More information about the lambda-dev
mailing list