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