001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: /*
019: * IntPtg.java
020: *
021: * Created on October 29, 2001, 7:37 PM
022: */
023: package org.apache.poi.hssf.record.formula;
024:
025: import org.apache.poi.util.LittleEndian;
026: import org.apache.poi.hssf.model.Workbook;
027: import org.apache.poi.hssf.record.RecordInputStream;
028:
029: /**
030: * Integer (unsigned short intger)
031: * Stores an unsigned short value (java int) in a formula
032: * @author Andrew C. Oliver (acoliver at apache dot org)
033: * @author Jason Height (jheight at chariot dot net dot au)
034: */
035:
036: public class IntPtg extends Ptg {
037: public final static int SIZE = 3;
038: public final static byte sid = 0x1e;
039: private short field_1_value;
040:
041: private IntPtg() {
042: //Required for clone methods
043: }
044:
045: public IntPtg(RecordInputStream in) {
046: setValue(in.readShort());
047: }
048:
049: // IntPtg should be able to create itself, shouldnt have to call setValue
050: public IntPtg(String formulaToken) {
051: setValue(Short.parseShort(formulaToken));
052: }
053:
054: /**
055: * Sets the wrapped value.
056: * Normally you should call with a positive int.
057: */
058: public void setValue(short value) {
059: field_1_value = value;
060: }
061:
062: /**
063: * Sets the unsigned value.
064: * (Handles conversion to the internal short value)
065: */
066: public void setValue(int value) {
067: if (value > Short.MAX_VALUE) {
068: // Need to wrap
069: value -= (Short.MAX_VALUE + 1) * 2;
070: }
071: field_1_value = (short) value;
072: }
073:
074: /**
075: * Returns the value as a short, which may have
076: * been wrapped into negative numbers
077: */
078: public short getValue() {
079: return field_1_value;
080: }
081:
082: /**
083: * Returns the value as an unsigned positive int.
084: */
085: public int getValueAsInt() {
086: if (field_1_value < 0) {
087: return (Short.MAX_VALUE + 1) * 2 + field_1_value;
088: }
089: return field_1_value;
090: }
091:
092: public void writeBytes(byte[] array, int offset) {
093: array[offset + 0] = sid;
094: LittleEndian.putShort(array, offset + 1, getValue());
095: }
096:
097: public int getSize() {
098: return SIZE;
099: }
100:
101: public String toFormulaString(Workbook book) {
102: return "" + getValue();
103: }
104:
105: public byte getDefaultOperandClass() {
106: return Ptg.CLASS_VALUE;
107: }
108:
109: public Object clone() {
110: IntPtg ptg = new IntPtg();
111: ptg.field_1_value = field_1_value;
112: return ptg;
113: }
114: }
|