View Javadoc
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  
19  import java.text.FieldPosition;
20  import java.text.Format;
21  import java.text.ParseException;
22  import java.text.ParsePosition;
23  
24  /**
25   * Formats using one formatter and parses using a different formatter. An
26   * example of use for this would be a webapp where data is taken in one way and
27   * stored in a database another way.
28   * @deprecated as of 3.6, use commons-text
29   * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/CompositeFormat.html">
30   * CompositeFormat</a> instead
31   */
32  @Deprecated
33  public class CompositeFormat extends Format {
34  
35      /**
36       * Required for serialization support.
37       *
38       * @see java.io.Serializable
39       */
40      private static final long serialVersionUID = -4329119827877627683L;
41  
42      /** The parser to use. */
43      private final Format parser;
44      /** The formatter to use. */
45      private final Format formatter;
46  
47      /**
48       * Create a format that points its parseObject method to one implementation
49       * and its format method to another.
50       *
51       * @param parser implementation
52       * @param formatter implementation
53       */
54      public CompositeFormat(final Format parser, final Format formatter) {
55          this.parser = parser;
56          this.formatter = formatter;
57      }
58  
59      /**
60       * Uses the formatter Format instance.
61       *
62       * @param obj the object to format
63       * @param toAppendTo the {@link StringBuffer} to append to
64       * @param pos the FieldPosition to use (or ignore).
65       * @return <code>toAppendTo</code>
66       * @see Format#format(Object, StringBuffer, FieldPosition)
67       */
68      @Override // Therefore has to use StringBuffer
69      public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
70              final FieldPosition pos) {
71          return formatter.format(obj, toAppendTo, pos);
72      }
73  
74      /**
75       * Uses the parser Format instance.
76       *
77       * @param source the String source
78       * @param pos the ParsePosition containing the position to parse from, will
79       *            be updated according to parsing success (index) or failure
80       *            (error index)
81       * @return the parsed Object
82       * @see Format#parseObject(String, ParsePosition)
83       */
84      @Override
85      public Object parseObject(final String source, final ParsePosition pos) {
86          return parser.parseObject(source, pos);
87      }
88  
89      /**
90       * Provides access to the parser Format implementation.
91       *
92       * @return parser Format implementation
93       */
94      public Format getParser() {
95          return this.parser;
96      }
97  
98      /**
99       * Provides access to the parser Format implementation.
100      *
101      * @return formatter Format implementation
102      */
103     public Format getFormatter() {
104         return this.formatter;
105     }
106 
107     /**
108      * Utility method to parse and then reformat a String.
109      *
110      * @param input String to reformat
111      * @return A reformatted String
112      * @throws ParseException thrown by parseObject(String) call
113      */
114     public String reformat(final String input) throws ParseException {
115         return format(parseObject(input));
116     }
117 
118 }