Advertisement
Guest User

Untitled

a guest
Jul 19th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. interface Email{ //( 1)
  2. public String getFrom(); //( 2)
  3. public String getMessage(); //( 3)
  4. } //( 4)
  5. // ############################################## //( 5)
  6. class TextEmail implements Email{ //( 6)
  7. private String from, message; //( 7)
  8. public TextEmail(String from, String message){ //( 8)
  9. this.from = from; //( 9)
  10. this.message = message; //(10)
  11. } //(11)
  12. public String getFrom() { return from; } //(12)
  13. public String getMessage(){ return message; } //(13)
  14. } //(14)
  15. // ############################################## //(15)
  16. class Filter{ //(16)
  17. public boolean check(Email r) {return true;} //(17)
  18. public void process(Email e){} //(18)
  19. } //(19)
  20. // ############################################## //(20)
  21. class PatternFilter extends Filter{ //(21)
  22. private String pattern; //(22)
  23. public PatternFilter(String pattern){ //(23)
  24. this.pattern = pattern; //(24)
  25. } //(25)
  26. public boolean check(Email e){ //(26)
  27. return (e.getFrom() + e.getMessage()). //(27)
  28. matches(pattern); //(28)
  29. } //(29)
  30. } //(30)
  31. // ############################################## //(31)
  32. class FolderFilter extends PatternFilter{ //(32)
  33. private String folder; //(33)
  34. public FolderFilter(String f,String p){ //(34)
  35. super(p); //(35)
  36. this.folder = f; //(36)
  37. } //(37)
  38. public void process(Email e){ //(38)
  39. System.out.println("Moved to "+folder); //(39)
  40. } //(40)
  41. } //(41)
  42. // ############################################## //(42)
  43. class PriorityFilter extends PatternFilter{ //(43)
  44. public PriorityFilter(String p){ //(44)
  45. super(p); //(45)
  46. } //(46)
  47. public void process(Email e){ //(47)
  48. System.out.println("Marked as important"); //(48)
  49. } //(49)
  50. } //(50)
  51. // ############################################## //(51)
  52. class GroupFilter extends Filter{ //(52)
  53. class Node{ //(53)
  54. Filter filter; //(54)
  55. Node next; //(55)
  56. Node(Filter filter, Node next){ //(56)
  57. this.filter = filter; //(57)
  58. this.next = next; //(58)
  59. } //(59)
  60. } //(60)
  61. Node node; //(61)
  62. public boolean check(Email e){ //(62)
  63. Node n = this.node; //(63)
  64. while(n!=null){ //(64)
  65. if(n.filter.check(e)) return true; //(65)
  66. n = n.next; //(66)
  67. } //(67)
  68. return false; //(68)
  69. } //(69)
  70. public void process(Email e){ //(70)
  71. Node n = this.node; //(71)
  72. boolean r = false; //(72)
  73. while(n!=null){ //(73)
  74. if(n.filter.check(e)){ //(74)
  75. n.filter.process(e); //(75)
  76. r = true; //(76)
  77. } //(77)
  78. n = n.next; //(78)
  79. } //(79)
  80. if(!r) throw new RuntimeException( //(80)
  81. "Cannot process message"); //(81)
  82. } //(82)
  83. public void setNext(Filter filter){ //(83)
  84. this.node = new Node(filter,this.node); //(84)
  85. } //(85)
  86. } //(86)
  87. // ############################################## //(87)
  88. class EmailProcessor{ //(88)
  89. GroupFilter filter = new GroupFilter(); //(89)
  90. public void addFilter(Filter filter){ //(90)
  91. this.filter.setNext(filter); //(91)
  92. } //(92)
  93. public void process(Email mail){ //(93)
  94. System.out.println("%%%->\nNew message from " //(94)
  95. + mail.getFrom() + "\nMessage: " //(95)
  96. + mail.getMessage()); //(96)
  97. filter.process(mail); //(97)
  98. } //(98)
  99. } //(99)
  100. // ############################################## //(100)
  101. class Test{ //(101)
  102. public static EmailProcessor init(){ //(102)
  103. EmailProcessor p = new EmailProcessor(); //(103)
  104. Filter[] filters = { //(104)
  105. new Filter(), //(105)
  106. new FolderFilter("work","(.*)company(.*)"), //(106)
  107. new PriorityFilter("(.*)money(.*)") }; //(107)
  108. for(Filter f : filters) p.addFilter(f); //(108)
  109. return p; //(109)
  110. } //(110)
  111. public static void main(String[] args){ //(111)
  112. try{ //(112)
  113. EmailProcessor p = init(); //(113)
  114. Email m = new TextEmail( //(114)
  115. "noone@gmail.com","You won a lot of money!"); //(115)
  116. Email b = new TextEmail( //(116)
  117. "boss@company.com","You got promoted!"); //(117)
  118. Email h = new TextEmail( //(118)
  119. "hater@facebook.com","I like you!"); //(119)
  120. Email[] inbox = {m,b,h}; //(120)
  121. for(Email mail : inbox) p.process(mail); //(121)
  122. p.process(new Email(){ //(122)
  123. public String getFrom(){ return "#@#"; } //(123)
  124. public String getMessage(){return "?!?";}}); //(124)
  125. }catch(Exception e){ //(125)
  126. System.out.println(e.getMessage()); //(126)
  127. } //(127)
  128. } //(128)
  129. } //(129)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement