KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.pdfbox.exceptions.COSVisitorException;
37
38 /**
39  *
40  * This class represents an integer number in a PDF document.
41  *
42  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
43  * @version $Revision: 1.12 $
44  */

45 public class COSInteger extends COSNumber
46 {
47
48     private long value;
49
50     /**
51      * constructor.
52      *
53      * @param val The integer value of this object.
54      */

55     public COSInteger( long val )
56     {
57         value = val;
58     }
59
60     /**
61      * constructor.
62      *
63      * @param val The integer value of this object.
64      */

65     public COSInteger( int val )
66     {
67         this( (long)val );
68     }
69
70     /**
71      * This will create a new PDF Int object using a string.
72      *
73      * @param val The string value of the integer.
74      *
75      * @throws IOException If the val is not an integer type.
76      */

77     public COSInteger( String JavaDoc val ) throws IOException JavaDoc
78     {
79         try
80         {
81             value = Long.parseLong( val );
82         }
83         catch( NumberFormatException JavaDoc e )
84         {
85             throw new IOException JavaDoc( "Error: value is not an integer type actual='" + val + "'" );
86         }
87     }
88
89     /**
90      * {@inheritDoc}
91      */

92     public boolean equals(Object JavaDoc o)
93     {
94         return o instanceof COSInteger && ((COSInteger)o).intValue() == intValue();
95     }
96
97     /**
98      * {@inheritDoc}
99      */

100     public int hashCode()
101     {
102         //taken from java.lang.Long
103
return (int)(value ^ (value >> 32));
104     }
105
106     /**
107      * {@inheritDoc}
108      */

109     public String JavaDoc toString()
110     {
111         return "COSInt{" + value + "}";
112     }
113     
114     /**
115      * Change the value of this reference.
116      *
117      * @param newValue The new value.
118      */

119     public void setValue( long newValue )
120     {
121         value = newValue;
122     }
123
124     /**
125      * polymorphic access to value as float.
126      *
127      * @return The float value of this object.
128      */

129     public float floatValue()
130     {
131         return value;
132     }
133
134     /**
135      * polymorphic access to value as float.
136      *
137      * @return The double value of this object.
138      */

139     public double doubleValue()
140     {
141         return value;
142     }
143
144     /**
145      * Polymorphic access to value as int
146      * This will get the integer value of this object.
147      *
148      * @return The int value of this object,
149      */

150     public int intValue()
151     {
152         return (int)value;
153     }
154
155     /**
156      * Polymorphic access to value as int
157      * This will get the integer value of this object.
158      *
159      * @return The int value of this object,
160      */

161     public long longValue()
162     {
163         return value;
164     }
165
166     /**
167      * visitor pattern double dispatch method.
168      *
169      * @param visitor The object to notify when visiting this object.
170      * @return any object, depending on the visitor implementation, or null
171      * @throws COSVisitorException If an error occurs while visiting this object.
172      */

173     public Object JavaDoc accept(ICOSVisitor visitor) throws COSVisitorException
174     {
175         return visitor.visitFromInt(this);
176     }
177     
178     /**
179      * This will output this string as a PDF object.
180      *
181      * @param output The stream to write to.
182      * @throws IOException If there is an error writing to the stream.
183      */

184     public void writePDF( OutputStream JavaDoc output ) throws IOException JavaDoc
185     {
186         output.write(String.valueOf(value).getBytes());
187     }
188 }
Popular Tags