1|package com.infocom.print;
  2|
  3|/**
  4| * Class: PFUnit <p>
  5| *
  6| * <p>
  7| * Base class for the measurment system in the print framework.
  8| * When adding a new measurement unit to the framework you must
  9| * extend this class and implement the <code>getPoint ()</code>.<p>
 10| *
 11| * @author Jean-Pierre Dube <jpdube@videotron.ca>
 12| * @version 1.0
 13| * @since 1.0
 14| * @see Cloneable
 15| * Created: Wed Sep 27 14:45:50 2000
 16| */
 17|
 18|
 19|public abstract class PFUnit implements Cloneable {
 20|
 21|   //--- Private instances declarations
 22|   private double units = 0.0;
 23|   private double points = 0.0;
 24|
 25|
 26|   /**
 27|    * Constructor: PFUnit <p>
 28|    *
 29|    */
 30|   public PFUnit () {
 31|
 32|   }
 33|
 34|
 35|   /**
 36|    * Constructor: PFUnit <p>
 37|    *
 38|    * @param parUnits a value of type double
 39|    */
 40|   public PFUnit (double parUnits) {
 41|
 42|      units = parUnits;
 43|   }
 44|
 45|
 46|   /**
 47|    * Method: setUnits <p>
 48|    *
 49|    * Set the unit value, using the implemented unit of measurment.
 50|    *
 51|    * @param parValue a value of type double
 52|    */
 53|   public void setUnits (double parUnits) {
 54|
 55|      units = parUnits;
 56|
 57|   }
 58|
 59|
 60|   /**
 61|    * Method: getUnits <p>
 62|    *
 63|    * Get the units in the implemented measurement system.
 64|    *
 65|    * @return a value of type double
 66|    */
 67|   public double getUnits () {
 68|
 69|      return (units);
 70|
 71|   }
 72|
 73|
 74|   /**
 75|    * Method: getPoints <p>
 76|    *
 77|    * This method is in charge of converting the unit value in the
 78|    * current measurement system to points. There is 72 points per inch.
 79|    *
 80|    * @return a value of type double
 81|    */
 82|   public abstract double getPoints ();
 83|
 84|
 85|
 86|   /**
 87|    * Method: add <p>
 88|    *
 89|    * Add another measurement system value to this unit.
 90|    *
 91|    * @param parUnit a value of type PFUnit
 92|    */
 93|   public PFUnit add (PFUnit parUnits) {
 94|
 95|      PFUnit tempUnit = (PFUnit) clone ();
 96|
 97|      tempUnit.setUnits (tempUnit.getUnits () + parUnits.getUnits ());
 98|
 99|      return (tempUnit);
100|   }
101|
102|
103|   /**
104|    * Method: add <p>
105|    *
106|    * Add a <code>double</code> value to this unit. Note
107|    * that the value passed in parameters is assumed to be
108|    * in the current units defined by this object.
109|    *
110|    * @param parUnits a value of type PFUnit
111|    */
112|   public PFUnit add (double parUnits) {
113|
114|      PFUnit tempUnit = (PFUnit) clone ();
115|
116|      tempUnit.setUnits (this.getUnits () + parUnits);
117|
118|      return (tempUnit);
119|   }
120|
121|
122|   /**
123|    * Method: substract <p>
124|    *
125|    * Substract another measurement system value to this unit.
126|    *
127|    * @param parUnit a value of type PFUnit
128|    */
129|   public PFUnit substract (PFUnit parUnits) {
130|
131|      PFUnit tempUnit = (PFUnit) clone ();
132|
133|      tempUnit.setUnits (tempUnit.getUnits () - parUnits.getUnits ());
134|
135|      return (tempUnit);
136|   }
137|
138|
139|   /**
140|    * Method: substract <p>
141|    *
142|    * Substract a <code>double</code> value to this unit. Note
143|    * that the value passed in parameters is assumed to be
144|    * in the current units defined by this object.
145|    *
146|    * @param parUnits a value of type PFUnit
147|    * @return a value of type PFUnit
148|    */
149|   public PFUnit substract (double parUnits) {
150|
151|      PFUnit tempUnit = (PFUnit) clone ();
152|
153|      tempUnit.setUnits (this.getUnits () - parUnits);
154|
155|      return (tempUnit);
156|   }
157|
158|
159|   /**
160|    * Method: multiply <p>
161|    *
162|    * Multiply another measurement system value to this unit.
163|    *
164|    * @param parUnits a value of type PFUnit
165|    */
166|   public PFUnit multiply (PFUnit parUnits) {
167|
168|      PFUnit tempUnit = (PFUnit) clone ();
169|
170|      tempUnit.setUnits (tempUnit.getUnits () * parUnits.getUnits ());
171|
172|      return (tempUnit);
173|   }
174|
175|
176|   /**
177|    * Method: multiply <p>
178|    *
179|    * Multiply a <code>double</code> value to this unit. Note
180|    * that the value passed in parameters is assumed to be
181|    * in the current units defined by this object.
182|    *
183|    * @param parUnits a value of type double
184|    */
185|   public PFUnit multiply (double parUnits) {
186|
187|      PFUnit tempUnit = (PFUnit) clone ();
188|
189|      tempUnit.setUnits (this.getUnits () * parUnits);
190|
191|      return (tempUnit);
192|   }
193|
194|
195|   /**
196|    * Method: divide <p>
197|    *
198|    * Divide another measurement system value to this unit.
199|    *
200|    * Note: A denominator with a zero value will be trapped
201|    * and a <code>null</code> value will be returned.
202|    *
203|    * @param parUnits a value of type PFUnit
204|    */
205|   public PFUnit divide (PFUnit parUnits) {
206|
207|      if (parUnits.getUnits () != 0) {
208|         PFUnit tempUnit = (PFUnit) clone ();
209|         tempUnit.setUnits (tempUnit.getUnits () / parUnits.getUnits ());
210|         return (tempUnit);
211|      }
212|      else
213|         return (null);
214|   }
215|
216|
217|   /**
218|    * Method: divide <p>
219|    *
220|    * Divide a <code>double</code> value to this unit. Note
221|    * that the value passed in parameters is assumed to be
222|    * in the current units defined by this object.
223|    *
224|    * Note: A denominator with a zero value will be trapped
225|    * and a <code>null</code> value will be returned.
226|    *
227|    * @param parUnits a value of type double
228|    */
229|   public PFUnit divide (double parUnits) {
230|
231|
232|      if (parUnits != 0) {
233|         PFUnit tempUnit = (PFUnit) clone ();
234|         tempUnit.setUnits (tempUnit.getUnits () / parUnits);
235|         return (tempUnit);
236|      }
237|      else
238|         return (null);
239|
240|   }
241|
242|
243|   /**
244|    * Method: equal <p>
245|    *
246|    * @param parUnit a value of type Object
247|    * @return a value of type boolean
248|    */
249|   public boolean equals (Object parUnit) {
250|
251|      if (parUnit instanceof PFUnit)
252|         if (((PFUnit) parUnit).getUnits () == this.getUnits ())
253|            return (true);
254|
255|      return (false);
256|
257|   }
258|
259|
260|   /**
261|    * Method: clone <p>
262|    *
263|    * @return a value of type Object
264|    */
265|   public Object clone () {
266|
267|      try {
268|         return ((PFUnit) super.clone ());
269|      }
270|      catch (CloneNotSupportedException cnse) {
271|         cnse.printStackTrace ();
272|         return (null);
273|      }
274|
275|   }
276|
277|
278|   /**
279|    * Method: toString <p>
280|    *
281|    * @return a value of type String
282|    */
283|   public String toString () {
284|
285|      return ("Units=" + units + ": Points=" + points);
286|
287|   }
288|}// PFUnit