Created
November 21, 2021 01:00
-
-
Save alcatrazEscapee/774a5b42c1eef5783b7d44b4787417ff to your computer and use it in GitHub Desktop.
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
package constructor_reference_hacks; | |
import java.util.function.Predicate; | |
public class ConstructorReferenceHacks | |
{ | |
static class Super | |
{ | |
// Super class has a constructor parameter of a predicate, which we want to be able to use 'this' | |
protected Super(Predicate<String> function) {} | |
} | |
static class Box<T> | |
{ | |
// A simple mutable box class. Can use apache's Mutable<T> if desired | |
private T t; | |
} | |
static class Sub extends Super | |
{ | |
// Need this constructor wrapper | |
public static Sub create() | |
{ | |
final Box<Sub> box = new Box<>(); | |
final Sub sub = new Sub(value -> box.t.isFeatureChunk(value)); | |
box.t = sub; | |
return sub; | |
} | |
private Sub(Predicate<String> function) | |
{ | |
super(function); | |
} | |
// This is a method on the sub class, which is passed indirectly to the constructor | |
// This will work as long as the super constructor does not invoke the predicate directly. | |
private boolean isFeatureChunk(String value) | |
{ | |
return true; // whatever logic here | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment