package util.lang;

import org.apache.commons.lang.StringUtils;

public class EnhancedBuilder {

                private StringBuilder builder = new StringBuilder();
                private String previous = null;
                private boolean first = true;
                private boolean finalized = false;

                private boolean skipFirstPrefix = false;
                private String firstPrefix = "";
                private String prefix = "";
                private String lastPrefix = "";
                private boolean skipLastPrefix = false;

                private boolean skipFirstSuffix = false;
                private String firstSuffix = "";
                private String suffix = "";
                private String lastSuffix = "";
                private boolean skipLastSuffix = false;

                private boolean ignoreBlank = false;

                public EnhancedBuilder() {
                }

                public EnhancedBuilder(String value) {
                                this.append(value);
                }

                public EnhancedBuilder(String value, String prefix, String 
suffix) {
                                this.prefix = prefix;
                                this.suffix = suffix;
                                this.append(value);
                }

                public EnhancedBuilder reset() {
                                previous = null;
                                first = true;
                                finalized = false;

                                skipFirstPrefix = false;
                                firstPrefix = "";
                                prefix = "";
                                lastPrefix = "";
                                skipLastPrefix = false;

                                skipFirstSuffix = false;
                                firstSuffix = "";
                                suffix = "";
                                lastSuffix = "";
                                skipLastSuffix = false;
                                builder = new StringBuilder();
                                return this;
                }

                public EnhancedBuilder append(Long value) {
                                if (value != null) append(value.toString());
                                return this;
                }

                public EnhancedBuilder append(Integer value) {
                                if (value != null) append(value.toString());
                                return this;
                }

                public EnhancedBuilder append(Double value) {
                                if (value != null) append(value.toString());
                                return this;
                }

                public EnhancedBuilder append(Float value) {
                                if (value != null) append(value.toString());
                                return this;
                }

                public EnhancedBuilder append(String item) {
                                if (ignoreBlank && StringUtils.isEmpty(item)) {
                                                return this;
                                }
                                if (previous != null) {
                                                if (first) {
                                                                if 
(!skipFirstPrefix) {
                                                                                
builder.append(StringUtils.isEmpty(firstPrefix) ? prefix : firstPrefix);
                                                                }
                                                                
builder.append(previous);
                                                                if 
(!skipFirstSuffix) {
                                                                                
builder.append(StringUtils.isEmpty(firstSuffix) ? suffix : firstSuffix);

                                                                }
                                                                first = false;
                                                } else {
                                                                
builder.append(prefix + previous + suffix);
                                                }

                                                previous = item;
                                } else {
                                                previous = item;
                                }
                                return this;
                }

                public String toString() {
                                if (!finalized) {
                                                if (!skipLastPrefix) {
                                                                
builder.append(StringUtils.isEmpty(lastPrefix) ? prefix : lastPrefix);

                                                }
                                                builder.append(previous);
                                                if (!skipLastSuffix) {
                                                                
builder.append(StringUtils.isEmpty(lastSuffix) ? suffix : lastSuffix);

                                                }
                                                finalized = true;
                                }
                                return builder.toString();
                }

                public boolean isSkipFirstPrefix() {
                                return skipFirstPrefix;
                }

                public EnhancedBuilder setSkipFirstPrefix(boolean 
skipFirstPrefix) {
                                this.skipFirstPrefix = skipFirstPrefix;
                                return this;
                }

                public String getFirstPrefix() {
                                return firstPrefix;
                }

                public EnhancedBuilder setFirstPrefix(String firstPrefix) {
                                this.firstPrefix = firstPrefix;
                                return this;
                }

                public String getPrefix() {
                                return prefix;
                }

                public EnhancedBuilder setPrefix(String prefix) {
                                this.prefix = prefix;
                                return this;
                }

                public String getLastPrefix() {
                                return lastPrefix;
                }

                public EnhancedBuilder setLastPrefix(String lastPrefix) {
                                this.lastPrefix = lastPrefix;
                                return this;
                }

                public boolean isSkipLastPrefix() {
                                return skipLastPrefix;
                }

                public EnhancedBuilder setSkipLastPrefix(boolean 
skipLastPrefix) {
                                this.skipLastPrefix = skipLastPrefix;
                                return this;
                }

                public boolean isSkipFirstSuffix() {
                                return skipFirstSuffix;
                }

                public EnhancedBuilder setSkipFirstSuffix(boolean 
skipFirstSuffix) {
                                this.skipFirstSuffix = skipFirstSuffix;
                                return this;
                }

                public String getFirstSuffix() {
                                return firstSuffix;
                }

                public EnhancedBuilder setFirstSuffix(String firstSuffix) {
                                this.firstSuffix = firstSuffix;
                                return this;
                }

                public String getSuffix() {
                                return suffix;
                }

                public EnhancedBuilder setSuffix(String suffix) {
                                this.suffix = suffix;
                                return this;
                }

                public String getLastSuffix() {
                                return lastSuffix;
                }

                public EnhancedBuilder setLastSuffix(String lastSuffix) {
                                this.lastSuffix = lastSuffix;
                                return this;
                }

                public boolean isSkipLastSuffix() {
                                return skipLastSuffix;
                }

                public EnhancedBuilder setSkipLastSuffix(boolean 
skipLastSuffix) {
                                this.skipLastSuffix = skipLastSuffix;
                                return this;
                }

                public boolean isIgnoreBlank() {
                                return ignoreBlank;
                }

                public void setIgnoreBlank(boolean ignoreBlank) {
                                this.ignoreBlank = ignoreBlank;
                }

                public static void main(String[] args) {
                                EnhancedBuilder eb = new EnhancedBuilder();
                                eb.setFirstSuffix(": \n")
                                .setSuffix(",\n")
                                .setPrefix(" -")
                                .setLastSuffix(".")
                                .setSkipFirstPrefix(true);
                                System.out.println(eb.append("Please specify 
the following 
arguments").append("Username").append("Cell").append("Email").toString());

                }

}





Regards
Darryl Smith

From: Darryl Smith [mailto:darryl.sm...@vcontractor.co.za]
Sent: 15 December 2011 10:38 AM
To: dev@commons.apache.org
Subject: [lang] - Request to add a wrapper class for StringBuilder to handle 
the adding of suffixes and prefixes in output strings in certain cases.

The intended purpose is to be able to format Strings with text intermixed with 
variable String values that conform to correct punctuation.

i.e.

Building up the correct display String depending on which variables are 
correctly entered.
"Please specify the following: " followed by a comma delimited listing of the 
required values [Name | Cell | Email Address] terminated by a fullstop.

EnhancedBuilder allows us to easily specify the logic to build such a String.

                EnhancedBuilder eb = new EnhancedBuilder();
                                eb.setFirstSuffix(": \n")
                                .setSuffix(",\n")
                                .setPrefix(" -")
                                .setLastSuffix(".")
                                .setSkipFirstPrefix(true);
                                System.out.println(eb.append("Please specify 
the following 
arguments").append("Username").append("Cell").append("Email").toString());
This e-mail is classified C2 - Vodacom Restricted - Information to be used 
inside Vodacom but it may be shared with authorised partners. "This e-mail is 
sent on the Terms and Conditions that can be accessed by Clicking on this link 
http://www.vodacom.co.za/vodacom/terms+and+conditions " "This e-mail is sent on 
the Terms and Conditions that can be accessed by Clicking on this link 
http://www.vodacom.co.za/vodacom/terms+and+conditions "

“This e-mail is sent on the Terms and Conditions that can be accessed by 
Clicking on this link www.vodacom.co.za/vodacom/terms+and+conditions "

Reply via email to