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