You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2010/01/26 17:15:14 UTC
svn commit: r903299 - in /labs/magma/trunk:
foundation-beans/src/main/java/org/apache/magma/conversion/string/
foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/
foundation-i18n/src/main/resources/META-INF/
foundation-i18n/src/test/java/or...
Author: simoneg
Date: Tue Jan 26 16:15:13 2010
New Revision: 903299
URL: http://svn.apache.org/viewvc?rev=903299&view=rev
Log:
Integer formatter
Added:
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/IntegerFormatter.java
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/formatters/IntegerFormatterTest.java
Modified:
labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/IntegerConverter.java
labs/magma/trunk/foundation-i18n/src/main/resources/META-INF/magma.default.properties
Modified: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/IntegerConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/IntegerConverter.java?rev=903299&r1=903298&r2=903299&view=diff
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/IntegerConverter.java (original)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/IntegerConverter.java Tue Jan 26 16:15:13 2010
@@ -32,13 +32,13 @@
/**
* Max length is the length in characters of the minimum value of int.
*/
- private static int maxlength = Integer.toString(Integer.MIN_VALUE).length();
+ protected static int maxlength = Integer.toString(Integer.MIN_VALUE).length();
/**
* The error used when conversion is not possible.
*/
- private LocalizableString parsingError = new LocalizableString("Not a valid number");
- private LocalizableString parsingNotInRange = new LocalizableString("Please provide a number between {0} and {1}", Integer.MIN_VALUE, Integer.MAX_VALUE);
+ protected LocalizableString parsingError = new LocalizableString("Not a valid number");
+ protected LocalizableString parsingNotInRange = new LocalizableString("Please provide a number between {0} and {1}", Integer.MIN_VALUE, Integer.MAX_VALUE);
public IntegerConverter() {
super(Integer.class);
Added: labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/IntegerFormatter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/IntegerFormatter.java?rev=903299&view=auto
==============================================================================
--- labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/IntegerFormatter.java (added)
+++ labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/IntegerFormatter.java Tue Jan 26 16:15:13 2010
@@ -0,0 +1,130 @@
+/*
+ * 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.magma.i18n.formatters;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Currency;
+import java.util.Locale;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.conversion.ConversionException;
+import org.apache.magma.conversion.string.DoubleConverter;
+import org.apache.magma.conversion.string.IntegerConverter;
+import org.apache.magma.i18n.CurrentLocale;
+import org.apache.magma.i18n.Formatter;
+
+/**
+ * Formats integer values.
+ *
+ * If not format string is specified, the default java formatting is used, see {@link NumberFormat} for details.
+ *
+ * Otherwise "percent" style is supported for representing percents. While a double is
+ * far better for representing fractions, yet you can use it with integers. In that case,
+ * 50 = 50%, 100 = 100% etc..
+ *
+ * @author Simone Gianni <si...@apache.org>
+ */
+public class IntegerFormatter extends IntegerConverter implements Formatter<Integer> {
+
+
+ private String myformat = "default";
+
+ public IntegerFormatter buildDefault(Class<? extends Integer> clazz) {
+ return new IntegerFormatter();
+ }
+
+ public void setFormat(String format) {
+ myformat = format;
+ }
+
+ @Override
+ public String to(Integer value) {
+ if (value == null) return "";
+ String formatterName = getFormatterName();
+ NumberFormat formatter = getFormatter(formatterName);
+ if (formatter == null) return super.to(value);
+ if (formatterName != null && formatterName.equals("percent")) {
+ double val = ((double)value.intValue()) / 100d;
+ return formatter.format(val).trim();
+ }
+ return formatter.format(value).trim();
+ }
+
+ @Override
+ public Integer from(String value) {
+ if (value == null || value.length() == 0) return onNative ? 0 : null;
+ String formattername = getFormatterName();
+ NumberFormat formatter = getFormatter(formattername);
+ if (formatter == null) return super.from(value);
+ try {
+ Number number = formatter.parse(value);
+ int intValue = number.intValue();
+ long longValue = number.longValue();
+ if (formattername != null && formattername.equals("percent")) {
+ intValue = (int)(number.doubleValue() * 100);
+ longValue = (long)(number.doubleValue() * 100);
+ }
+ if (intValue != longValue) {
+ throw new ConversionException(parsingNotInRange);
+ }
+ return intValue;
+ } catch (ParseException e) {
+ throw new ConversionException(parsingError, e);
+ }
+ }
+
+ public String getFormatterName() {
+ String useformat = this.myformat;
+ if (this.myformat.toLowerCase().equals("default")) {
+ useformat = new LocalizableString("integer_format").toString();
+ if (useformat.toString().endsWith("integer_format")) {
+ return null;
+ }
+ }
+ return useformat;
+ }
+
+ public NumberFormat getFormatter(String useformat) {
+ Locale locale = CurrentLocale.getLocale();
+ if (useformat == null) {
+ return NumberFormat.getInstance(locale);
+ }
+ if (useformat.toLowerCase().startsWith("percent")) {
+ NumberFormat instance = NumberFormat.getPercentInstance(locale);
+ return instance;
+ } else {
+ NumberFormat instance = NumberFormat.getInstance(locale);
+ if (instance instanceof DecimalFormat) {
+ DecimalFormat df = (DecimalFormat) instance;
+ df.applyPattern(useformat);
+ }
+ return instance;
+ }
+ }
+
+ public String getPattern() {
+ NumberFormat formatter = getFormatter(getFormatterName());
+ if (formatter instanceof DecimalFormat) {
+ return ((DecimalFormat)formatter).toPattern();
+ }
+ return null;
+ }
+
+}
Modified: labs/magma/trunk/foundation-i18n/src/main/resources/META-INF/magma.default.properties
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/main/resources/META-INF/magma.default.properties?rev=903299&r1=903298&r2=903299&view=diff
==============================================================================
--- labs/magma/trunk/foundation-i18n/src/main/resources/META-INF/magma.default.properties (original)
+++ labs/magma/trunk/foundation-i18n/src/main/resources/META-INF/magma.default.properties Tue Jan 26 16:15:13 2010
@@ -14,6 +14,7 @@
#limitations under the License.
org.apache.magma.i18n.Formatter.~date=org.apache.magma.i18n.formatters.DateFormatter
org.apache.magma.i18n.Formatter.~double=org.apache.magma.i18n.formatters.DoubleFormatter
+org.apache.magma.i18n.Formatter.~integer=org.apache.magma.i18n.formatters.IntegerFormatter
org.apache.magma.i18n.Formatter.~boolean=org.apache.magma.i18n.formatters.BooleanFormatter
org.apache.magma.i18n.Formatter.~enum=org.apache.magma.i18n.formatters.EnumFormatter
org.apache.magma.i18n.Formatter.~string=org.apache.magma.i18n.formatters.StringFormatter
Added: labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/formatters/IntegerFormatterTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/formatters/IntegerFormatterTest.java?rev=903299&view=auto
==============================================================================
--- labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/formatters/IntegerFormatterTest.java (added)
+++ labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/formatters/IntegerFormatterTest.java Tue Jan 26 16:15:13 2010
@@ -0,0 +1,99 @@
+/*
+ * 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.magma.i18n.formatters;
+
+import java.util.Locale;
+
+import org.apache.magma.i18n.CurrentLocale;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+import static org.junit.Assert.*;
+
+@RunWith(Theories.class)
+public class IntegerFormatterTest {
+
+ static class Check {
+ public int value;
+ public String format;
+ public String itstring;
+ public String usstring;
+
+ public boolean checkto = true;
+ public boolean checkfrom = true;
+
+ public Check(int value, String format, String itstring, String usstring) {
+ this.value = value;
+ this.format = format;
+ this.itstring = itstring;
+ this.usstring = usstring;
+ }
+
+ public Check(int value, String format, String itstring, String usstring, boolean checkto, boolean checkfrom) {
+ this.value = value;
+ this.format = format;
+ this.itstring = itstring;
+ this.usstring = usstring;
+ this.checkto = checkto;
+ this.checkfrom = checkfrom;
+ }
+
+
+
+ }
+
+ @DataPoint
+ public static Check
+ set1 = new Check(1, "default", "1", "1"),
+ set3 = new Check(-1, "default", "-1", "-1"),
+ set4 = new Check(100000, "default", "100.000", "100,000"),
+ set4b = new Check(100000, "default", "100000", "100000", false, true),
+ set6 = new Check(50, "percent", "50%", "50%"),
+ set7 = new Check(200, "percent", "200%", "200%"),
+ set10 = new Check(100, "##0.00", "100,00", "100.00"),
+ set11 = new Check(0, "#00.00", "00,00", "00.00");
+
+ @Theory
+ public void doubleFormat(Check check) throws Throwable {
+ IntegerFormatter df = new IntegerFormatter();
+ df.setFormat(check.format);
+ CurrentLocale.setLocale(Locale.ITALIAN);
+ String itTo = check.checkto ? df.to(check.value) : "";
+ int itFrom = check.checkfrom ? df.from(check.itstring) : 0;
+ CurrentLocale.setLocale(Locale.US);
+ String usTo = check.checkto ? df.to(check.value) : "";
+ int usFrom = check.checkfrom ? df.from(check.usstring) : 0;
+
+ try {
+ if (check.checkto) {
+ assertEquals(check.itstring, itTo);
+ assertEquals(check.usstring, usTo);
+ }
+ if (check.checkfrom) {
+ assertEquals(check.value, itFrom);
+ assertEquals(check.value, usFrom);
+ }
+ } catch (Throwable e) {
+ System.out.println(e.getMessage());
+ throw e;
+ }
+ }
+
+
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org