View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.util;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.PrintStream;
20  
21  /**
22   * Builds a <code>{@link String}</code> instance using a pattern similar to the varargs printf() variety.
23   * 
24   * 
25   */
26  public class PatternedStringBuilder {
27      private String _pattern;
28  
29      /**
30       * Constructor that takes a pattern
31       * 
32       * @param pattern
33       */
34      public PatternedStringBuilder(String pattern) {
35          setPattern(pattern);
36      }
37  
38      /**
39       * Write accessor method for pattern
40       * 
41       * @param pattern
42       */
43      public void setPattern(String pattern) {
44          _pattern = pattern;
45      }
46  
47      /**
48       * Read accessor method for pattern
49       * 
50       * @return String
51       */
52      public String getPattern() {
53          return _pattern;
54      }
55  
56      /**
57       * Takes an ellipses of <code>{@link String}</code> parameters and builds a <code>{@link String}</code> instance from them
58       * and the pattern given earlier.
59       * 
60       * @param args
61       * @return String
62       */
63      public String sprintf(Object... args) {
64          ByteArrayOutputStream retval = new ByteArrayOutputStream();
65  
66          new PrintStream(retval).printf(getPattern(), args);
67  
68          return retval.toString();
69      }
70  }