Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.16 KB | None | 0 0
  1. package by.koshko.task01.service.specification;
  2.  
  3. import by.koshko.task01.entity.Plan;
  4. import by.koshko.task01.service.exception.ServiceException;
  5. import by.koshko.task01.service.validation.Validator;
  6.  
  7. import java.util.function.Function;
  8.  
  9. public class FindSpecificationBuilder {
  10.     public enum SearchType {
  11.         /**
  12.          * Search by plan id.
  13.          */
  14.         FindByID,
  15.         /**
  16.          * Search by plan name.
  17.          */
  18.         FindByName,
  19.         /**
  20.          * Search plan with name start with given value.
  21.          */
  22.         NameStartWith,
  23.         /**
  24.          * Search by cost within network.
  25.          */
  26.         FindByCostWN,
  27.         /**
  28.          * Search by cost within network in specified range.
  29.          */
  30.         FindByCostWNBetween,
  31.         /**
  32.          * Search by cost to other networks.
  33.          */
  34.         FindByCostON,
  35.         /**
  36.          * Search by cost to other networks in specified range.
  37.          */
  38.         FindByCostONBetween,
  39.         /**
  40.          * Search by cost abroad.
  41.          */
  42.         FindByCostAbroad,
  43.         /**
  44.          * Search by cost abroad in specified range.
  45.          */
  46.         FindByCostAbroadBetween,
  47.         /**
  48.          * Search by cost sms.
  49.          */
  50.         FindByCostSms,
  51.         /**
  52.          * Search by cost sms in specified range.
  53.          */
  54.         FindByCostSmsBetween,
  55.         /**
  56.          * Search by cost megabyte.
  57.          */
  58.         FindByCostMegabyte,
  59.         /**
  60.          * Search by cost megabyte in specified range.
  61.          */
  62.         FindByCostMegabyteBetween
  63.     }
  64.  
  65.     /**
  66.      * {@code Specification} to be compiled.
  67.      */
  68.     private AbstractFindBySpecification<Plan> spec = null;
  69.  
  70.     /**
  71.      * Builds specifications.
  72.      *
  73.      * @param type Type of specification.
  74.      * @param args Arguments to pass into specification.
  75.      * @throws ServiceException if some arguments for specification is invalid.
  76.      */
  77.     public final void compose(final SearchType type, final String... args)
  78.             throws ServiceException {
  79.         switch (type) {
  80.             case FindByID:
  81.                 findByID(args[0]);
  82.                 break;
  83.             case FindByName:
  84.                 findByName(args[0]);
  85.                 break;
  86.             case NameStartWith:
  87.                 nameStartWith(args[0]);
  88.                 break;
  89.             case FindByCostWN:
  90.                 findByCostWN(args[0]);
  91.                 break;
  92.             case FindByCostWNBetween:
  93.                 System.out.println("HELLO");
  94.                 costBetween(Plan::getOutgoingWithinNetwork, args);
  95.                 break;
  96.             case FindByCostON:
  97.                 findByCostON(args[0]);
  98.                 break;
  99.             case FindByCostONBetween:
  100.                 costBetween(Plan::getOutgoingOtherNetwork, args);
  101.                 break;
  102.             case FindByCostAbroad:
  103.                 findByCostAbr(args[0]);
  104.                 break;
  105.             case FindByCostAbroadBetween:
  106.                 costBetween(Plan::getOutgoingAbroad, args);
  107.                 break;
  108.             case FindByCostSms:
  109.                 findByCostSms(args[0]);
  110.                 break;
  111.             case FindByCostSmsBetween:
  112.                 costBetween(Plan::getSmsCost, args);
  113.                 break;
  114.             case FindByCostMegabyte:
  115.                 findByCostMB(args[0]);
  116.                 break;
  117.             case FindByCostMegabyteBetween:
  118.                 costBetween(Plan::getMegabyteCost, args);
  119.             default:
  120.                 throw new ServiceException("Impossible exception.");
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Returns compiled specification.
  126.      *
  127.      * @return compiled specification.
  128.      */
  129.     public final AbstractFindBySpecification<Plan> build() {
  130.         return spec;
  131.     }
  132.  
  133.     private void findByID(final String arg) throws ServiceException {
  134.         long id = checkLong(arg);
  135.         spec = spec != null ? spec.and(new PlanIdSpecification(id))
  136.                 : new PlanIdSpecification(id);
  137.     }
  138.  
  139.     private void findByName(final String arg) throws ServiceException {
  140.         checkString(arg);
  141.         spec = spec != null ? spec.and(new PlanNameSpecification(arg))
  142.                 : new PlanNameSpecification(arg);
  143.     }
  144.  
  145.     private void nameStartWith(final String arg) throws ServiceException {
  146.         checkString(arg);
  147.         spec = spec != null ? spec.and(new PlanNameStartWithSpecification(arg))
  148.                 : new PlanNameStartWithSpecification(arg);
  149.     }
  150.  
  151.     private void findByCostWN(final String arg) throws ServiceException {
  152.         double val = checkDouble(arg);
  153.         spec = spec != null ? spec.and(new PlanCostInSpecification(val))
  154.                 : new PlanCostInSpecification(val);
  155.     }
  156.  
  157.     private void findByCostON(final String arg) throws ServiceException {
  158.         double val = checkDouble(arg);
  159.         spec = spec != null ? spec.and(new PlanCostOtherSpecification(val))
  160.                 : new PlanCostOtherSpecification(val);
  161.     }
  162.  
  163.     private void findByCostAbr(final String arg) throws ServiceException {
  164.         double val = checkDouble(arg);
  165.         spec = spec != null ? spec.and(new PlanCostAbroadSpecification(val))
  166.                 : new PlanCostAbroadSpecification(val);
  167.     }
  168.  
  169.     private void findByCostSms(final String arg) throws ServiceException {
  170.         double val = checkDouble(arg);
  171.         spec = spec != null ? spec.and(new PlanCostSmsSpecification(val))
  172.                 : new PlanCostSmsSpecification(val);
  173.     }
  174.  
  175.     private void findByCostMB(final String arg) throws ServiceException {
  176.         double val = checkDouble(arg);
  177.         spec = spec != null ? spec.and(new PlanCostMegabyteSpecification(val))
  178.                 : new PlanCostMegabyteSpecification(val);
  179.     }
  180.  
  181.     private void costBetween(final Function<Plan, Double> f,
  182.                              final String... args) throws ServiceException {
  183.         checkLength(args);
  184.         double min = checkDouble(args[0]);
  185.         double max = checkDouble(args[1]);
  186.         AbstractFindBySpecification<Plan> s
  187.                 = new PlanDoubleBetweenSpecification(
  188.                         min, max, f
  189.                 );
  190.         spec = spec != null ? spec.and(s) : s;
  191.     }
  192.  
  193.     private void checkLength(final String[] args) throws ServiceException {
  194.         if (args.length != 2) {
  195.             throw new ServiceException("Not enough arguments."
  196.                     + " Min and max value required");
  197.         }
  198.     }
  199.  
  200.     private long checkLong(final String arg) throws ServiceException {
  201.         if (Validator.validateLong(arg)) {
  202.             return Long.valueOf(arg);
  203.         } else {
  204.             throw new ServiceException("Invalid parameter. Long required.");
  205.         }
  206.     }
  207.  
  208.     private double checkDouble(final String arg) throws ServiceException {
  209.         if (Validator.validateDouble(arg)) {
  210.             return Double.valueOf(arg);
  211.         } else {
  212.             throw new ServiceException("Invalid parameter. Double required.");
  213.         }
  214.     }
  215.  
  216.     private void checkString(final String arg) throws ServiceException {
  217.         if (arg == null || arg.length() == 0) {
  218.             throw new ServiceException("Empty string");
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement