Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: Other

      Description

      I wonder if this can be added.

      /**

      • 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;
        public CompositeFormat(final Format parser, final Format formatter) { this.parser = parser; this.formatter = formatter; }

        public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
        final FieldPosition pos)

        { return formatter.format(obj,toAppendTo,pos); }

        public Object parseObject(final String source, final ParsePosition pos)

        { return parser.parseObject(source,pos); }

        }

        Activity

        Archimedes Trajano created issue -
        Hide
        Henri Yandell added a comment -

        General questions....

        Should there be set and/or get methods for the parser and formatter instances?

        Show
        Henri Yandell added a comment - General questions.... Should there be set and/or get methods for the parser and formatter instances?
        Hide
        Henri Yandell added a comment -

        svn ci -m "Added new class CompositeFormat, as per issue #30184
        (http://issues.apache.org/bugzilla/show_bug.cgi?id=30184)"
        Adding src/java/org/apache/commons/lang/text/CompositeFormat.java
        Adding src/test/org/apache/commons/lang/text/CompositeFormatTest.java
        Sending src/test/org/apache/commons/lang/text/TextTestSuite.java
        Transmitting file data ...
        Committed revision 394617.

        Added a get pair of methods for the parser/formatter. A unit test, javadoc etc.
        Left the @author from the original reporter in.

        Show
        Henri Yandell added a comment - svn ci -m "Added new class CompositeFormat, as per issue #30184 ( http://issues.apache.org/bugzilla/show_bug.cgi?id=30184 )" Adding src/java/org/apache/commons/lang/text/CompositeFormat.java Adding src/test/org/apache/commons/lang/text/CompositeFormatTest.java Sending src/test/org/apache/commons/lang/text/TextTestSuite.java Transmitting file data ... Committed revision 394617. Added a get pair of methods for the parser/formatter. A unit test, javadoc etc. Left the @author from the original reporter in.
        Hide
        Archimedes Trajano added a comment -

        There shouldn't be a setter to make the class immutable to prevent accidental
        corruption of the Format. I am not sure if the getter should be there either
        but since formatters should be immutable I guess its okay.

        Show
        Archimedes Trajano added a comment - There shouldn't be a setter to make the class immutable to prevent accidental corruption of the Format. I am not sure if the getter should be there either but since formatters should be immutable I guess its okay.
        Hide
        Archimedes Trajano added a comment -

        Additional method that may be useful
        /**

        • Parses a string and formats it with {@link #formatter}

          .

        • @param source
        • string to parse
        • @return formatted string
        • @throws ParseException
        • error parsing
          */
          public String reformat(final String source) throws ParseException { return format(parseObject(source)); }
        Show
        Archimedes Trajano added a comment - Additional method that may be useful /** Parses a string and formats it with {@link #formatter} . @param source string to parse @return formatted string @throws ParseException error parsing */ public String reformat(final String source) throws ParseException { return format(parseObject(source)); }
        Hide
        Archimedes Trajano added a comment -

        Unit test

        package net.trajano;

        import java.text.Format;
        import java.text.SimpleDateFormat;
        import java.util.Locale;

        import junit.framework.TestCase;

        public class CompositeFormatTest extends TestCase {
        public void testCompositeFormat() throws Exception

        { Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH); Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); CompositeFormat c = new CompositeFormat(f1, f2); String testString = "January 3, 2005"; assertEquals(testString, c.format(c.parseObject("01032005"))); assertEquals(testString, c.reformat("01032005")); }

        }

        Show
        Archimedes Trajano added a comment - Unit test package net.trajano; import java.text.Format; import java.text.SimpleDateFormat; import java.util.Locale; import junit.framework.TestCase; public class CompositeFormatTest extends TestCase { public void testCompositeFormat() throws Exception { Format f1 = new SimpleDateFormat("MMddyyyy", Locale.ENGLISH); Format f2 = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); CompositeFormat c = new CompositeFormat(f1, f2); String testString = "January 3, 2005"; assertEquals(testString, c.format(c.parseObject("01032005"))); assertEquals(testString, c.reformat("01032005")); } }
        Hide
        Henri Yandell added a comment -

        Thanks Archimedes,

        Code applied.

        Sending src/java/org/apache/commons/lang/text/CompositeFormat.java
        Sending src/test/org/apache/commons/lang/text/CompositeFormatTest.java
        Transmitting file data ..
        Committed revision 398536.

        Show
        Henri Yandell added a comment - Thanks Archimedes, Code applied. Sending src/java/org/apache/commons/lang/text/CompositeFormat.java Sending src/test/org/apache/commons/lang/text/CompositeFormatTest.java Transmitting file data .. Committed revision 398536.
        Henri Yandell made changes -
        Field Original Value New Value
        issue.field.bugzillaimportkey 30184 12341580
        Henri Yandell made changes -
        Assignee Henri Yandell [ bayard ]
        Fix Version/s 2.2 [ 12311686 ]
        Project Commons [ 12310458 ] Commons Lang [ 12310481 ]
        Key COM-1428 LANG-246
        Component/s Lang [ 12311121 ]
        Affects Version/s unspecified [ 12311647 ]
        Henri Yandell made changes -
        Fix Version/s 2.2 [ 12311702 ]
        Henri Yandell made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Workflow jira [ 12370485 ] Default workflow, editable Closed status [ 12602116 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Resolved Resolved Closed Closed
        931d 23h 36m 1 Henri Yandell 06/Feb/07 23:47

          People

          • Assignee:
            Unassigned
            Reporter:
            Archimedes Trajano
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development