CompositeFormat.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text;

  18. import java.text.FieldPosition;
  19. import java.text.Format;
  20. import java.text.ParseException;
  21. import java.text.ParsePosition;

  22. /**
  23.  * Formats using one formatter and parses using a different formatter. An
  24.  * example of use for this would be a webapp where data is taken in one way and
  25.  * stored in a database another way.
  26.  *
  27.  * @since 1.0
  28.  */
  29. public class CompositeFormat extends Format {

  30.     /**
  31.      * Required for serialization support.
  32.      *
  33.      * @see java.io.Serializable
  34.      */
  35.     private static final long serialVersionUID = -4329119827877627683L;

  36.     /** The parser to use. */
  37.     private final Format parser;

  38.     /** The formatter to use. */
  39.     private final Format formatter;

  40.     /**
  41.      * Constructs a format that points its parseObject method to one implementation
  42.      * and its format method to another.
  43.      *
  44.      * @param parser implementation
  45.      * @param formatter implementation
  46.      */
  47.     public CompositeFormat(final Format parser, final Format formatter) {
  48.         this.parser = parser;
  49.         this.formatter = formatter;
  50.     }

  51.     /**
  52.      * Formats the input.
  53.      *
  54.      * @param obj the object to format
  55.      * @param toAppendTo the {@link StringBuffer} to append to
  56.      * @param pos the FieldPosition to use (or ignore).
  57.      * @return {@code toAppendTo}
  58.      * @see Format#format(Object, StringBuffer, FieldPosition)
  59.      */
  60.     @Override // Therefore has to use StringBuffer
  61.     public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
  62.             final FieldPosition pos) {
  63.         return formatter.format(obj, toAppendTo, pos);
  64.     }

  65.     /**
  66.      * Gets the parser Format implementation.
  67.      *
  68.      * @return formatter Format implementation
  69.      */
  70.     public Format getFormatter() {
  71.         return this.formatter;
  72.     }

  73.     /**
  74.      * Gets the parser Format implementation.
  75.      *
  76.      * @return parser Format implementation
  77.      */
  78.     public Format getParser() {
  79.         return this.parser;
  80.     }

  81.     /**
  82.      * Parses the input.
  83.      *
  84.      * @param source the String source
  85.      * @param pos the ParsePosition containing the position to parse from, will
  86.      *            be updated according to parsing success (index) or failure
  87.      *            (error index)
  88.      * @return The parsed Object
  89.      * @see Format#parseObject(String, ParsePosition)
  90.      */
  91.     @Override
  92.     public Object parseObject(final String source, final ParsePosition pos) {
  93.         return parser.parseObject(source, pos);
  94.     }

  95.     /**
  96.      * Parses and then reformats a String.
  97.      *
  98.      * @param input String to reformat
  99.      * @return A reformatted String
  100.      * @throws ParseException thrown by parseObject(String) call
  101.      */
  102.     public String reformat(final String input) throws ParseException {
  103.         return format(parseObject(input));
  104.     }

  105. }