001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.cos;
031:
032: import java.io.IOException;
033: import java.io.OutputStream;
034:
035: import java.text.DecimalFormat;
036: import java.text.DecimalFormatSymbols;
037: import java.text.NumberFormat;
038:
039: import org.pdfbox.exceptions.COSVisitorException;
040:
041: /**
042: * This class represents a floating point number in a PDF document.
043: *
044: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
045: * @version $Revision: 1.17 $
046: */
047: public class COSFloat extends COSNumber {
048: private float value;
049:
050: /**
051: * Constructor.
052: *
053: * @param aFloat The primitive float object that this object wraps.
054: */
055: public COSFloat(float aFloat) {
056: value = aFloat;
057: }
058:
059: /**
060: * Constructor.
061: *
062: * @param aFloat The primitive float object that this object wraps.
063: *
064: * @throws IOException If aFloat is not a float.
065: */
066: public COSFloat(String aFloat) throws IOException {
067: try {
068: value = Float.parseFloat(aFloat);
069: } catch (NumberFormatException e) {
070: throw new IOException(
071: "Error expected floating point number actual='"
072: + aFloat + "'");
073: }
074: }
075:
076: /**
077: * Set the value of the float object.
078: *
079: * @param floatValue The new float value.
080: */
081: public void setValue(float floatValue) {
082: value = floatValue;
083: }
084:
085: /**
086: * The value of the float object that this one wraps.
087: *
088: * @return The value of this object.
089: */
090: public float floatValue() {
091: return value;
092: }
093:
094: /**
095: * The value of the double object that this one wraps.
096: *
097: * @return The double of this object.
098: */
099: public double doubleValue() {
100: return value;
101: }
102:
103: /**
104: * This will get the integer value of this object.
105: *
106: * @return The int value of this object,
107: */
108: public long longValue() {
109: return (long) value;
110: }
111:
112: /**
113: * This will get the integer value of this object.
114: *
115: * @return The int value of this object,
116: */
117: public int intValue() {
118: return (int) value;
119: }
120:
121: /**
122: * {@inheritDoc}
123: */
124: public boolean equals(Object o) {
125: return o instanceof COSFloat
126: && Float.floatToIntBits(((COSFloat) o).value) == Float
127: .floatToIntBits(value);
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public int hashCode() {
134: return Float.floatToIntBits(value);
135: }
136:
137: /**
138: * {@inheritDoc}
139: */
140: public String toString() {
141: return "COSFloat{" + value + "}";
142: }
143:
144: /**
145: * visitor pattern double dispatch method.
146: *
147: * @param visitor The object to notify when visiting this object.
148: * @return any object, depending on the visitor implementation, or null
149: * @throws COSVisitorException If an error occurs while visiting this object.
150: */
151: public Object accept(ICOSVisitor visitor)
152: throws COSVisitorException {
153: return visitor.visitFromFloat(this );
154: }
155:
156: /**
157: * This will output this string as a PDF object.
158: *
159: * @param output The stream to write to.
160: * @throws IOException If there is an error writing to the stream.
161: */
162: public void writePDF(OutputStream output) throws IOException {
163: DecimalFormat formatDecimal = (DecimalFormat) NumberFormat
164: .getNumberInstance();
165: formatDecimal.setMaximumFractionDigits(10);
166: formatDecimal.setGroupingUsed(false);
167: DecimalFormatSymbols symbols = formatDecimal
168: .getDecimalFormatSymbols();
169: symbols.setDecimalSeparator('.');
170: formatDecimal.setDecimalFormatSymbols(symbols);
171: output.write(formatDecimal.format(value).getBytes());
172: }
173: }
|