Guest User

Untitled

a guest
Apr 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. Human noWeapon = new Human(null);
  2. Human noMag = new Human(new Weapon(null));
  3. Human hasAll = new Human(new Weapon(new Magazine(2)));
  4.  
  5. Human noWeapon = new Human(null);
  6. Either <String, Human> either2 = new Right <String, Human>(noWeapon);
  7. Right <String, Human> either2_right = (Right<String, Human>) either2;
  8.  
  9. either2_right.getRight().getWeapon().getMag().getCount();
  10.  
  11. abstract class Either<A, B> { }
  12.  
  13. class Left<A, B> extends Either<A, B> {
  14. public A left_value;
  15. public Left(A a)
  16. {
  17. left_value = a;
  18. }
  19.  
  20. public A getLeft(){
  21. return this.left_value;
  22. }
  23.  
  24. public <B2> Either<A,B2> flatMap(final Function<B,Either<A,B2>> f){
  25. return (Either<A,B2>)this;
  26. }
  27.  
  28. public <B2> Either<A,B2> map(final Function<B,B2> f){
  29. return (Either<A,B2>)this;
  30. }
  31. }
  32.  
  33. class Right<A, B> extends Either<A, B> {
  34. public B right_value;
  35. public Right(B b)
  36. {
  37. right_value = b;
  38. }
  39.  
  40. public B getRight(){
  41. return this.right_value;
  42. }
  43.  
  44. public <B2> Either<A,B2> flatMap(final Function<B,Either<A,B2>> f){
  45. return f.apply(right_value);
  46. }
  47.  
  48. public <B2> Either<A,B2> map(final Function<B,B2> f){
  49. return new Right(f.apply(right_value));
  50. }
  51. }
  52.  
  53. class Human {
  54. Weapon w;
  55.  
  56. public Human(Weapon w)
  57. {
  58. this.w = w;
  59. }
  60.  
  61. public Weapon getWeapon()
  62. {
  63. return w;
  64. }
  65. }
  66.  
  67. class Weapon {
  68. Magazine m;
  69.  
  70. public Weapon(Magazine m)
  71. {
  72. this.m = m;
  73. }
  74.  
  75. public Magazine getMag()
  76. {
  77. return m;
  78. }
  79. }
  80.  
  81. class Magazine {
  82. private int count;
  83.  
  84. public Magazine(int c)
  85. {
  86. count = c;
  87. }
  88.  
  89. public int getCount()
  90. {
  91. return count;
  92. }
  93. }
Add Comment
Please, Sign In to add comment