View Javadoc

1   /*
2      Copyright 2015 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue.law;
17  
18  import java.util.function.BiFunction;
19  
20  /**
21   * Represents two values of the same type that are expected to be equal.
22   *
23   */
24  public final class IsEq<A> {
25  
26    private final A lhs;
27  
28    private final A rhs;
29  
30    /**
31     * Build an equality instance
32     *
33     * @param lhs first value to compare
34     * @param rhs second value to compare
35     */
36    public IsEq(final A lhs, final A rhs) {
37      this.lhs = lhs;
38      this.rhs = rhs;
39    }
40  
41    /**
42     * Function to use for comparison of two equal elements
43     *
44     * @param cases a {@link java.util.function.BiFunction} taking two values of
45     * this equality class
46     * @param <R> result type
47     * @return a R object.
48     */
49    public <R> R match(final BiFunction<A, A, R> cases) {
50      return cases.apply(lhs, rhs);
51    }
52  
53    /**
54     * Access the left element
55     *
56     * @return left hand side,
57     */
58    public A lhs() {
59      return lhs;
60    }
61  
62    /**
63     * Access the right element
64     *
65     * @return right hand side,
66     */
67    public A rhs() {
68      return rhs;
69    }
70  
71    /**
72     * Two equal elements
73     *
74     * @param lhs an A
75     * @param rhs an A
76     * @param <A> equality type
77     * @return a {@link io.atlassian.fugue.law.IsEq} instance
78     */
79    public static <A> IsEq<A> isEq(final A lhs, final A rhs) {
80      return new IsEq<>(lhs, rhs);
81    }
82  }