KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > cos > COSFloat


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.cos;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 import java.text.DecimalFormat JavaDoc;
37 import java.text.DecimalFormatSymbols JavaDoc;
38 import java.text.NumberFormat JavaDoc;
39
40 import org.pdfbox.exceptions.COSVisitorException;
41
42 /**
43  * This class represents a floating point number in a PDF document.
44  *
45  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
46  * @version $Revision: 1.17 $
47  */

48 public class COSFloat extends COSNumber
49 {
50     private float value;
51
52     /**
53      * Constructor.
54      *
55      * @param aFloat The primitive float object that this object wraps.
56      */

57     public COSFloat( float aFloat )
58     {
59         value = aFloat;
60     }
61
62     /**
63      * Constructor.
64      *
65      * @param aFloat The primitive float object that this object wraps.
66      *
67      * @throws IOException If aFloat is not a float.
68      */

69     public COSFloat( String JavaDoc aFloat ) throws IOException JavaDoc
70     {
71         try
72         {
73             value = Float.parseFloat( aFloat );
74         }
75         catch( NumberFormatException JavaDoc e )
76         {
77             throw new IOException JavaDoc( "Error expected floating point number actual='" +aFloat + "'" );
78         }
79     }
80     
81     /**
82      * Set the value of the float object.
83      *
84      * @param floatValue The new float value.
85      */

86     public void setValue( float floatValue )
87     {
88         value = floatValue;
89     }
90
91     /**
92      * The value of the float object that this one wraps.
93      *
94      * @return The value of this object.
95      */

96     public float floatValue()
97     {
98         return value;
99     }
100
101     /**
102      * The value of the double object that this one wraps.
103      *
104      * @return The double of this object.
105      */

106     public double doubleValue()
107     {
108         return value;
109     }
110
111     /**
112      * This will get the integer value of this object.
113      *
114      * @return The int value of this object,
115      */

116     public long longValue()
117     {
118         return (long)value;
119     }
120
121     /**
122      * This will get the integer value of this object.
123      *
124      * @return The int value of this object,
125      */

126     public int intValue()
127     {
128         return (int)value;
129     }
130
131     /**
132      * {@inheritDoc}
133      */

134     public boolean equals( Object JavaDoc o )
135     {
136         return o instanceof COSFloat && Float.floatToIntBits(((COSFloat)o).value) == Float.floatToIntBits(value);
137     }
138
139     /**
140      * {@inheritDoc}
141      */

142     public int hashCode()
143     {
144         return Float.floatToIntBits(value);
145     }
146
147     /**
148      * {@inheritDoc}
149      */

150     public String JavaDoc toString()
151     {
152         return "COSFloat{" + value + "}";
153     }
154
155     /**
156      * visitor pattern double dispatch method.
157      *
158      * @param visitor The object to notify when visiting this object.
159      * @return any object, depending on the visitor implementation, or null
160      * @throws COSVisitorException If an error occurs while visiting this object.
161      */

162     public Object JavaDoc accept(ICOSVisitor visitor) throws COSVisitorException
163     {
164         return visitor.visitFromFloat(this);
165     }
166     
167     /**
168      * This will output this string as a PDF object.
169      *
170      * @param output The stream to write to.
171      * @throws IOException If there is an error writing to the stream.
172      */

173     public void writePDF( OutputStream JavaDoc output ) throws IOException JavaDoc
174     {
175         DecimalFormat JavaDoc formatDecimal = (DecimalFormat JavaDoc)NumberFormat.getNumberInstance();
176         formatDecimal.setMaximumFractionDigits( 10 );
177         formatDecimal.setGroupingUsed( false );
178         DecimalFormatSymbols JavaDoc symbols = formatDecimal.getDecimalFormatSymbols();
179         symbols.setDecimalSeparator( '.' );
180         formatDecimal.setDecimalFormatSymbols( symbols );
181         output.write(formatDecimal.format( value ).getBytes());
182     }
183 }
Popular Tags