001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - 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: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.cos;
031:
032: /**
033: * This class represents integer numbers in pdf
034: */
035: public class COSInteger extends COSNumber {
036: static public COSInteger create(byte[] bytes, int start, int length) {
037: int result = 0;
038: int end = start + length;
039: boolean negative = false;
040: byte prefix = bytes[start];
041: if (prefix == '+') {
042: start++;
043: } else if (prefix == '-') {
044: negative = true;
045: start++;
046: }
047: for (int i = start; i < end; i++) {
048: result = ((result * 10) + bytes[i]) - '0';
049: }
050: if (negative) {
051: return new COSInteger(-result);
052: }
053: return new COSInteger(result);
054: }
055:
056: static public COSInteger create(int value) {
057: return new COSInteger(value);
058: }
059:
060: /** the integer value represented */
061: private final int value;
062:
063: protected COSInteger(COSObject object) {
064: super (object);
065: COSInteger integer = (COSInteger) object;
066: this .value = integer.value;
067: }
068:
069: protected COSInteger(int newValue) {
070: value = newValue;
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see de.intarsys.pdf.cos.COSObject#accept(de.intarsys.pdf.cos.ICOSObjectVisitor)
077: */
078: public java.lang.Object accept(ICOSObjectVisitor visitor)
079: throws COSVisitorException {
080: return visitor.visitFromInteger(this );
081: }
082:
083: /*
084: * (non-Javadoc)
085: *
086: * @see de.intarsys.pdf.cos.COSObject#asInteger()
087: */
088: public COSInteger asInteger() {
089: return this ;
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see de.intarsys.pdf.cos.COSObject#basicToString()
096: */
097: protected String basicToString() {
098: return String.valueOf(intValue());
099: }
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see de.intarsys.pdf.cos.COSObject#copyBasic()
105: */
106: protected COSObject copyBasic() {
107: return create(intValue());
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see java.lang.Object#equals(java.lang.Object)
114: */
115: public boolean equals(Object o) {
116: if (!(o instanceof COSInteger)) {
117: return false;
118: }
119: return value == ((COSInteger) o).intValue();
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see de.intarsys.pdf.cos.COSNumber#floatValue()
126: */
127: public float floatValue() {
128: return value;
129: }
130:
131: /*
132: * (non-Javadoc)
133: *
134: * @see java.lang.Object#hashCode()
135: */
136: public int hashCode() {
137: return value;
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see de.intarsys.pdf.cos.COSNumber#intValue()
144: */
145: public int intValue() {
146: return value;
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see de.intarsys.tools.objectsession.ISaveStateSupport#saveState()
153: */
154: public Object saveState() {
155: return new COSInteger(this);
156: }
157: }
|