001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.util;
017    
018    import java.io.ByteArrayOutputStream;
019    import java.io.PrintStream;
020    
021    /**
022     * Builds a <code>{@link String}</code> instance using a pattern similar to the varargs printf() variety.
023     * 
024     * 
025     */
026    public class PatternedStringBuilder {
027        private String _pattern;
028    
029        /**
030         * Constructor that takes a pattern
031         * 
032         * @param pattern
033         */
034        public PatternedStringBuilder(String pattern) {
035            setPattern(pattern);
036        }
037    
038        /**
039         * Write accessor method for pattern
040         * 
041         * @param pattern
042         */
043        public void setPattern(String pattern) {
044            _pattern = pattern;
045        }
046    
047        /**
048         * Read accessor method for pattern
049         * 
050         * @return String
051         */
052        public String getPattern() {
053            return _pattern;
054        }
055    
056        /**
057         * Takes an ellipses of <code>{@link String}</code> parameters and builds a <code>{@link String}</code> instance from them
058         * and the pattern given earlier.
059         * 
060         * @param args
061         * @return String
062         */
063        public String sprintf(Object... args) {
064            ByteArrayOutputStream retval = new ByteArrayOutputStream();
065    
066            new PrintStream(retval).printf(getPattern(), args);
067    
068            return retval.toString();
069        }
070    }