Added new class CompositeFormat, as per issue #30184 (http://issues.apache.org/bugzilla/show_bug.cgi?id=30184)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@394617 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7c4b3e8894
commit
77fdd9eaae
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 {@link 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -48,6 +48,7 @@ public class TextTestSuite extends TestCase {
|
|||
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());
|
||||
|
|
Loading…
Reference in New Issue