KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > object > PjNumber


1 package com.etymon.pj.object;
2
3 import java.io.*;
4
5 /**
6    A representation of the PDF number type.
7    @author Nassib Nassar
8 */

9 public class PjNumber
10     extends PjObject {
11
12     /**
13        Creates a number object.
14        @param f the numeric value to initialize this object to.
15     */

16     public PjNumber(float f) {
17         _f = f;
18     }
19
20     /**
21        Returns the floating point value of this object.
22        @return the floating point value of this object.
23     */

24     public float getFloat() {
25         return _f;
26     }
27
28     /**
29        Returns the integer value of this object.
30        @return the integer value of this object.
31     */

32     public int getInt() {
33         return new Float JavaDoc(_f).intValue();
34     }
35
36     /**
37        Returns the long value of this object.
38        @return the long value of this object.
39     */

40     public long getLong() {
41         return new Float JavaDoc(_f).longValue();
42     }
43
44     /**
45        Determines whether this number is an integer.
46        @return true if this number has no fractions.
47     */

48     public boolean isInteger() {
49         return ( ((float)(new Float JavaDoc(_f).intValue())) == _f );
50     }
51
52         /**
53            Writes this number object to a stream in PDF format.
54            @param os the stream to write to.
55            @return the number of bytes written.
56            @exception IOException if an I/O error occurs.
57          */

58         public long writePdf(OutputStream os) throws IOException {
59                 Float JavaDoc ft = new Float JavaDoc(_f);
60                 int x = ft.intValue();
61                 if ((float)x == _f) {
62                         return write(os, new Integer JavaDoc(x));
63                 } else {
64                         return write(os, ft);
65                 }
66         }
67
68     /**
69        Returns a string representation of this number in PDF format.
70        @return the string representation.
71     public String toString() {
72         Float ft = new Float(_f);
73         int x = ft.intValue();
74         if ((float)x == _f) {
75             return new Integer(x).toString();
76         } else {
77             return ft.toString();
78         }
79     }
80      */

81
82     /**
83        Returns a deep copy of this object.
84        @return a deep copy of this object.
85     */

86     public Object JavaDoc clone() {
87         return this;
88     }
89     
90     /**
91        Compares two PjNumber objects for equality.
92        @param obj the reference object to compare to.
93        @return true if this object is the same as obj, false
94        otherwise.
95     */

96     public boolean equals(Object JavaDoc obj) {
97         if (obj == null) {
98             return false;
99         }
100         if (obj instanceof PjNumber) {
101             return (_f == ((PjNumber)obj)._f);
102         } else {
103             return false;
104         }
105     }
106
107     /**
108        Returns a hash code value for the object.
109        @return a hashcode value for this object.
110     */

111     public int hashCode() {
112         return new Float JavaDoc(_f).hashCode();
113     }
114     
115     public static final PjNumber ZERO = new PjNumber(0);
116     public static final PjNumber ONE = new PjNumber(1);
117     public static final PjNumber TWO = new PjNumber(2);
118     
119     private float _f;
120     
121 }
122
Popular Tags