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