Author: bayard
Date: Sun Apr 16 23:59:50 2006
New Revision: 394617
URL: http://svn.apache.org/viewcvs?rev=394617&view=rev
Log:
Added new class CompositeFormat, as per issue #30184
(http://issues.apache.org/bugzilla/show_bug.cgi?id=30184)
Added:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/CompositeFormat.java
(with props)
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/CompositeFormatTest.java
(with props)
Modified:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/TextTestSuite.java
Added:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/CompositeFormat.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/CompositeFormat.java?rev=394617&view=auto
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/CompositeFormat.java
(added)
+++
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/CompositeFormat.java
Sun Apr 16 23:59:50 2006
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.commons.lang.text;
+
+import java.text.Format;
+import java.text.FieldPosition;
+import java.text.ParsePosition;
+
+/**
+ * Formats using one formatter and parses using a different formatter.
+ * An example of use for this would be a webapp where data is taken in one way
+ * and stored in a database another way.
+ *
+ * @author Archimedes Trajano
+ */
+public class CompositeFormat extends Format {
+
+ private final Format parser;
+ private final Format formatter;
+
+ /**
+ * Create a format that points its parseObject method to one
implementation
+ * and its format method to another.
+ *
+ * @param Format parser implementation
+ * @param Format formatter implementation
+ */
+ public CompositeFormat(Format parser, Format formatter) {
+ this.parser = parser;
+ this.formatter = formatter;
+ }
+
+ /**
+ * Uses the formatter Format instance.
+ *
+ * @see Format.format(Object, StringBuffer, FieldPosition)
+ */
+ public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
+ return formatter.format(obj,toAppendTo,pos);
+ }
+
+ /**
+ * Uses the parser Format instance.
+ *
+ * @see Format.parseObject(String, ParsePosition)
+ */
+ public Object parseObject(String source, ParsePosition pos) {
+ return parser.parseObject(source,pos);
+ }
+
+ /**
+ * Provides access to the parser Format implementation.
+ *
+ * @return Parser Format implementation
+ */
+ public Format getParser() {
+ return this.parser;
+ }
+
+ /**
+ * Provides access to the parser Format implementation.
+ *
+ * @return Formatter Format implementation
+ */
+ public Format getFormatter() {
+ return this.formatter;
+ }
+
+}
Propchange:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/CompositeFormat.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/CompositeFormatTest.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/CompositeFormatTest.java?rev=394617&view=auto
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/CompositeFormatTest.java
(added)
+++
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/CompositeFormatTest.java
Sun Apr 16 23:59:50 2006
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.commons.lang.text;
+
+import java.text.Format;
+import java.text.FieldPosition;
+import java.text.ParsePosition;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Unit tests for [EMAIL PROTECTED]
org.apache.commons.lang.text.CompositeFormat}.
+ */
+public class CompositeFormatTest extends TestCase {
+
+ /**
+ * Main method.
+ *
+ * @param args command line arguments, ignored
+ */
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Return a new test suite containing this test case.
+ *
+ * @return a new test suite containing this test case
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite(CompositeFormatTest.class);
+ suite.setName("CompositeFormat Tests");
+ return suite;
+ }
+
+ /**
+ * Create a new test case with the specified name.
+ *
+ * @param name
+ * name
+ */
+ public CompositeFormatTest(String name) {
+ super(name);
+ }
+
+
+ /**
+ * Ensures that the parse/format separation is correctly maintained.
+ */
+ public void testCompositeFormat() {
+
+ Format parser = new Format() {
+ public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ public Object parseObject(String source, ParsePosition pos) {
+ return null; // do nothing
+ }
+ };
+
+ Format formatter = new Format() {
+ public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
+ return null; // do nothing
+ }
+
+ public Object parseObject(String source, ParsePosition pos) {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ };
+
+ Format composite = new CompositeFormat(parser, formatter);
+
+ composite.parseObject("", null);
+ composite.format(new Object(), new StringBuffer(), null);
+ }
+
+}
Propchange:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/CompositeFormatTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/TextTestSuite.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/TextTestSuite.java?rev=394617&r1=394616&r2=394617&view=diff
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/TextTestSuite.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/TextTestSuite.java
Sun Apr 16 23:59:50 2006
@@ -48,6 +48,7 @@
public static Test suite() {
TestSuite suite = new TestSuite();
suite.setName("Commons-Lang-Text Tests");
+ suite.addTest(CompositeFormatTest.suite());
suite.addTest(StrBuilderTest.suite());
suite.addTest(StrBuilderAppendInsertTest.suite());
suite.addTest(StrMatcherTest.suite());
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]