mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-16 23:15:28 +00:00
[LANG-366] retract
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@630967 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
68c6547d0c
commit
583c62281e
src
java/org/apache/commons/lang/text
test/org/apache/commons/lang/text
@ -1,167 +0,0 @@
|
||||
/*
|
||||
* 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.commons.lang.text;
|
||||
|
||||
import java.text.FieldPosition;
|
||||
import java.text.Format;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
||||
/**
|
||||
* Format that tries a number of delegates in turn until one is successful.
|
||||
* Contrast to {@link CompositeFormat}.
|
||||
*
|
||||
* @author Matt Benson
|
||||
* @since 2.4
|
||||
* @version $Id$
|
||||
*/
|
||||
public class MultiFormat extends Format {
|
||||
private static final long serialVersionUID = -6128683973856547540L;
|
||||
|
||||
/**
|
||||
* Provides a builder with a fluent interface. Example:
|
||||
* <p>
|
||||
* <code>
|
||||
* <pre>
|
||||
* MultiFormat mf = new MultiFormat.Builder().add(new FooFormat()).add(
|
||||
* new BarFormat()).add(new BazFormat()).toMultiFormat();
|
||||
* </pre></code>
|
||||
* </p>
|
||||
*/
|
||||
public static class Builder {
|
||||
private ArrayList delegates = new ArrayList();
|
||||
|
||||
/**
|
||||
* Add a delegate format.
|
||||
*
|
||||
* @param delegate Format
|
||||
* @return the builder
|
||||
*/
|
||||
public Builder add(Format delegate) {
|
||||
Validate.notNull(delegate, "delegate format is null");
|
||||
delegates.add(delegate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the {@link MultiFormat} instance from this Builder.
|
||||
*
|
||||
* @return MultiFormat
|
||||
*/
|
||||
public MultiFormat toMultiFormat() {
|
||||
return new MultiFormat((Format[]) delegates
|
||||
.toArray(new Format[delegates.size()]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Format[] delegates;
|
||||
|
||||
/**
|
||||
* Create a new MultiFormat.
|
||||
*/
|
||||
public MultiFormat() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new MultiFormat.
|
||||
*
|
||||
* @param delegates Formats
|
||||
*/
|
||||
public MultiFormat(Format[] delegates) {
|
||||
setDelegates(delegates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format <code>obj</code>; append to <code>toAppendTo</code>.
|
||||
*
|
||||
* @param obj Object to format
|
||||
* @param toAppendTo StringBuffer to append to
|
||||
* @param pos FieldPosition
|
||||
* @return <code>toAppendTo</code>
|
||||
*/
|
||||
public StringBuffer format(Object obj, StringBuffer toAppendTo,
|
||||
FieldPosition pos) {
|
||||
Format[] d = getValidDelegates();
|
||||
for (int i = 0; i < d.length; i++) {
|
||||
try {
|
||||
return d[i].format(obj, toAppendTo, pos);
|
||||
} catch (IllegalArgumentException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No delegate Format can parse "
|
||||
+ obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an object by trying each delegate.
|
||||
*
|
||||
* @param source string
|
||||
* @param pos current parse position
|
||||
* @return value returned from first delegate that does not encounter an
|
||||
* error.
|
||||
*/
|
||||
public Object parseObject(String source, ParsePosition pos) {
|
||||
int start = pos.getIndex();
|
||||
Format[] d = getDelegates();
|
||||
for (int i = 0; i < d.length; i++) {
|
||||
Object o = d[i].parseObject(source, pos);
|
||||
if (pos.getErrorIndex() < 0) {
|
||||
return o;
|
||||
}
|
||||
// set up for next attempt:
|
||||
pos.setIndex(start);
|
||||
pos.setErrorIndex(-1);
|
||||
}
|
||||
pos.setErrorIndex(start);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the delegates.
|
||||
*
|
||||
* @param delegates the Format[] delegates to set.
|
||||
*/
|
||||
public void setDelegates(Format[] delegates) {
|
||||
Validate.noNullElements(delegates,
|
||||
"Null elements present in delegates Format[]");
|
||||
this.delegates = delegates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the delegates.
|
||||
*
|
||||
* @return Format[].
|
||||
*/
|
||||
public Format[] getDelegates() {
|
||||
return delegates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate and return our delegates.
|
||||
*
|
||||
* @return delegate Formats, not null
|
||||
*/
|
||||
private Format[] getValidDelegates() {
|
||||
Format[] result = getDelegates();
|
||||
Validate.notEmpty(result, "No delegate Formats configured");
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,135 +0,0 @@
|
||||
/*
|
||||
* 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.commons.lang.text;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.FieldPosition;
|
||||
import java.text.Format;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.lang.ClassUtils;
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Test MultiFormat
|
||||
*
|
||||
* @author Matt Benson
|
||||
* @since 2.4
|
||||
* @version $Id$
|
||||
*/
|
||||
public class MultiFormatTest extends TestCase {
|
||||
private class GuardedFormat extends Format {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
Format delegate;
|
||||
Class[] allowableTypes;
|
||||
|
||||
/**
|
||||
* Create a new MultiFormatTest.GuardedFormat.
|
||||
*/
|
||||
public GuardedFormat(Format delegate, Class[] allowableTypes) {
|
||||
Validate.notNull(delegate);
|
||||
this.delegate = delegate;
|
||||
Validate.notNull(allowableTypes);
|
||||
this.allowableTypes = allowableTypes;
|
||||
}
|
||||
|
||||
public StringBuffer format(Object obj, StringBuffer toAppendTo,
|
||||
FieldPosition pos) {
|
||||
Class c = obj == null ? null : obj.getClass();
|
||||
for (int i = 0; i < allowableTypes.length; i++) {
|
||||
if (ClassUtils.isAssignable(c, allowableTypes[i])) {
|
||||
return delegate.format(obj, toAppendTo, pos);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public Object parseObject(String source, ParsePosition pos) {
|
||||
return delegate.parseObject(source, pos);
|
||||
}
|
||||
}
|
||||
|
||||
private Format format;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
// silliness to avoid the DateFormat grabbing the Integer, or the
|
||||
// integer parsing the first (month) date component:
|
||||
format = new MultiFormat.Builder().add(
|
||||
new GuardedFormat(DateFormat.getDateInstance(DateFormat.SHORT,
|
||||
Locale.US), new Class[] { Date.class })).add(
|
||||
getIntegerNumberFormat(Locale.US)).toMultiFormat();
|
||||
}
|
||||
|
||||
private NumberFormat getIntegerNumberFormat(Locale locale) {
|
||||
NumberFormat result = NumberFormat.getInstance(locale);
|
||||
result.setMaximumFractionDigits(0);
|
||||
result.setParseIntegerOnly(true);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void testFormatNumber() {
|
||||
assertEquals("1,000", format.format(new Integer(1000)));
|
||||
}
|
||||
|
||||
public void testParseNumber() throws ParseException {
|
||||
assertEquals(new Integer(-1000).intValue(), ((Number) format
|
||||
.parseObject("-1,000")).intValue());
|
||||
}
|
||||
|
||||
public void testFormatDate() {
|
||||
assertEquals("1/1/70", format.format(new GregorianCalendar(1970,
|
||||
Calendar.JANUARY, 01).getTime()));
|
||||
}
|
||||
|
||||
public void testParseDate() throws ParseException {
|
||||
assertEquals(new GregorianCalendar(1970, Calendar.JANUARY, 01)
|
||||
.getTime(), format.parseObject("1/1/70"));
|
||||
}
|
||||
|
||||
public void testFormatObject() {
|
||||
try {
|
||||
format.format(new Object());
|
||||
fail("expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// okay
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseGarbage() {
|
||||
try {
|
||||
format.parseObject("garbage");
|
||||
fail("expected ParseException");
|
||||
} catch (ParseException e) {
|
||||
//okay
|
||||
}
|
||||
}
|
||||
}
|
@ -56,7 +56,6 @@ public static Test suite() {
|
||||
suite.addTest(StrMatcherTest.suite());
|
||||
suite.addTest(StrSubstitutorTest.suite());
|
||||
suite.addTest(StrTokenizerTest.suite());
|
||||
suite.addTestSuite(MultiFormatTest.class);
|
||||
suite.addTestSuite(MessageFormatTest.US.class);
|
||||
suite.addTestSuite(MessageFormatTest.UK.class);
|
||||
suite.addTestSuite(MessageFormatTest.DE.class);
|
||||
|
Loading…
x
Reference in New Issue
Block a user