From e4c0ee1fa0f835230c70712d72a6824056e2f4ad Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Wed, 9 Mar 2016 20:57:26 +0000 Subject: [PATCH] Remove a superfluous/bloated log function, which String.format can do better and is only used once ... git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734310 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/model/InternalSheet.java | 3 +- src/java/org/apache/poi/util/POILogger.java | 93 ------------------- src/java/org/apache/poi/util/StringUtil.java | 85 +---------------- .../org/apache/poi/util/TestPOILogger.java | 9 -- .../org/apache/poi/util/TestStringUtil.java | 36 +------ 5 files changed, 5 insertions(+), 221 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/InternalSheet.java b/src/java/org/apache/poi/hssf/model/InternalSheet.java index 8cb482a574..b215e03f8a 100644 --- a/src/java/org/apache/poi/hssf/model/InternalSheet.java +++ b/src/java/org/apache/poi/hssf/model/InternalSheet.java @@ -688,8 +688,7 @@ public final class InternalSheet { */ public void removeValueRecord(int row, CellValueRecordInterface col) { - log.logFormatted(POILogger.DEBUG, "remove value record row %", - new int[]{row } ); + log.log(POILogger.DEBUG, "remove value record row "+row); _rowsAggregate.removeCell(col); } diff --git a/src/java/org/apache/poi/util/POILogger.java b/src/java/org/apache/poi/util/POILogger.java index d892366e7e..04198bfbab 100644 --- a/src/java/org/apache/poi/util/POILogger.java +++ b/src/java/org/apache/poi/util/POILogger.java @@ -17,18 +17,11 @@ package org.apache.poi.util; -import java.util.ArrayList; -import java.util.List; - /** * A logger interface that strives to make it as easy as possible for * developers to write log calls, while simultaneously making those * calls as cheap as possible by performing lazy evaluation of the log * message.

- * - * @author Marc Johnson (mjohnson at apache dot org) - * @author Glen Stampoultzis (glens at apache.org) - * @author Nicola Ken Barozzi (nicolaken at apache.org) */ @Internal public abstract class POILogger { @@ -110,90 +103,4 @@ public abstract class POILogger { log(level, msg, lastEx); } } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.

- * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after message are matched - * in order.

- * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.

- * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param unflatParams The objects to match against. - */ - public void logFormatted(int level, String message, Object... unflatParams) { - if (!check(level)) return; - Object[] params = flattenArrays(unflatParams); - String msg = StringUtil.format(message, params); - msg = msg.replaceAll("[\r\n]+", " "); // log forging escape - - if (params.length > 0 && params[params.length-1] instanceof Throwable) { - log(level, msg, (Throwable)params[params.length-1]); - } else { - log(level, msg); - } - } - - /** - * Flattens any contained objects. Only traverses one level deep. - */ - private Object[] flattenArrays(Object... unflatParams) { - List results = new ArrayList(); - for (Object obj : unflatParams) { - flattenObject(results, obj); - } - return results.toArray(new Object[results.size()]); - } - - private void flattenObject(List results, Object object) { - if (object instanceof byte[]) { - for (byte b : (byte[])object) { - results.add(Byte.valueOf(b)); - } - } else if (object instanceof char[]) { - for (char c : (char[])object) { - results.add(Character.valueOf(c)); - } - } else if (object instanceof short[]) { - for (short s : (short[])object) { - results.add(Short.valueOf(s)); - } - } else if (object instanceof int[]) { - for (int i : (int[])object) { - results.add(Integer.valueOf(i)); - } - } else if (object instanceof long[]) { - for (long l : (long[])object) { - results.add(Long.valueOf(l)); - } - } else if (object instanceof float[]) { - for (float f : (float[])object) { - results.add(Float.valueOf(f)); - } - } else if (object instanceof double[]) { - for (double d : (double[])object) { - results.add(Double.valueOf(d)); - } - } else if (object instanceof Object[]) { - for (Object o : (Object[])object) { - results.add(o); - } - } else { - results.add(object); - } - } } diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index d6597a9e6d..e1735e6242 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -18,20 +18,14 @@ package org.apache.poi.util; import java.nio.charset.Charset; -import java.text.FieldPosition; -import java.text.NumberFormat; import java.util.HashMap; import java.util.Iterator; -import java.util.Locale; import java.util.Map; -import org.apache.poi.hssf.record.RecordInputStream; /** - * Title: String Utility Description: Collection of string handling utilities

- * - * Note - none of the methods in this class deals with {@link org.apache.poi.hssf.record.ContinueRecord}s. - * For such functionality, consider using {@link RecordInputStream} + * Collection of string handling utilities */ +@Internal public class StringUtil { protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); protected static final Charset UTF16LE = Charset.forName("UTF-16LE"); @@ -261,81 +255,6 @@ public class StringUtil { return new String(bytes, UTF16LE); } - /** - * Apply printf() like formatting to a string. - * Primarily used for logging. - * @param message the string with embedded formatting info - * eg. "This is a test %2.2" - * @param params array of values to format into the string - * @return The formatted string - */ - public static String format(String message, Object[] params) { - int currentParamNumber = 0; - StringBuffer formattedMessage = new StringBuffer(); - for (int i = 0; i < message.length(); i++) { - if (message.charAt(i) == '%') { - if (currentParamNumber >= params.length) { - formattedMessage.append("?missing data?"); - } else if ( - (params[currentParamNumber] instanceof Number) - && (i + 1 < message.length())) { - i - += matchOptionalFormatting( - (Number) params[currentParamNumber++], - message.substring(i + 1), - formattedMessage); - } else { - formattedMessage.append( - params[currentParamNumber++].toString()); - } - } else { - if ((message.charAt(i) == '\\') - && (i + 1 < message.length()) - && (message.charAt(i + 1) == '%')) { - formattedMessage.append('%'); - i++; - } else { - formattedMessage.append(message.charAt(i)); - } - } - } - return formattedMessage.toString(); - } - - - private static int matchOptionalFormatting( - Number number, - String formatting, - StringBuffer outputTo) { - NumberFormat numberFormat = NumberFormat.getInstance(Locale.US); - if ((0 < formatting.length()) - && Character.isDigit(formatting.charAt(0))) { - numberFormat.setMinimumIntegerDigits( - Integer.parseInt(formatting.charAt(0) + "")); - if ((2 < formatting.length()) - && (formatting.charAt(1) == '.') - && Character.isDigit(formatting.charAt(2))) { - numberFormat.setMaximumFractionDigits( - Integer.parseInt(formatting.charAt(2) + "")); - numberFormat.format(number, outputTo, new FieldPosition(0)); - return 3; - } - numberFormat.format(number, outputTo, new FieldPosition(0)); - return 1; - } else if ( - (0 < formatting.length()) && (formatting.charAt(0) == '.')) { - if ((1 < formatting.length()) - && Character.isDigit(formatting.charAt(1))) { - numberFormat.setMaximumFractionDigits( - Integer.parseInt(formatting.charAt(1) + "")); - numberFormat.format(number, outputTo, new FieldPosition(0)); - return 2; - } - } - numberFormat.format(number, outputTo, new FieldPosition(0)); - return 1; - } - /** * @return the encoding we want to use, currently hardcoded to ISO-8859-1 */ diff --git a/src/testcases/org/apache/poi/util/TestPOILogger.java b/src/testcases/org/apache/poi/util/TestPOILogger.java index a9a5a880eb..a53de44eb4 100644 --- a/src/testcases/org/apache/poi/util/TestPOILogger.java +++ b/src/testcases/org/apache/poi/util/TestPOILogger.java @@ -51,15 +51,6 @@ public final class TestPOILogger extends POILogger { log.log(POILogger.WARN, "Test = ", 1); assertEquals("Test = 1", tlog.lastLog); - log.logFormatted(POILogger.ERROR, "Test param 1 = %, param 2 = %d", "2", 3 ); - assertEquals("Test param 1 = 2, param 2 = 3", tlog.lastLog); - - log.logFormatted(POILogger.ERROR, "Test param 1 = %d, param 2 = %", new int[]{4, 5} ); - assertEquals("Test param 1 = 4, param 2 = 5", tlog.lastLog); - - log.logFormatted(POILogger.ERROR, "Test param 1 = %1.1, param 2 = %0.1", new double[]{4, 5.23} ); - assertEquals("Test param 1 = 4, param 2 = 5.2", tlog.lastLog); - log.log(POILogger.ERROR, "Test ", 1,2,new Exception("bla")); assertEquals("Test 12", tlog.lastLog); assertNotNull(tlog.lastEx); diff --git a/src/testcases/org/apache/poi/util/TestStringUtil.java b/src/testcases/org/apache/poi/util/TestStringUtil.java index fcef5bd17a..17498396ec 100644 --- a/src/testcases/org/apache/poi/util/TestStringUtil.java +++ b/src/testcases/org/apache/poi/util/TestStringUtil.java @@ -18,12 +18,11 @@ package org.apache.poi.util; import java.nio.charset.Charset; -import java.text.NumberFormat; - -import junit.framework.TestCase; import org.apache.poi.util.StringUtil.StringsIterator; +import junit.framework.TestCase; + /** * Unit test for StringUtil * @@ -125,37 +124,6 @@ public final class TestStringUtil extends TestCase { } } - public void testFormat() { - - confirm("This is a test " + fmt(1.2345, 2, 2), "This is a test %2.2", new Double(1.2345)); - confirm("This is a test " + fmt(1.2345, -1, 3), "This is a test %.3", new Double(1.2345)); - confirm("This is a great test " + fmt(1.2345, -1, 3), - "This is a % test %.3", "great", new Double(1.2345)); - confirm("This is a test 1", "This is a test %", Integer.valueOf(1)); - confirm("This is a test 1", "This is a test %", Integer.valueOf(1), Integer.valueOf(1)); - confirm("This is a test 1.x", "This is a test %1.x", Integer.valueOf(1)); - confirm("This is a test ?missing data?1.x", "This is a test %1.x"); - confirm("This is a test %1.x", "This is a test \\%1.x"); - } - - private static void confirm(String expectedResult, String fmtString, Object ... params) { - String actualResult = StringUtil.format(fmtString, params); - assertEquals(expectedResult, actualResult); - } - - private static String fmt(double num, int minIntDigits, int maxFracDigitis) { - NumberFormat nf = NumberFormat.getInstance(LocaleUtil.getUserLocale()); - - if (minIntDigits != -1) { - nf.setMinimumIntegerDigits(minIntDigits); - } - if (maxFracDigitis != -1) { - nf.setMaximumFractionDigits(maxFracDigitis); - } - - return nf.format( num ); - } - public void testStringsIterator() { StringsIterator i;