Home » pdfbox-1.1.0-src » org.apache.pdfbox.cos » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.cos;
   18   
   19   import java.io.IOException;
   20   import java.io.OutputStream;
   21   
   22   import org.apache.pdfbox.exceptions.COSVisitorException;
   23   
   24   /**
   25    * This class represents an integer number in a PDF document.
   26    *
   27    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   28    * @version $Revision: 1.12 $
   29    */
   30   public class COSInteger extends COSNumber
   31   {
   32   
   33       /**
   34        * The lowest integer to be kept in the {@link #STATIC} array.
   35        */
   36       private static int LOW = -100;
   37   
   38       /**
   39        * The highest integer to be kept in the {@link #STATIC} array.
   40        */
   41       private static int HIGH = 256;
   42   
   43       /**
   44        * Static instances of all COSIntegers in the range from {@link #LOW}
   45        * to {@link #HIGH}.
   46        */
   47       private static final COSInteger[] STATIC = new COSInteger[HIGH - LOW + 1];
   48   
   49       /**
   50        * Constant for the number zero.
   51        * @since Apache PDFBox 1.1.0
   52        */
   53       public static final COSInteger ZERO = get(0); 
   54   
   55       /**
   56        * Constant for the number one.
   57        * @since Apache PDFBox 1.1.0
   58        */
   59       public static final COSInteger ONE = get(1); 
   60   
   61       /**
   62        * Constant for the number two.
   63        * @since Apache PDFBox 1.1.0
   64        */
   65       public static final COSInteger TWO = get(2); 
   66   
   67       /**
   68        * Constant for the number three.
   69        * @since Apache PDFBox 1.1.0
   70        */
   71       public static final COSInteger THREE = get(3); 
   72   
   73       /**
   74        * Returns a COSInteger instance with the given value.
   75        *
   76        * @param val integer value
   77        * @return COSInteger instance
   78        */
   79       public static COSInteger get(long val) {
   80           if (LOW <= val && val <= HIGH) {
   81               int index = (int) val - LOW;
   82               // no synchronization needed
   83               if (STATIC[index] == null) {
   84                   STATIC[index] = new COSInteger(val);
   85               }
   86               return STATIC[index];
   87           } else {
   88               return new COSInteger(val);
   89           }
   90       }
   91   
   92       private long value;
   93   
   94       /**
   95        * constructor.
   96        *
   97        * @deprecated use the static {@link #get(long)} method instead
   98        * @param val The integer value of this object.
   99        */
  100       public COSInteger( long val )
  101       {
  102           value = val;
  103       }
  104   
  105       /**
  106        * constructor.
  107        *
  108        * @deprecated use the static {@link #get(long)} method instead
  109        * @param val The integer value of this object.
  110        */
  111       public COSInteger( int val )
  112       {
  113           this( (long)val );
  114       }
  115   
  116       /**
  117        * This will create a new PDF Int object using a string.
  118        *
  119        * @param val The string value of the integer.
  120        * @deprecated use the static {@link #get(long)} method instead
  121        * @throws IOException If the val is not an integer type.
  122        */
  123       public COSInteger( String val ) throws IOException
  124       {
  125           try
  126           {
  127               value = Long.parseLong( val );
  128           }
  129           catch( NumberFormatException e )
  130           {
  131               throw new IOException( "Error: value is not an integer type actual='" + val + "'" );
  132           }
  133       }
  134   
  135       /**
  136        * {@inheritDoc}
  137        */
  138       public boolean equals(Object o)
  139       {
  140           return o instanceof COSInteger && ((COSInteger)o).intValue() == intValue();
  141       }
  142   
  143       /**
  144        * {@inheritDoc}
  145        */
  146       public int hashCode()
  147       {
  148           //taken from java.lang.Long
  149           return (int)(value ^ (value >> 32));
  150       }
  151   
  152       /**
  153        * {@inheritDoc}
  154        */
  155       public String toString()
  156       {
  157           return "COSInt{" + value + "}";
  158       }
  159   
  160       /**
  161        * Change the value of this reference.
  162        *
  163        * @param newValue The new value.
  164        */
  165       public void setValue( long newValue )
  166       {
  167           value = newValue;
  168       }
  169   
  170       /**
  171        * polymorphic access to value as float.
  172        *
  173        * @return The float value of this object.
  174        */
  175       public float floatValue()
  176       {
  177           return value;
  178       }
  179   
  180       /**
  181        * polymorphic access to value as float.
  182        *
  183        * @return The double value of this object.
  184        */
  185       public double doubleValue()
  186       {
  187           return value;
  188       }
  189   
  190       /**
  191        * Polymorphic access to value as int
  192        * This will get the integer value of this object.
  193        *
  194        * @return The int value of this object,
  195        */
  196       public int intValue()
  197       {
  198           return (int)value;
  199       }
  200   
  201       /**
  202        * Polymorphic access to value as int
  203        * This will get the integer value of this object.
  204        *
  205        * @return The int value of this object,
  206        */
  207       public long longValue()
  208       {
  209           return value;
  210       }
  211   
  212       /**
  213        * visitor pattern double dispatch method.
  214        *
  215        * @param visitor The object to notify when visiting this object.
  216        * @return any object, depending on the visitor implementation, or null
  217        * @throws COSVisitorException If an error occurs while visiting this object.
  218        */
  219       public Object accept(ICOSVisitor visitor) throws COSVisitorException
  220       {
  221           return visitor.visitFromInt(this);
  222       }
  223   
  224       /**
  225        * This will output this string as a PDF object.
  226        *
  227        * @param output The stream to write to.
  228        * @throws IOException If there is an error writing to the stream.
  229        */
  230       public void writePDF( OutputStream output ) throws IOException
  231       {
  232           output.write(String.valueOf(value).getBytes());
  233       }
  234   
  235   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.cos » [javadoc | source]