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 org.pdfbox.exceptions.COSVisitorException;
036:
037: /**
038: *
039: * This class represents an integer number in a PDF document.
040: *
041: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
042: * @version $Revision: 1.12 $
043: */
044: public class COSInteger extends COSNumber {
045:
046: private long value;
047:
048: /**
049: * constructor.
050: *
051: * @param val The integer value of this object.
052: */
053: public COSInteger(long val) {
054: value = val;
055: }
056:
057: /**
058: * constructor.
059: *
060: * @param val The integer value of this object.
061: */
062: public COSInteger(int val) {
063: this ((long) val);
064: }
065:
066: /**
067: * This will create a new PDF Int object using a string.
068: *
069: * @param val The string value of the integer.
070: *
071: * @throws IOException If the val is not an integer type.
072: */
073: public COSInteger(String val) throws IOException {
074: try {
075: value = Long.parseLong(val);
076: } catch (NumberFormatException e) {
077: throw new IOException(
078: "Error: value is not an integer type actual='"
079: + val + "'");
080: }
081: }
082:
083: /**
084: * {@inheritDoc}
085: */
086: public boolean equals(Object o) {
087: return o instanceof COSInteger
088: && ((COSInteger) o).intValue() == intValue();
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: public int hashCode() {
095: //taken from java.lang.Long
096: return (int) (value ^ (value >> 32));
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public String toString() {
103: return "COSInt{" + value + "}";
104: }
105:
106: /**
107: * Change the value of this reference.
108: *
109: * @param newValue The new value.
110: */
111: public void setValue(long newValue) {
112: value = newValue;
113: }
114:
115: /**
116: * polymorphic access to value as float.
117: *
118: * @return The float value of this object.
119: */
120: public float floatValue() {
121: return value;
122: }
123:
124: /**
125: * polymorphic access to value as float.
126: *
127: * @return The double value of this object.
128: */
129: public double doubleValue() {
130: return value;
131: }
132:
133: /**
134: * Polymorphic access to value as int
135: * This will get the integer value of this object.
136: *
137: * @return The int value of this object,
138: */
139: public int intValue() {
140: return (int) value;
141: }
142:
143: /**
144: * Polymorphic access to value as int
145: * This will get the integer value of this object.
146: *
147: * @return The int value of this object,
148: */
149: public long longValue() {
150: return value;
151: }
152:
153: /**
154: * visitor pattern double dispatch method.
155: *
156: * @param visitor The object to notify when visiting this object.
157: * @return any object, depending on the visitor implementation, or null
158: * @throws COSVisitorException If an error occurs while visiting this object.
159: */
160: public Object accept(ICOSVisitor visitor)
161: throws COSVisitorException {
162: return visitor.visitFromInt(this );
163: }
164:
165: /**
166: * This will output this string as a PDF object.
167: *
168: * @param output The stream to write to.
169: * @throws IOException If there is an error writing to the stream.
170: */
171: public void writePDF(OutputStream output) throws IOException {
172: output.write(String.valueOf(value).getBytes());
173: }
174: }
|