Browse Source

First try of uncluttering CellNumberFormatter.

Make inner classes static and remove setting of private outer properties while in constructor.
Make properties final to make sure they are only set once.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1735300 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_15_BETA2
Andreas Beeker 8 years ago
parent
commit
6841ce8d55

+ 236
- 409
src/java/org/apache/poi/ss/format/CellNumberFormatter.java
File diff suppressed because it is too large
View File


+ 167
- 0
src/java/org/apache/poi/ss/format/CellNumberPartHandler.java View File

@@ -0,0 +1,167 @@
/* ====================================================================
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.format;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import org.apache.poi.ss.format.CellFormatPart.PartHandler;
import org.apache.poi.ss.format.CellNumberFormatter.Special;
import org.apache.poi.util.Internal;
/**
* Internal helper class for CellNumberFormatter
*/
@Internal
public class CellNumberPartHandler implements PartHandler {
private char insertSignForExponent;
private double scale = 1;
private Special decimalPoint;
private Special slash;
private Special exponent;
private Special numerator;
private final List<Special> specials = new LinkedList<Special>();
private boolean improperFraction;
public String handlePart(Matcher m, String part, CellFormatType type, StringBuffer descBuf) {
int pos = descBuf.length();
char firstCh = part.charAt(0);
switch (firstCh) {
case 'e':
case 'E':
// See comment in writeScientific -- exponent handling is complex.
// (1) When parsing the format, remove the sign from after the 'e' and
// put it before the first digit of the exponent.
if (exponent == null && specials.size() > 0) {
specials.add(exponent = new Special('.', pos));
insertSignForExponent = part.charAt(1);
return part.substring(0, 1);
}
break;
case '0':
case '?':
case '#':
if (insertSignForExponent != '\0') {
specials.add(new Special(insertSignForExponent, pos));
descBuf.append(insertSignForExponent);
insertSignForExponent = '\0';
pos++;
}
for (int i = 0; i < part.length(); i++) {
char ch = part.charAt(i);
specials.add(new Special(ch, pos + i));
}
break;
case '.':
if (decimalPoint == null && specials.size() > 0)
specials.add(decimalPoint = new Special('.', pos));
break;
case '/':
//!! This assumes there is a numerator and a denominator, but these are actually optional
if (slash == null && specials.size() > 0) {
numerator = previousNumber();
// If the first number in the whole format is the numerator, the
// entire number should be printed as an improper fraction
if (numerator == firstDigit(specials))
improperFraction = true;
specials.add(slash = new Special('.', pos));
}
break;
case '%':
// don't need to remember because we don't need to do anything with these
scale *= 100;
break;
default:
return null;
}
return part;
}
public char getInsertSignForExponent() {
return insertSignForExponent;
}
public double getScale() {
return scale;
}
public Special getDecimalPoint() {
return decimalPoint;
}
public Special getSlash() {
return slash;
}
public Special getExponent() {
return exponent;
}
public Special getNumerator() {
return numerator;
}
public List<Special> getSpecials() {
return specials;
}
public boolean isImproperFraction() {
return improperFraction;
}
private Special previousNumber() {
ListIterator<Special> it = specials.listIterator(specials.size());
while (it.hasPrevious()) {
Special s = it.previous();
if (isDigitFmt(s)) {
Special numStart = s;
Special last = s;
while (it.hasPrevious()) {
s = it.previous();
if (last.pos - s.pos > 1) // it has to be continuous digits
break;
if (isDigitFmt(s))
numStart = s;
else
break;
last = s;
}
return numStart;
}
}
return null;
}
private static boolean isDigitFmt(Special s) {
return s.ch == '0' || s.ch == '?' || s.ch == '#';
}
private static Special firstDigit(List<Special> specials) {
for (Special s : specials) {
if (isDigitFmt(s))
return s;
}
return null;
}
}

+ 109
- 0
src/java/org/apache/poi/ss/format/CellNumberStringMod.java View File

@@ -0,0 +1,109 @@
/* ====================================================================
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.format;
import org.apache.poi.ss.format.CellNumberFormatter.Special;
import org.apache.poi.util.Internal;
/**
* Internal helper class for CellNumberFormatter
*
* This class represents a single modification to a result string. The way
* this works is complicated, but so is numeric formatting. In general, for
* most formats, we use a DecimalFormat object that will put the string out
* in a known format, usually with all possible leading and trailing zeros.
* We then walk through the result and the original format, and note any
* modifications that need to be made. Finally, we go through and apply
* them all, dealing with overlapping modifications.
*/
@Internal
public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
public static final int BEFORE = 1;
public static final int AFTER = 2;
public static final int REPLACE = 3;
private final Special special;
private final int op;
private CharSequence toAdd;
private Special end;
private boolean startInclusive;
private boolean endInclusive;
public CellNumberStringMod(Special special, CharSequence toAdd, int op) {
this.special = special;
this.toAdd = toAdd;
this.op = op;
}
public CellNumberStringMod(Special start, boolean startInclusive, Special end, boolean endInclusive, char toAdd) {
this(start, startInclusive, end, endInclusive);
this.toAdd = toAdd + "";
}
public CellNumberStringMod(Special start, boolean startInclusive, Special end, boolean endInclusive) {
special = start;
this.startInclusive = startInclusive;
this.end = end;
this.endInclusive = endInclusive;
op = REPLACE;
toAdd = "";
}
public int compareTo(CellNumberStringMod that) {
int diff = special.pos - that.special.pos;
return (diff != 0) ? diff : (op - that.op);
}
@Override
public boolean equals(Object that) {
try {
return compareTo((CellNumberStringMod) that) == 0;
} catch (RuntimeException ignored) {
// NullPointerException or CastException
return false;
}
}
@Override
public int hashCode() {
return special.hashCode() + op;
}
public Special getSpecial() {
return special;
}
public int getOp() {
return op;
}
public CharSequence getToAdd() {
return toAdd;
}
public Special getEnd() {
return end;
}
public boolean isStartInclusive() {
return startInclusive;
}
public boolean isEndInclusive() {
return endInclusive;
}
}

Loading…
Cancel
Save