Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,43 @@
interface Thingable {
String thing();
}
class Delegator {
public Thingable delegate;
public String operation() {
if (delegate == null)
return "default implementation";
else
return delegate.thing();
}
}
class Delegate implements Thingable {
public String thing() {
return "delegate implementation";
}
}
// Example usage
// Memory management ignored for simplification
public class DelegateExample {
public static void main(String[] args) {
// Without a delegate:
Delegator a = new Delegator();
assert a.operation().equals("default implementation");
// With a delegate:
Delegate d = new Delegate();
a.delegate = d;
assert a.operation().equals("delegate implementation");
// Same as the above, but with an anonymous class:
a.delegate = new Thingable() {
public String thing() {
return "anonymous delegate implementation";
}
};
assert a.operation().equals("anonymous delegate implementation");
}
}

View file

@ -0,0 +1,6 @@
package delegate;
@FunctionalInterface
public interface Thingable {
public String thing();
}

View file

@ -0,0 +1,19 @@
package delegate;
import java.util.Optional;
public interface Delegator {
public Thingable delegate();
public Delegator delegate(Thingable thingable);
public static Delegator new_() {
return $Delegator.new_();
}
public default String operation() {
return Optional.ofNullable(delegate())
.map(Thingable::thing)
.orElse("default implementation")
;
}
}

View file

@ -0,0 +1,17 @@
package delegate;
@FunctionalInterface
/* package */ interface $Delegator extends Delegator {
@Override
public default Delegator delegate(Thingable thingable) {
return new_(thingable);
}
public static $Delegator new_() {
return new_(() -> null);
}
public static $Delegator new_(Thingable thingable) {
return () -> thingable;
}
}

View file

@ -0,0 +1,8 @@
package delegate;
public final class Delegate implements Thingable {
@Override
public String thing() {
return "delegate implementation";
}
}

View file

@ -0,0 +1,36 @@
package delegate;
// Example usage
// Memory management ignored for simplification
public interface DelegateTest {
public static String thingable() {
return "method reference implementation";
}
public static void main(String... arguments) {
// Without a delegate:
Delegator d1 = Delegator.new_();
assert d1.operation().equals("default implementation");
// With a delegate:
Delegator d2 = d1.delegate(new Delegate());
assert d2.operation().equals("delegate implementation");
// Same as the above, but with an anonymous class:
Delegator d3 = d2.delegate(new Thingable() {
@Override
public String thing() {
return "anonymous delegate implementation";
}
});
assert d3.operation().equals("anonymous delegate implementation");
// Same as the above, but with a method reference:
Delegator d4 = d3.delegate(DelegateTest::thingable);
assert d4.operation().equals("method reference implementation");
// Same as the above, but with a lambda expression:
Delegator d5 = d4.delegate(() -> "lambda expression implementation");
assert d5.operation().equals("lambda expression implementation");
}
}