View Javadoc

1   /*
2    * $Id: Fraction.java,v 1.3 2011/07/29 20:31:40 oboehm Exp $
3    *
4    * Copyright (c) 2008 by Oliver Boehm
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   * (c)reated 19.10.2008 by oliver (ob@oasd.de)
19   */
20  package patterntesting.sample;
21  
22  import java.math.BigDecimal;
23  
24  import org.slf4j.*;
25  
26  import patterntesting.runtime.annotation.*;
27  
28  /**
29   * This is a litte demo class to show how to use the @UnsupportedOperation
30   * annotation.
31   *
32   * @author <a href="boehm@javatux.de">oliver</a>
33   * @since 19.10.2008
34   * @version $Revision: 1.3 $
35   */
36  public final class Fraction implements Comparable<Fraction> {
37  
38      private static final Logger log = LoggerFactory.getLogger(Fraction.class);
39      private final int counter;
40      private final int denominator;
41  
42      /**
43       * Instantiates a new fraction.
44       *
45       * @param counter the counter
46       * @param denominator the denominator
47       */
48      public Fraction(final int counter, final int denominator) {
49          this.counter = counter;
50          if (denominator == 0) {
51              throw new IllegalArgumentException("illegal fraction x/0");
52          }
53          this.denominator = denominator;
54      }
55  
56      /**
57       * Checks if is negative.
58       *
59       * @return true, if is negative
60       */
61      public boolean isNegative() {
62          return this.toBigDecimal().compareTo(BigDecimal.ZERO) < 0;
63      }
64  
65      /**
66       * This is an example of an automatically generated method by Eclipse.
67       * If you extends this class by the Comparable interface but haven't
68       * implement the compareTo method you can ask Eclipse to do that by
69       * the quick fix "Add unimplemented methods".
70       * <br/>
71       * The problem with such generated default implementation is that
72       * the result may look ok but you can't rely on it. For this reason you
73       * should throw an RuntimeException or something else if you have not
74       * implement it. This is where the @NotYetImplemented annotation
75       * comes in.
76       *
77       * @param o the o
78       *
79       * @return the int
80       */
81      @NotYetImplemented
82      public int compareTo(final Fraction o) {
83          // Auto-generated method stub
84          return 0;
85      }
86  
87      /**
88       * Because we have implemented (or want to implement) the
89       * compareTo(..) method we should overwrite the equals method.
90       * @see java.lang.Object#equals(java.lang.Object)
91       *
92       * @param other the other object
93       * @return true if the objects are equal
94       */
95      @Override
96      public boolean equals(final Object other) {
97          try {
98              return compareTo((Fraction) other) == 0;
99          } catch (ClassCastException e) {
100             log.debug("can't compare " + this + " with " + other, e);
101             return false;
102         }
103     }
104 
105     /**
106      * Because we overwrite the equals() method we have also to overwrite
107      * the hashCode() implementation.
108      * @see java.lang.Object#hashCode()
109      *
110      * @return the hash code
111      */
112     @Override
113     public int hashCode() {
114         return this.toString().hashCode();
115     }
116 
117     /**
118      * This is a very simple implementation for toBigDecimal().
119      *
120      * @return the fraction as decimal
121      */
122     public BigDecimal toBigDecimal() {
123         return new BigDecimal(this.counter / (double) this.denominator);
124     }
125 
126     /**
127      * Because of rounding problems this method should be no longer used.
128      * So it is now marked as @UnsupportedOperation.
129      * <br/>
130      * Normally I would mark it also as "@Deprecated". But since it is only
131      * an example I abstain from it.
132      *
133      * @return the double
134      */
135     @UnsupportedOperation
136     public double toDouble() {
137         return this.counter / (double) this.denominator;
138     }
139 
140 	/**
141 	 * To string.
142 	 *
143 	 * @return the string
144 	 * @see java.lang.Object#toString()
145 	 */
146 	@Override
147 	public String toString() {
148 		return this.counter + "/" + this.denominator;
149 	}
150 
151 }