Facebook
From sheeple, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 154
  1. ////////////////////////////////////////////////////////////////////////////////
  2. package pl.glx.pattern;
  3.  
  4. public interface HasFriendship<Owner extends HasFriendship<Owner, Friend>, Friend extends HasFriendship<Friend, Owner>> {
  5.     Friendship<Owner, ?> friendship(Class friendClass);
  6. }
  7.  
  8. ////////////////////////////////////////////////////////////////////////////////
  9. package pl.glx.pattern;
  10.  
  11. public class Friendship<Owner extends HasFriendship, Friend extends HasFriendship>
  12.     implements HasFriend<Owner, Friend> {
  13.  
  14.     final Owner         owner;
  15.     Friend              friend;
  16.     final Class<Friend> friendClass;
  17.     final Class<Owner>  ownerClass;
  18.  
  19.     public Friendship(Owner owner, Class<Friend> friendClass) {
  20.         this.owner = owner;
  21.         this.friendClass = friendClass;
  22.         this.ownerClass = (Class<Owner>) owner.getClass();
  23.     }
  24.  
  25.     @Override
  26.     public Owner set(Friend newFriend) {
  27.         if (null != newFriend) {
  28.             if (null != newFriend.friendship(ownerClass).friend)
  29.                 newFriend.friendship(ownerClass).friend.friendship(friendClass).friend = null;
  30.             if (null != this.friend)
  31.                 this.friend.friendship(ownerClass).friend = null;
  32.             newFriend.friendship(ownerClass).friend = owner;
  33.             this.friend = newFriend;
  34.         }
  35.         else if (null == newFriend) {
  36.             if (null != this.friend)
  37.                 this.friend.friendship(ownerClass).friend = null;
  38.             this.friend = null;
  39.         }
  40.         return owner;
  41.     }
  42.  
  43.     public Friend get() {
  44.         return friend;
  45.     }
  46. }
  47.  
  48. ////////////////////////////////////////////////////////////////////////////////
  49. package pl.glx.pattern;
  50.  
  51. import lombok.*;
  52. import lombok.experimental.Accessors;
  53.  
  54. @Accessors(fluent = true)
  55. public class A implements HasName<A>, HasFriendship {
  56.  
  57.     @Getter @Setter
  58.     String name;
  59.  
  60.     public A(String name) {
  61.         this.name = name;
  62.     }
  63.  
  64.     Friendship<A, B> fb = new Friendship<>(this, B.class);
  65.  
  66.     @Override
  67.     public Friendship<A, B> friendship(Class friendClass) {
  68.         return fb;
  69.     }
  70. }
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////
  73. package pl.glx.pattern;
  74.  
  75. import lombok.*;
  76. import lombok.experimental.Accessors;
  77.  
  78. @Accessors(fluent = true)
  79. public class B implements HasName<B>, HasFriendship {
  80.  
  81.     @Getter @Setter
  82.     String name;
  83.  
  84.     public B(String name) {
  85.         this.name = name;
  86.     }
  87.  
  88.     Friendship<B, A> fa = new Friendship<>(this, A.class);
  89.  
  90.     @Override
  91.     public Friendship<B, A> friendship(Class friendClass) {
  92.         if (friendClass.isAssignableFrom(A.class))
  93.             return fa;
  94.         return null;
  95.     }
  96. }
  97.  
  98. ////////////////////////////////////////////////////////////////////////////////
  99. package pl.glx.pattern;
  100.  
  101. import lombok.experimental.Accessors;
  102.  
  103. @Accessors(fluent = true)
  104. public class Camera implements HasFriendship {
  105.  
  106.     public final Friendship<Camera, Controller> controller =
  107.         new Friendship<>(this, Controller.class);
  108.  
  109.     @Override
  110.     public Friendship<Camera, Controller> friendship(Class cClass) {
  111.         return controller;
  112.     }
  113. }
  114.  
  115. ////////////////////////////////////////////////////////////////////////////////
  116. package pl.glx.pattern;
  117.  
  118. import lombok.experimental.Accessors;
  119.  
  120. @Accessors(fluent = true)
  121. public class Controller implements HasFriendship {
  122.  
  123.     public final Friendship<Controller, Camera> camera = new Friendship<>(this, Camera.class);
  124.  
  125.     public final Friendship<Controller, Mouse>  mouse  = new Friendship<>(this, Mouse.class);
  126.  
  127.     public Friendship<Controller, ?> friendship(Class friendClass) {
  128.         if (friendClass.isAssignableFrom(Mouse.class))
  129.             return mouse;
  130.         if (friendClass.isAssignableFrom(Camera.class))
  131.             return camera;
  132.         return null;
  133.     }
  134. }
  135.  
  136. ////////////////////////////////////////////////////////////////////////////////
  137. package pl.glx.pattern;
  138.  
  139. import lombok.experimental.Accessors;
  140.  
  141.  
  142. @Accessors(fluent = true)
  143. public class Mouse implements HasFriendship {
  144.  
  145.     public final Friendship<Mouse, Controller> controller = new Friendship<>(this, Controller.class);
  146.  
  147.     @Override
  148.     public Friendship<Mouse, ?> friendship(Class friendClass) {
  149.         return controller;
  150.     }
  151. }
  152.  
  153. ////////////////////////////////////////////////////////////////////////////////
  154. package pl.glx.pattern;
  155.  
  156. import static pl.glx.common.Helpers.*;
  157. import static pl.glx.common.Constants.*;
  158.  
  159.  
  160. public class AB {
  161.  
  162.     static
  163.     private void showObjects(String what, A a1, B b1, A a2, B b2) {
  164.  
  165.         println(charRepeat('-', 20) + SPACE + what);
  166.  
  167.         printf("a1 b: %s", null != a1.fb.get() ? a1.fb.get().name() : null);
  168.         printf("b1 a: %s", null != b1.fa.get() ? b1.fa.get().name() : null);
  169.         printf("a2 b: %s", null != a2.fb.get() ? a2.fb.get().name() : null);
  170.         printf("b2 a: %s", null != b2.fa.get() ? b2.fa.get().name() : null);
  171.     }
  172.  
  173.     public static void main(String[] args) {
  174.         A a1 = new A("a1");
  175.         B b1 = new B("b1");
  176.         A a2 = new A("a2");
  177.         B b2 = new B("b2");
  178.  
  179.         a1.fb.set(b1);
  180.         b2.fa.set(a2);
  181.         showObjects("a1.fb.set(b1); b2.fa.set(a2);", a1, b1, a2, b2);
  182.  
  183.         a1.fb.set(b2);
  184.         showObjects("a1.fb.set(b2);", a1, b1, a2, b2);
  185.  
  186.         b2.fa.set(a2);
  187.         showObjects("b2.fa.set(a2);", a1, b1, a2, b2);
  188.  
  189.         b1.fa.set(null);
  190.         showObjects("b1.fa.set(null);", a1, b1, a2, b2);
  191.  
  192.         a2.fb.set(null);
  193.         showObjects("a2.fb.set(null);", a1, b1, a2, b2);
  194.  
  195.         Camera cam = new Camera();
  196.         Controller ctrl = new Controller();
  197.         Mouse mice = new Mouse();
  198.  
  199.         cam.controller.set(ctrl);
  200.         ctrl.mouse.set(mice);
  201.         assert cam == ctrl.camera.get() : "cam != ctrl.camera.get()";
  202.         assert ctrl == cam.controller.get() : "ctrl != cam.controller.get()";
  203.         assert mice == ctrl.mouse.get() : "mice != ctrl.mouse.get()";
  204.  
  205.         cam.controller.set(null);
  206.         assert null == ctrl.camera.get() : "null != ctrl.camera.get()";
  207.  
  208.     }
  209. }
  210.  
  211. ////////////////////////////////////////////////////////////////////////////////
  212.