+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
package org.apache.poi.ss.formula;\r
\r
import java.util.Arrays;\r
import org.apache.poi.util.LittleEndianInput;\r
import org.apache.poi.util.LittleEndianOutput;\r
\r
+/**\r
+ * Encapsulates an encoded formula token array. \r
+ * \r
+ * @author Josh Micich\r
+ */\r
public class Formula {\r
\r
- private static final byte[] EMPTY_BYTE_ARRAY = { };\r
+ private static final Formula EMPTY = new Formula(new byte[0], 0);\r
+\r
+ /** immutable */\r
private final byte[] _byteEncoding;\r
private final int _encodedTokenLen;\r
\r
_byteEncoding = byteEncoding;\r
_encodedTokenLen = encodedTokenLen;\r
if (false) { // set to true to eagerly check Ptg decoding \r
- LittleEndianByteArrayInputStream in = new LittleEndianByteArrayInputStream(byteEncoding);\r
- Ptg.readTokens(encodedTokenLen, in);\r
- int nUnusedBytes = _byteEncoding.length - in.getReadIndex();\r
- if (nUnusedBytes > 0) {\r
- // TODO - this seems to occur when IntersectionPtg is present\r
- // This example file "IntersectionPtg.xls"\r
- // used by test: TestIntersectionPtg.testReading()\r
- // has 10 bytes unused at the end of the formula\r
- // 10 extra bytes are just 0x01 and 0x00\r
- System.out.println(nUnusedBytes + " unused bytes at end of formula");\r
- }\r
+ LittleEndianByteArrayInputStream in = new LittleEndianByteArrayInputStream(byteEncoding);\r
+ Ptg.readTokens(encodedTokenLen, in);\r
+ int nUnusedBytes = _byteEncoding.length - in.getReadIndex();\r
+ if (nUnusedBytes > 0) {\r
+ // TODO - this seems to occur when IntersectionPtg is present\r
+ // This example file "IntersectionPtg.xls"\r
+ // used by test: TestIntersectionPtg.testReading()\r
+ // has 10 bytes unused at the end of the formula\r
+ // 10 extra bytes are just 0x01 and 0x00\r
+ System.out.println(nUnusedBytes + " unused bytes at end of formula");\r
+ }\r
}\r
}\r
/**\r
* @return Never <code>null</code> (Possibly empty if the supplied <tt>ptgs</tt> is <code>null</code>)\r
*/\r
public static Formula create(Ptg[] ptgs) {\r
- if (ptgs == null) {\r
- return new Formula(EMPTY_BYTE_ARRAY, 0);\r
+ if (ptgs == null || ptgs.length < 1) {\r
+ return EMPTY;\r
}\r
int totalSize = Ptg.getEncodedSize(ptgs);\r
byte[] encodedData = new byte[totalSize];\r
}\r
\r
public Formula copy() {\r
- // OK to return this for the moment because currently immutable\r
+ // OK to return this because immutable\r
return this;\r
}\r
\r