View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ********************************************************************************/
17  package net.sourceforge.jhunters.pool.data;
18  
19  
20  /**
21   * Result of Fixture.
22   */
23  public enum Result {
24      /**
25       * 2-0 win.
26       */
27      TWONIL(2, 0),
28  
29      /**
30       * 2-1 win.
31       */
32      TWOONE(2, 1),
33  
34      /**
35       * 1-2 loss.
36       */
37      ONETWO(1, 2),
38  
39      /**
40       * 0-2 loss.
41       */
42      NILTWO(0, 2);
43  
44      /**
45       * The Display Version.
46       */
47      private String theDisplay;
48  
49      /**
50       * FramesFor.
51       */
52      private int theFramesFor;
53  
54      /**
55       * FramesAgainst.
56       */
57      private int theFramesAgainst;
58  
59      /**
60       * Constructor.
61       * @param pFor the frames for.
62       * @param pAgainst the frames against
63       */
64      private Result(final int pFor, final int pAgainst) {
65          theFramesFor = pFor;
66          theFramesAgainst = pAgainst;
67      }
68  
69      @Override
70      public String toString() {
71          if (theDisplay == null) {
72              theDisplay = buildDisplay();
73          }
74          return theDisplay;
75      }
76  
77      /**
78       * build the display string.
79       * @return the display string
80       */
81      public String buildDisplay() {
82          StringBuilder myBuilder = new StringBuilder();
83          myBuilder.append(theFramesFor);
84          myBuilder.append('-');
85          myBuilder.append(theFramesAgainst);
86          return myBuilder.toString();
87      }
88  
89      /**
90       * Obtain frames for.
91       * @return the frames for.
92       */
93      public int getFramesFor() {
94          return theFramesFor;
95      }
96  
97      /**
98       * Obtain frames against.
99       * @return the frames against.
100      */
101     public int getFramesAgainst() {
102         return theFramesAgainst;
103     }
104 
105     /**
106      * Obtain frames difference.
107      * @return the frames difference.
108      */
109     public int getFramesDifference() {
110         return theFramesFor - theFramesAgainst;
111     }
112 
113     /**
114      * Is this a win?
115      * @return true/false.
116      */
117     public boolean isWin() {
118         return theFramesFor > theFramesAgainst;
119     }
120 
121     /**
122      * Obtain points.
123      * @return the points value
124      */
125     public int getPoints() {
126         switch (this) {
127             case TWONIL:
128             case TWOONE:
129                 return 2;
130             case ONETWO:
131             case NILTWO:
132             default:
133                 return 0;
134         }
135     }
136 
137     /**
138      * reverse value.
139      * @return the reverse value
140      */
141     public Result reverse() {
142         switch (this) {
143             case TWONIL:
144                 return NILTWO;
145             case TWOONE:
146                 return ONETWO;
147             case ONETWO:
148                 return TWOONE;
149             case NILTWO:
150             default:
151                 return TWONIL;
152         }
153     }
154 
155     /**
156      * get value from name.
157      * @param pValue the name
158      * @return the corresponding ENum object
159      */
160     public static Result fromName(final String pValue) {
161         for (Result myType : values()) {
162             if (pValue.equals(myType.toString())) {
163                 return myType;
164             }
165         }
166         throw new IllegalArgumentException("Invalid Result: "
167                 + pValue);
168     }
169 }