-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1344 from hcoles/feature/delayed_execution2
Mutate delayed execution code stored in static fields
- Loading branch information
Showing
9 changed files
with
316 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...try/src/test/java/com/example/staticinitializers/delayedexecution/EnumFieldMethodRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.staticinitializers.delayedexecution; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public enum EnumFieldMethodRef { | ||
A(EnumFieldMethodRef::canMutate), B(EnumFieldMethodRef::canMutate); | ||
|
||
private final Supplier<String> supplier; | ||
|
||
|
||
EnumFieldMethodRef(Supplier<String> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
private static String canMutate() { | ||
return "Do not mutate"; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ntry/src/test/java/com/example/staticinitializers/delayedexecution/EnumFieldSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.staticinitializers.delayedexecution; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public enum EnumFieldSupplier { | ||
A(canMutate()), B(canMutate()); | ||
|
||
private final Supplier<String> supplier; | ||
|
||
EnumFieldSupplier(Supplier<String> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
private static Supplier<String> canMutate() { | ||
// don't mutate | ||
System.out.println("ideally would mutate me"); | ||
|
||
return () -> "Do not mutate"; // mutate | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...st/java/com/example/staticinitializers/delayedexecution/EnumMethodReferenceNotStored.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.staticinitializers.delayedexecution; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public enum EnumMethodReferenceNotStored { | ||
A(Arrays.asList(1,2,3)); | ||
|
||
private final List<Integer> l; | ||
EnumMethodReferenceNotStored(List<Integer> list) { | ||
l = list.stream() | ||
.filter(this::doNotMutate) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean doNotMutate(Integer i) { | ||
return i > 2; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...-entry/src/test/java/com/example/staticinitializers/delayedexecution/EnumMixedFields.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.staticinitializers.delayedexecution; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public enum EnumMixedFields { | ||
A(EnumMixedFields::canMutate, doNotMutate()), B(EnumMixedFields::canMutate, doNotMutate()); | ||
|
||
private final Supplier<String> supplier; | ||
private final String s; | ||
|
||
EnumMixedFields(Supplier<String> supplier, String s) { | ||
this.supplier = supplier; | ||
this.s = s; | ||
} | ||
|
||
private static String canMutate() { | ||
return "mutate me"; // mutate | ||
} | ||
|
||
private static String doNotMutate() { | ||
return "Do not mutate"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ry/src/test/java/com/example/staticinitializers/delayedexecution/StaticFunctionField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.staticinitializers.delayedexecution; | ||
|
||
import java.util.function.Function; | ||
|
||
public class StaticFunctionField { | ||
private static final Function<String,String> FOO = canMutate(); | ||
|
||
private static Function<String, String> canMutate() { | ||
// don't mutate | ||
System.out.println("ideally would mutate me"); | ||
|
||
return s -> s + "foo"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ry/src/test/java/com/example/staticinitializers/delayedexecution/StaticSupplierField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.staticinitializers.delayedexecution; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class StaticSupplierField { | ||
final static Supplier<String> SUPPLER = canMutate(); | ||
|
||
private static Supplier<String> canMutate() { | ||
// don't mutate | ||
System.out.println("ideally would mutate me"); | ||
|
||
return () -> "Do not mutate"; // mutate | ||
} | ||
} |
Oops, something went wrong.