From 2ebe3f7e328534457b6bd807c16e391d3cd36b01 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Wed, 3 Dec 2008 23:39:11 +0000 Subject: [PATCH] Minor code improvements, fixed comments git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@723146 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/formula/Formula.java | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/Formula.java b/src/java/org/apache/poi/ss/formula/Formula.java index eaf73cd63..f2cef2cb4 100644 --- a/src/java/org/apache/poi/ss/formula/Formula.java +++ b/src/java/org/apache/poi/ss/formula/Formula.java @@ -1,3 +1,20 @@ +/* ==================================================================== + 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.ss.formula; import java.util.Arrays; @@ -14,9 +31,16 @@ import org.apache.poi.util.LittleEndianByteArrayInputStream; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutput; +/** + * Encapsulates an encoded formula token array. + * + * @author Josh Micich + */ public class Formula { - private static final byte[] EMPTY_BYTE_ARRAY = { }; + private static final Formula EMPTY = new Formula(new byte[0], 0); + + /** immutable */ private final byte[] _byteEncoding; private final int _encodedTokenLen; @@ -24,17 +48,17 @@ public class Formula { _byteEncoding = byteEncoding; _encodedTokenLen = encodedTokenLen; if (false) { // set to true to eagerly check Ptg decoding - LittleEndianByteArrayInputStream in = new LittleEndianByteArrayInputStream(byteEncoding); - Ptg.readTokens(encodedTokenLen, in); - int nUnusedBytes = _byteEncoding.length - in.getReadIndex(); - if (nUnusedBytes > 0) { - // TODO - this seems to occur when IntersectionPtg is present - // This example file "IntersectionPtg.xls" - // used by test: TestIntersectionPtg.testReading() - // has 10 bytes unused at the end of the formula - // 10 extra bytes are just 0x01 and 0x00 - System.out.println(nUnusedBytes + " unused bytes at end of formula"); - } + LittleEndianByteArrayInputStream in = new LittleEndianByteArrayInputStream(byteEncoding); + Ptg.readTokens(encodedTokenLen, in); + int nUnusedBytes = _byteEncoding.length - in.getReadIndex(); + if (nUnusedBytes > 0) { + // TODO - this seems to occur when IntersectionPtg is present + // This example file "IntersectionPtg.xls" + // used by test: TestIntersectionPtg.testReading() + // has 10 bytes unused at the end of the formula + // 10 extra bytes are just 0x01 and 0x00 + System.out.println(nUnusedBytes + " unused bytes at end of formula"); + } } } /** @@ -112,8 +136,8 @@ public class Formula { * @return Never null (Possibly empty if the supplied ptgs is null) */ public static Formula create(Ptg[] ptgs) { - if (ptgs == null) { - return new Formula(EMPTY_BYTE_ARRAY, 0); + if (ptgs == null || ptgs.length < 1) { + return EMPTY; } int totalSize = Ptg.getEncodedSize(ptgs); byte[] encodedData = new byte[totalSize]; @@ -136,7 +160,7 @@ public class Formula { } public Formula copy() { - // OK to return this for the moment because currently immutable + // OK to return this because immutable return this; }