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.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 *
029 * @since 1.0
030 */
031public class CompositeFormat extends Format {
032
033    /**
034     * Required for serialization support.
035     *
036     * @see java.io.Serializable
037     */
038    private static final long serialVersionUID = -4329119827877627683L;
039
040    /** The parser to use. */
041    private final Format parser;
042
043    /** The formatter to use. */
044    private final Format formatter;
045
046    /**
047     * Constructs a format that points its parseObject method to one implementation
048     * and its format method to another.
049     *
050     * @param parser implementation
051     * @param formatter implementation
052     */
053    public CompositeFormat(final Format parser, final Format formatter) {
054        this.parser = parser;
055        this.formatter = formatter;
056    }
057
058    /**
059     * Formats the input.
060     *
061     * @param obj the object to format
062     * @param toAppendTo the {@link StringBuffer} to append to
063     * @param pos the FieldPosition to use (or ignore).
064     * @return {@code toAppendTo}
065     * @see Format#format(Object, StringBuffer, FieldPosition)
066     */
067    @Override // Therefore has to use StringBuffer
068    public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
069            final FieldPosition pos) {
070        return formatter.format(obj, toAppendTo, pos);
071    }
072
073    /**
074     * Gets the parser Format implementation.
075     *
076     * @return formatter Format implementation
077     */
078    public Format getFormatter() {
079        return this.formatter;
080    }
081
082    /**
083     * Gets the parser Format implementation.
084     *
085     * @return parser Format implementation
086     */
087    public Format getParser() {
088        return this.parser;
089    }
090
091    /**
092     * Parses the input.
093     *
094     * @param source the String source
095     * @param pos the ParsePosition containing the position to parse from, will
096     *            be updated according to parsing success (index) or failure
097     *            (error index)
098     * @return The parsed Object
099     * @see Format#parseObject(String, ParsePosition)
100     */
101    @Override
102    public Object parseObject(final String source, final ParsePosition pos) {
103        return parser.parseObject(source, pos);
104    }
105
106    /**
107     * Parses and then reformats a String.
108     *
109     * @param input String to reformat
110     * @return A reformatted String
111     * @throws ParseException thrown by parseObject(String) call
112     */
113    public String reformat(final String input) throws ParseException {
114        return format(parseObject(input));
115    }
116
117}