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    /** The formatter to use. */
043    private final Format formatter;
044
045    /**
046     * Create a format that points its parseObject method to one implementation
047     * and its format method to another.
048     * 
049     * @param parser implementation
050     * @param formatter implementation
051     */
052    public CompositeFormat(final Format parser, final Format formatter) {
053        this.parser = parser;
054        this.formatter = formatter;
055    }
056
057    /**
058     * Uses the formatter Format instance.
059     * 
060     * @param obj the object to format
061     * @param toAppendTo the {@link StringBuffer} to append to
062     * @param pos the FieldPosition to use (or ignore).
063     * @return <code>toAppendTo</code>
064     * @see Format#format(Object, StringBuffer, FieldPosition)
065     */
066    @Override // Therefore has to use StringBuffer
067    public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
068            final FieldPosition pos) {
069        return formatter.format(obj, toAppendTo, pos);
070    }
071
072    /**
073     * Uses the parser Format instance.
074     * 
075     * @param source the String source
076     * @param pos the ParsePosition containing the position to parse from, will
077     *            be updated according to parsing success (index) or failure
078     *            (error index)
079     * @return the parsed Object
080     * @see Format#parseObject(String, ParsePosition)
081     */
082    @Override
083    public Object parseObject(final String source, final ParsePosition pos) {
084        return parser.parseObject(source, pos);
085    }
086
087    /**
088     * Provides access to the parser Format implementation.
089     * 
090     * @return parser Format implementation
091     */
092    public Format getParser() {
093        return this.parser;
094    }
095
096    /**
097     * Provides access to the parser Format implementation.
098     * 
099     * @return formatter Format implementation
100     */
101    public Format getFormatter() {
102        return this.formatter;
103    }
104
105    /**
106     * Utility method to parse and then reformat a String.
107     * 
108     * @param input String to reformat
109     * @return A reformatted String
110     * @throws ParseException thrown by parseObject(String) call
111     */
112    public String reformat(final String input) throws ParseException {
113        return format(parseObject(input));
114    }
115
116}