Advertisement
Divinty2

Mail(4.3.9)

Jul 23rd, 2019
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. public static class UntrustworthyMailWorker implements MailService {
  2.     private final MailService realMailService = new RealMailService();
  3.     private MailService[] mailServices;
  4.     public UntrustworthyMailWorker(MailService[] services){
  5.         mailServices = services;
  6.     }
  7.  
  8.     public MailService getRealMailService(){
  9.         return realMailService;
  10.     }
  11.  
  12.     @Override
  13.     public Sendable processMail(Sendable mail) {
  14.         Sendable processed = mail;
  15.         for (int i = 0; i < mailServices.length; i++) {
  16.             processed = mailServices[i].processMail(processed);
  17.         }
  18.         return realMailService.processMail(mail);
  19.     }
  20. }
  21.  
  22. public static class Spy implements MailService {
  23.     private Logger LOGGER;
  24.     public Spy(Logger logger) {
  25.         LOGGER = logger;
  26.     }
  27.  
  28.     @Override
  29.     public Sendable processMail(Sendable mail) {
  30.         if(mail.getClass() == MailMessage.class) {
  31.             MailMessage mailMessage = (MailMessage) mail;
  32.             String from = mailMessage.getFrom();
  33.             String to = mailMessage.getTo();
  34.             if (from.equals(AUSTIN_POWERS) || to.equals(AUSTIN_POWERS)) {
  35.                 LOGGER.warning("Detected target mail correspondence: from " + from + " to " + to + " \"" + mailMessage.getMessage() + "\"");
  36.             } else {
  37.                 LOGGER.info("Usual correspondence: from " + from + " to " + to + "");
  38.             }
  39.         }
  40.         return mail;
  41.     }
  42. }
  43.  
  44. public static class Thief implements MailService {
  45.     private int minPrice = 0;
  46.     private int stolenPrice = 0;
  47.     public Thief(int minPrice){
  48.         this.minPrice = minPrice;
  49.     }
  50.  
  51.     public int getStolenValue(){
  52.         return stolenPrice;
  53.     }
  54.     @Override
  55.     public Sendable processMail(Sendable mail) {
  56.         if(mail.getClass() == MailPackage.class) {
  57.             Package pac = ((MailPackage)mail).getContent();
  58.             if(pac.getPrice() >= minPrice){
  59.                 stolenPrice += pac.getPrice();
  60.                 mail = new MailPackage(mail.getFrom(), mail.getTo(),new Package("stones instead of " + pac.getContent(), 0));
  61.             }
  62.        }
  63.         return mail;
  64.     }
  65. }
  66.  
  67. public static class IllegalPackageException extends RuntimeException {}
  68. public static class StolenPackageException extends RuntimeException {}
  69.  
  70. public static class Inspector implements MailService {
  71.     @Override
  72.     public Sendable processMail(Sendable mail) {
  73.         if(mail.getClass() == MailPackage.class) {
  74.             Package pac = ((MailPackage)mail).getContent();
  75.             String content = pac.getContent();
  76.             if(content.indexOf("stones instead of ") == 0) {
  77.                 throw new StolenPackageException();
  78.             } else if(content.equals(WEAPONS) || content.equals(BANNED_SUBSTANCE)){
  79.                 throw new IllegalPackageException();
  80.             }
  81.         }
  82.         return mail;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement