All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.hssf.record.FormulaSpecialCachedValue Maven / Gradle / Ivy

There is a newer version: 5.2.5
Show newest version
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hssf.record;

import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndianOutput;

/**
 * Manages the cached formula result values of other types besides numeric.
 * Excel encodes the same 8 bytes that would be field_4_value with various NaN
 * values that are decoded/encoded by this class.
 */
@Internal
public final class FormulaSpecialCachedValue {
    /** deliberately chosen by Excel in order to encode other values within Double NaNs */
    private static final long BIT_MARKER = 0xFFFF000000000000L;
    private static final int VARIABLE_DATA_LENGTH = 6;
    private static final int DATA_INDEX = 2;

    // FIXME: can these be merged with {@link CellType}?
    // are the numbers specific to the HSSF formula record format or just a poor-man's enum?
    public static final int STRING = 0;
    public static final int BOOLEAN = 1;
    public static final int ERROR_CODE = 2;
    public static final int EMPTY = 3;

    private final byte[] _variableData;

    FormulaSpecialCachedValue(FormulaSpecialCachedValue other) {
        _variableData = (other._variableData == null) ? null : other._variableData.clone();
    }

    private FormulaSpecialCachedValue(byte[] data) {
        _variableData = data;
    }

    public int getTypeCode() {
        return _variableData[0];
    }

    /**
     * @return null if the double value encoded by valueLongBits
     * is a normal (non NaN) double value.
     */
    public static FormulaSpecialCachedValue create(long valueLongBits) {
        if ((BIT_MARKER & valueLongBits) != BIT_MARKER) {
            return null;
        }

        byte[] result = new byte[VARIABLE_DATA_LENGTH];
        long x = valueLongBits;
        for (int i=0; i>= 8;
        }
        switch (result[0]) {
            case STRING:
            case BOOLEAN:
            case ERROR_CODE:
            case EMPTY:
                break;
            default:
                throw new org.apache.poi.util.RecordFormatException("Bad special value code (" + result[0] + ")");
        }
        return new FormulaSpecialCachedValue(result);
    }

    public void serialize(LittleEndianOutput out) {
        out.write(_variableData);
        out.writeShort(0xFFFF);
    }

    public String formatDebugString() {
        return formatValue() + ' ' + HexDump.toHex(_variableData);
    }

    private String formatValue() {
        int typeCode = getTypeCode();
        switch (typeCode) {
            case STRING:
                return "";
            case BOOLEAN:
                return getDataValue() == 0 ? "FALSE" : "TRUE";
            case ERROR_CODE:
                return ErrorEval.getText(getDataValue());
            case EMPTY:
                return "";
        }
        return "#error(type=" + typeCode + ")#";
    }

    private int getDataValue() {
        return _variableData[DATA_INDEX];
    }

    public static FormulaSpecialCachedValue createCachedEmptyValue() {
        return create(EMPTY, 0);
    }

    public static FormulaSpecialCachedValue createForString() {
        return create(STRING, 0);
    }

    public static FormulaSpecialCachedValue createCachedBoolean(boolean b) {
        return create(BOOLEAN, b ? 1 : 0);
    }

    public static FormulaSpecialCachedValue createCachedErrorCode(int errorCode) {
        return create(ERROR_CODE, errorCode);
    }

    private static FormulaSpecialCachedValue create(int code, int data) {
        byte[] vd = { (byte) code, 0, (byte) data, 0, 0, 0, };
        return new FormulaSpecialCachedValue(vd);
    }

    @Override
    public String toString() {
        return getClass().getName() + '[' + formatValue() + ']';
    }

    public int getValueType() {
        int typeCode = getTypeCode();
        switch (typeCode) {
            case EMPTY: // is this correct?
            case STRING:
                return CellType.STRING.getCode();
            case BOOLEAN:
                return CellType.BOOLEAN.getCode();
            case ERROR_CODE:
                return CellType.ERROR.getCode();
        }
        throw new IllegalStateException("Unexpected type id (" + typeCode + ")");
    }

    public boolean getBooleanValue() {
        if (getTypeCode() != BOOLEAN) {
            throw new IllegalStateException("Not a boolean cached value - " + formatValue());
        }
        return getDataValue() != 0;
    }

    public int getErrorValue() {
        if (getTypeCode() != ERROR_CODE) {
            throw new IllegalStateException("Not an error cached value - " + formatValue());
        }
        return getDataValue();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy