001: /**
002: * Copyright (c) 2003, 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:
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: /**
038: * This class represents an abstract number in a PDF document.
039: *
040: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
041: * @version $Revision: 1.10 $
042: */
043: public abstract class COSNumber extends COSBase {
044: /**
045: * ZERO.
046: */
047: public static final COSInteger ZERO = new COSInteger(0);
048: /**
049: * ONE.
050: */
051: public static final COSInteger ONE = new COSInteger(1);
052: private static final Map COMMON_NUMBERS = new HashMap();
053:
054: static {
055: COMMON_NUMBERS.put("0", ZERO);
056: COMMON_NUMBERS.put("1", ONE);
057: }
058:
059: /**
060: * This will get the float value of this number.
061: *
062: * @return The float value of this object.
063: */
064: public abstract float floatValue();
065:
066: /**
067: * This will get the double value of this number.
068: *
069: * @return The double value of this number.
070: */
071: public abstract double doubleValue();
072:
073: /**
074: * This will get the integer value of this number.
075: *
076: * @return The integer value of this number.
077: */
078: public abstract int intValue();
079:
080: /**
081: * This will get the long value of this number.
082: *
083: * @return The long value of this number.
084: */
085: public abstract long longValue();
086:
087: /**
088: * This factory method will get the appropriate number object.
089: *
090: * @param number The string representation of the number.
091: *
092: * @return A number object, either float or int.
093: *
094: * @throws IOException If the string is not a number.
095: */
096: public static COSNumber get(String number) throws IOException {
097: COSNumber result = (COSNumber) COMMON_NUMBERS.get(number);
098: if (result == null) {
099: if (number.indexOf('.') >= 0) {
100: result = new COSFloat(number);
101: } else {
102: result = new COSInteger(number);
103: }
104: }
105: return result;
106: }
107: }
|