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.lang3.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.  * @deprecated As of 3.6, use Apache Commons Text
  27.  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/CompositeFormat.html">
  28.  * CompositeFormat</a> instead
  29.  */
  30. @Deprecated
  31. public class CompositeFormat extends Format {

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

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

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

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

  67.     /**
  68.      * Provides access to the parser Format implementation.
  69.      *
  70.      * @return formatter Format implementation
  71.      */
  72.     public Format getFormatter() {
  73.         return this.formatter;
  74.     }

  75.     /**
  76.      * Provides access to the parser Format implementation.
  77.      *
  78.      * @return parser Format implementation
  79.      */
  80.     public Format getParser() {
  81.         return this.parser;
  82.     }

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

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

  107. }