Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main.lock;
- public class Main {
- public static void main(String[] args) {
- boolean result = new CompareManager<>(30)
- .ands()
- .moreThan(40)
- .lessOrEqual(30)
- .result(); // false
- boolean result2 = new CompareManager<>(50)
- .ors()
- .lessThan(60)
- .moreThan(100)
- .result(); // true
- }
- }
- class CompareManager<T extends Comparable<T>> {
- private T value;
- public CompareManager(T v) {
- this.value = v;
- }
- public AndComparer<T> ands() {
- return new AndComparer<>(value);
- }
- public OrComparer<T> ors() {
- return new OrComparer<>(value);
- }
- }
- // класс для сравнения 'И'
- class AndComparer<T extends Comparable<T>> {
- private T value;
- private boolean result = true;
- public AndComparer(T value) {
- this.value = value;
- }
- public AndComparer<T> lessThan(T another) {
- if (result) {
- result = value.compareTo(another) < 0;
- }
- return this;
- }
- public AndComparer<T> moreThan(T another) {
- if (result) {
- result = value.compareTo(another) > 0;
- }
- return this;
- }
- public AndComparer<T> moreOrEqual(T another) {
- if (result) {
- result = value.compareTo(another) >= 0;
- }
- return this;
- }
- public AndComparer<T> lessOrEqual(T another) {
- if (result) {
- result = value.compareTo(another) <= 0;
- }
- return this;
- }
- public boolean result() {
- return result;
- }
- }
- // класс для сравнения 'ИЛИ'
- class OrComparer<T extends Comparable<T>> {
- private T value;
- private boolean result = false;
- public OrComparer(T v) {
- this.value = v;
- }
- public OrComparer<T> lessThan(T another) {
- if (!result) {
- result = value.compareTo(another) < 0;
- }
- return this;
- }
- public OrComparer<T> moreThan(T another) {
- if (!result) {
- result = value.compareTo(another) > 0;
- }
- return this;
- }
- public OrComparer<T> moreOrEqual(T another) {
- if (!result) {
- result = value.compareTo(another) >= 0;
- }
- return this;
- }
- public OrComparer<T> lessOrEqual(T another) {
- if (!result) {
- result = value.compareTo(another) <= 0;
- }
- return this;
- }
- public boolean result() {
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement