    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.text;
    018
    019import java.text.FieldPosition;
    020import java.text.Format;
    021import java.text.ParseException;
    022import java.text.ParsePosition;
    023
    024/**
    025 * Formats using one formatter and parses using a different formatter. An
    026 * example of use for this would be a webapp where data is taken in one way and
    027 * stored in a database another way.
    028 * @deprecated As of 3.6, use Apache Commons Text
    029 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/CompositeFormat.html">
    030 * CompositeFormat</a> instead
    031 */
    032@Deprecated
    033public class CompositeFormat extends Format {
    034
    035    /**
    036     * Required for serialization support.
    037     *
    038     * @see java.io.Serializable
    039     */
    040    private static final long serialVersionUID = -4329119827877627683L;
    041
    042    /** The parser to use. */
    043    private final Format parser;
    044    /** The formatter to use. */
    045    private final Format formatter;
    046
    047    /**
    048     * Create a format that points its parseObject method to one implementation
    049     * and its format method to another.
    050     *
    051     * @param parser implementation
    052     * @param formatter implementation
    053     */
    054    public CompositeFormat(final Format parser, final Format formatter) {
    055        this.parser = parser;
    056        this.formatter = formatter;
    057    }
    058
    059    /**
    060     * Uses the formatter Format instance.
    061     *
    062     * @param obj the object to format
    063     * @param toAppendTo the {@link StringBuffer} to append to
    064     * @param pos the FieldPosition to use (or ignore).
    065     * @return {@code toAppendTo}
    066     * @see Format#format(Object, StringBuffer, FieldPosition)
    067     */
    068    @Override // Therefore has to use StringBuffer
    069    public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
    070            final FieldPosition pos) {
    071        return formatter.format(obj, toAppendTo, pos);
    072    }
    073
    074    /**
    075     * Uses the parser Format instance.
    076     *
    077     * @param source the String source
    078     * @param pos the ParsePosition containing the position to parse from, will
    079     *            be updated according to parsing success (index) or failure
    080     *            (error index)
    081     * @return the parsed Object
    082     * @see Format#parseObject(String, ParsePosition)
    083     */
    084    @Override
    085    public Object parseObject(final String source, final ParsePosition pos) {
    086        return parser.parseObject(source, pos);
    087    }
    088
    089    /**
    090     * Provides access to the parser Format implementation.
    091     *
    092     * @return parser Format implementation
    093     */
    094    public Format getParser() {
    095        return this.parser;
    096    }
    097
    098    /**
    099     * 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}