Intersection Type
μ΄λ―Έ μ‘΄μ¬νλ μ¬λ¬ νμ μ λͺ¨λ λ§μ‘±νλ νμ μ νννκΈ° μν μλ¨μΌλ‘ μΈν°μΉμ νμ μ μ¬μ©νλ€.
μ΄λ μλ‘μ΄ ν΄λμ€λ₯Ό μ μνμ§ μκ³ μ¬λ¬κ°μ μΈν°νμ΄μ€λ₯Ό μ‘°ν©νμ¬ μ¬μ©νλ ννλ‘ μλμ κ°λ€.
Β
public static <T extends Function & Serializable & Cloneable> void getData(T t) {
/* statement */
}
Lambda Expresstion μΌλ‘ μλμ κ°μ΄ μ¬λ¬ μΈν°νμ΄μ€λ₯Ό μ‘°ν©νμ¬ μ¬μ©ν μ μλ€.
public class IntersectionType {
interface Hello extends Function {
default void hello(){ System.out.println("Hello"); }
}
interface Hi extends Function {
default void hi() { System.out.println("Hi"); }
}
interface Printer {
default void print(String str) { System.out.println(str); }
}
public static void main(String[] args) {
run((Function & Hello & Hi & Printer) s -> s, o -< {
o.hello();
o.hi();
o.println("Lambda");
});
}
public static <T extends Function> void run (T t, Consumer<T> consumer) { consumer.accpet(t); }
}
class
μ λμ μΌλ‘ μΆκ° νκΈ° μν Intersection Type
νλμ ν΄λμ€λ₯Ό κΈ°μ€μΌλ‘ λ°μ΄ν°λ₯Ό 곡μ νλ©° λΉμ§λμ€ λ‘μ§ μ¬μ©μ΄ κ°λ₯νκ²λ ꡬννμλ€.
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
Β
public class IntersectionType {
interface Pair<T> {
T getFirst();
T getSecond();
void setFirst(T first);
void setSecond(T second);
}
interface DelegateTo<T> {
T delegate();
}
interface ForwardingPair<T> extends DelegateTo<Pair<T>>, Pair<T> {
default T getFirst() { return delegate().getFirst(); }
default T getSecond() { return delegate().getSecond(); }
default void setFirst(T first) { delegate().getFirst(); }
default void setSecond(T second) { delegate().getSecond(); }
}
static class Name implements Pair<String> {
String firstName;
String lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String getFirst() { return firstName; }
@Override
public String getSecond() { return lastName; }
@Override
public void setFirst(String first) { this.firstName = first; }
@Override
public void setSecond(String second) { this.lastName = second; }
}
public static <T extends DelegateTo<S>, S> void run(T t, Consumer<T> consumer) {
consumer.accept(t);
}
public static void main(String[] args) {
Pair<String> name = new Name("Kim", "Sudal");
run((ForwardingPair<String> & Convertable<String> & Printable<String>) () -> name, o -> {
o.print();
o.convert(s -> s.toUpperCase());
o.print();
o.convert(s -> s.toLowerCase());
o.print();
});
}
interface Convertable<T> extends DelegateTo<Pair<T>> {
default void convert(Function<T, T> mapper) {
Pair<T> pair = delegate();
pair.setFirst(mapper.apply(pair.getFirst()));
pair.setSecond(mapper.apply(pair.getSecond()));
}
}
interface Printable<T> extends DelegateTo<Pair<T>> {
default void print() {
System.out.println(delegate().getFirst() + " " + delegate().getSecond());
}
}
}
Kim Sudal
KIM SUDAL
kim sudal
β Serialize Thread Local β