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