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 * https://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 * 029 * @deprecated As of <a href="https://commons.apache.org/proper/commons-lang/changes-report.html#a3.6">3.6</a>, use Apache Commons Text 030 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/CompositeFormat.html"> 031 * CompositeFormat</a>. 032 */ 033@Deprecated 034public class CompositeFormat extends Format { 035 036 /** 037 * Required for serialization support. 038 * 039 * @see java.io.Serializable 040 */ 041 private static final long serialVersionUID = -4329119827877627683L; 042 043 /** The parser to use. */ 044 private final Format parser; 045 046 /** The formatter to use. */ 047 private final Format formatter; 048 049 /** 050 * Create a format that points its parseObject method to one implementation 051 * and its format method to another. 052 * 053 * @param parser implementation 054 * @param formatter implementation 055 */ 056 public CompositeFormat(final Format parser, final Format formatter) { 057 this.parser = parser; 058 this.formatter = formatter; 059 } 060 061 /** 062 * Uses the formatter Format instance. 063 * 064 * @param obj the object to format 065 * @param toAppendTo the {@link StringBuffer} to append to 066 * @param pos the FieldPosition to use (or ignore). 067 * @return {@code toAppendTo} 068 * @see Format#format(Object, StringBuffer, FieldPosition) 069 */ 070 @Override // Therefore has to use StringBuffer 071 public StringBuffer format(final Object obj, final StringBuffer toAppendTo, 072 final FieldPosition pos) { 073 return formatter.format(obj, toAppendTo, pos); 074 } 075 076 /** 077 * Provides access to the parser Format implementation. 078 * 079 * @return formatter Format implementation 080 */ 081 public Format getFormatter() { 082 return this.formatter; 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 * Uses the parser Format instance. 096 * 097 * @param source the String source 098 * @param pos the ParsePosition containing the position to parse from, will 099 * 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}