Coverage Report - org.apache.commons.lang3.text.CompositeFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeFormat
100%
9/9
N/A
1
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3.text;
 18  
 
 19  
 import java.text.FieldPosition;
 20  
 import java.text.Format;
 21  
 import java.text.ParseException;
 22  
 import java.text.ParsePosition;
 23  
 
 24  
 /**
 25  
  * Formats using one formatter and parses using a different formatter. An
 26  
  * example of use for this would be a webapp where data is taken in one way and
 27  
  * stored in a database another way.
 28  
  */
 29  
 public class CompositeFormat extends Format {
 30  
 
 31  
     /**
 32  
      * Required for serialization support.
 33  
      * 
 34  
      * @see java.io.Serializable
 35  
      */
 36  
     private static final long serialVersionUID = -4329119827877627683L;
 37  
 
 38  
     /** The parser to use. */
 39  
     private final Format parser;
 40  
     /** The formatter to use. */
 41  
     private final Format formatter;
 42  
 
 43  
     /**
 44  
      * Create a format that points its parseObject method to one implementation
 45  
      * and its format method to another.
 46  
      * 
 47  
      * @param parser implementation
 48  
      * @param formatter implementation
 49  
      */
 50  2
     public CompositeFormat(final Format parser, final Format formatter) {
 51  2
         this.parser = parser;
 52  2
         this.formatter = formatter;
 53  2
     }
 54  
 
 55  
     /**
 56  
      * Uses the formatter Format instance.
 57  
      * 
 58  
      * @param obj the object to format
 59  
      * @param toAppendTo the {@link StringBuffer} to append to
 60  
      * @param pos the FieldPosition to use (or ignore).
 61  
      * @return <code>toAppendTo</code>
 62  
      * @see Format#format(Object, StringBuffer, FieldPosition)
 63  
      */
 64  
     @Override // Therefore has to use StringBuffer
 65  
     public StringBuffer format(final Object obj, final StringBuffer toAppendTo,
 66  
             final FieldPosition pos) {
 67  3
         return formatter.format(obj, toAppendTo, pos);
 68  
     }
 69  
 
 70  
     /**
 71  
      * Uses the parser Format instance.
 72  
      * 
 73  
      * @param source the String source
 74  
      * @param pos the ParsePosition containing the position to parse from, will
 75  
      *            be updated according to parsing success (index) or failure
 76  
      *            (error index)
 77  
      * @return the parsed Object
 78  
      * @see Format#parseObject(String, ParsePosition)
 79  
      */
 80  
     @Override
 81  
     public Object parseObject(final String source, final ParsePosition pos) {
 82  3
         return parser.parseObject(source, pos);
 83  
     }
 84  
 
 85  
     /**
 86  
      * Provides access to the parser Format implementation.
 87  
      * 
 88  
      * @return parser Format implementation
 89  
      */
 90  
     public Format getParser() {
 91  1
         return this.parser;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Provides access to the parser Format implementation.
 96  
      * 
 97  
      * @return formatter Format implementation
 98  
      */
 99  
     public Format getFormatter() {
 100  1
         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  1
         return format(parseObject(input));
 112  
     }
 113  
 
 114  
 }