View Javadoc

1   /*
2    ComparableResult.java
3    Creation date : 01/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.common.utils.comparable;
26  
27  /**
28   * This enumeration type lists comparable results, i.e. results which are returned by the
29   * {@link java.lang.Comparable#compareTo} method.
30   *
31   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
32   * @since 1.3.0
33   * @version 1.3.0
34   */
35  public enum ComparableResult {
36  
37      /** The "before" comparable result. */
38      BEFORE(-1, 1),
39      /** The "after" comparable result. */
40      AFTER(1, -1),
41      /** The "equal" comparable result. */
42      EQUAL(0, 0);
43  
44      /**
45       * Gets the {@link ComparableResult} instance following a comparable value.
46       *
47       * @param comparableResult
48       *            the comparable value to build the {@link ComparableResult} instance from.
49       * @return the {@link ComparableResult} instance associated to the passed comparable value.
50       * @since 1.3.0
51       */
52      public static ComparableResult valueOf(final int comparableResult) {
53          ComparableResult result = null;
54  
55          if (comparableResult == 0) {
56              result = EQUAL;
57          } else if (comparableResult < 0) {
58              result = BEFORE;
59          } else if (comparableResult > 0) {
60              result = AFTER;
61          }
62  
63          return result;
64      }
65  
66      /**
67       * The integer value which is returned by the {@link java.lang.Comparable#compareTo} method to
68       * tell if the current instance is ordered before, after or is the same.
69       */
70      private int result;
71  
72      /**
73       * The integer value which is the logical reverse of the returned value by the
74       * {@link java.lang.Comparable#compareTo} method.
75       */
76      private int reverseResult;
77  
78      /**
79       * Default constructor with the result value.
80       *
81       * @param comparableResult
82       *            The integer value of the result to return.
83       * @param reverseValue
84       *            The integer value of the reverse result to return.
85       * @since 1.3.0
86       */
87      private ComparableResult(final int comparableResult, final int reverseValue) {
88          setResult(comparableResult);
89          setReverseResult(reverseValue);
90      }
91  
92      /**
93       * Gets the value of result.
94       *
95       * @return the value of result.
96       * @since 1.3.0
97       */
98      public int getResult() {
99          return result;
100     }
101 
102     /**
103      * Gets the reverse of the result, i.e. BEFORE if it is AFTER, AFTER if it is BEFORE, and EQUAL
104      * if it is EQUAL.
105      *
106      * @return the reverse of the normal result.
107      * @since 1.3.0
108      */
109     public int getReverseResult() {
110         return reverseResult;
111     }
112 
113     /**
114      * Sets the value of result.
115      *
116      * @param value
117      *            the result to set.
118      * @see #getResult()
119      * @since 1.3.0
120      */
121     private void setResult(final int value) {
122         result = value;
123     }
124 
125     /**
126      * Sets the value of reverseResult.
127      *
128      * @param value
129      *            the reverseResult to set.
130      * @see #getReverseResult()
131      * @since 1.3.0
132      */
133     private void setReverseResult(final int value) {
134         reverseResult = value;
135     }
136 
137     /**
138      * {@inheritDoc}
139      *
140      * @since 1.3.0
141      */
142     @Override
143     public String toString() {
144         final String str = super.toString();
145         return str.toLowerCase();
146     }
147 }