Advertisement
Guest User

Untitled

a guest
May 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package main.lock;
  2.  
  3. public class Main {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         boolean result = new CompareManager<>(30)
  9.                 .ands()
  10.                 .moreThan(40)
  11.                 .lessOrEqual(30)
  12.                 .result(); //      false
  13.        
  14.  
  15.         boolean result2 = new CompareManager<>(50)
  16.                 .ors()
  17.                 .lessThan(60)
  18.                 .moreThan(100)
  19.                 .result(); //      true
  20.        
  21.     }
  22.  
  23. }
  24.  
  25.  
  26. class CompareManager<T extends Comparable<T>> {
  27.  
  28.     private T value;
  29.  
  30.     public CompareManager(T v) {
  31.         this.value = v;
  32.     }
  33.  
  34.     public AndComparer<T> ands() {
  35.         return new AndComparer<>(value);
  36.     }
  37.  
  38.     public OrComparer<T> ors() {
  39.         return new OrComparer<>(value);
  40.     }
  41. }
  42.  
  43. // класс для сравнения 'И'
  44. class AndComparer<T extends Comparable<T>> {
  45.  
  46.     private T value;
  47.     private boolean result = true;
  48.  
  49.     public AndComparer(T value) {
  50.         this.value = value;
  51.     }
  52.  
  53.     public AndComparer<T> lessThan(T another) {
  54.         if (result) {
  55.             result = value.compareTo(another) < 0;
  56.         }
  57.         return this;
  58.     }
  59.  
  60.     public AndComparer<T> moreThan(T another) {
  61.         if (result) {
  62.             result = value.compareTo(another) > 0;
  63.         }
  64.         return this;
  65.     }
  66.  
  67.     public AndComparer<T> moreOrEqual(T another) {
  68.         if (result) {
  69.             result = value.compareTo(another) >= 0;
  70.         }
  71.         return this;
  72.     }
  73.  
  74.     public AndComparer<T> lessOrEqual(T another) {
  75.         if (result) {
  76.             result = value.compareTo(another) <= 0;
  77.         }
  78.         return this;
  79.     }
  80.  
  81.     public boolean result() {
  82.         return result;
  83.     }
  84. }
  85.  
  86.  
  87. // класс для сравнения 'ИЛИ'
  88. class OrComparer<T extends Comparable<T>> {
  89.  
  90.     private T value;
  91.     private boolean result = false;
  92.  
  93.     public OrComparer(T v) {
  94.         this.value = v;
  95.     }
  96.  
  97.     public OrComparer<T> lessThan(T another) {
  98.         if (!result) {
  99.             result = value.compareTo(another) < 0;
  100.         }
  101.         return this;
  102.     }
  103.  
  104.     public OrComparer<T> moreThan(T another) {
  105.         if (!result) {
  106.             result = value.compareTo(another) > 0;
  107.         }
  108.         return this;
  109.     }
  110.  
  111.     public OrComparer<T> moreOrEqual(T another) {
  112.         if (!result) {
  113.             result = value.compareTo(another) >= 0;
  114.         }
  115.         return this;
  116.     }
  117.  
  118.     public OrComparer<T> lessOrEqual(T another) {
  119.         if (!result) {
  120.             result = value.compareTo(another) <= 0;
  121.         }
  122.         return this;
  123.     }
  124.  
  125.     public boolean result() {
  126.         return result;
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement