You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Darryl Smith <Da...@vcontractor.co.za> on 2011/12/15 09:37:41 UTC

[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 www.vodacom.co.za/vodacom/terms+and+conditions "

RE: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by Darryl Smith <Da...@vcontractor.co.za>.
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.Smith@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 "

Re: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by Adrian Crum <ad...@sandglass-software.com>.
It seems to me it would be simpler to implement a String format extension for specifying collections (%l):

“Please specify the following: %l%n“, [Name, Cell, Email Address]

-Adrian

On 12/15/2011 11:25 AM, James Carman wrote:
> At first glance, I don't know how "general" this is.  It seems very
> limited in scope.  However, I can see how I would have used this in my
> code at times.  I guess I'm on the fence about this one.
>
> On Thu, Dec 15, 2011 at 3:37 AM, Darryl Smith
> <Da...@vcontractor.co.za>  wrote:
>> 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 "
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by Gary Gregory <ga...@gmail.com>.
On Dec 15, 2011, at 6:37, Adrian Crum
<ad...@sandglass-software.com> wrote:

> It seems to me it would be simpler to implement a String format extension for specifying collections (%l):
>
> “Please specify the following: %l%n“, [Name, Cell, Email Address]
>

That feels better to me than a SB wrapper. And much easier to read to boot.

Gary

> -Adrian
>
> On 12/15/2011 11:25 AM, James Carman wrote:
>> At first glance, I don't know how "general" this is.  It seems very
>> limited in scope.  However, I can see how I would have used this in my
>> code at times.  I guess I'm on the fence about this one.
>>
>> On Thu, Dec 15, 2011 at 3:37 AM, Darryl Smith
>> <Da...@vcontractor.co.za>  wrote:
>>> 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 "
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>>> For additional commands, e-mail: dev-help@commons.apache.org
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by James Carman <ja...@carmanconsulting.com>.
At first glance, I don't know how "general" this is.  It seems very
limited in scope.  However, I can see how I would have used this in my
code at times.  I guess I'm on the fence about this one.

On Thu, Dec 15, 2011 at 3:37 AM, Darryl Smith
<Da...@vcontractor.co.za> wrote:
> 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 "
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by sebb <se...@gmail.com>.
On 15 December 2011 08:37, Darryl Smith <Da...@vcontractor.co.za> wrote:
> The intended purpose is to be able to format Strings with text intermixed
> with variable String values that conform to correct punctuation.
>

The normal route for enhancement requests is via the issue tracker, i.e. JIRA

http://commons.apache.org/lang/issue-tracking.html

Please create a login and post your request there.

>
> 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 "
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by Gary Gregory <ga...@gmail.com>.
On Dec 15, 2011, at 3:38, Darryl Smith <Da...@vcontractor.co.za>
wrote:

  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());


How is this better than using String.format for example? The proposal feels
a little out of scope for [lang].

Gary

 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 "


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org

Re: [lang] - Request to add a wrapper class for StringBuilder to handle the adding of suffixes and prefixes in output strings in certain cases.

Posted by Konstantin Kolinko <kn...@gmail.com>.
2011/12/15 Darryl Smith <Da...@vcontractor.co.za>:
> 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.

Arrays.asList("Name", "Cell", "Email Address").toString(),

Or maybe use some configurable join() method for a collection?

>From quick look there are similar things in o.a.commons.lang3.builder:
ToStringBuilder, ToStringStyle, StandardToStringStyle.

ToStringBuilder says you should not create it without an object, but
you can use ToStringStyle.append(Object[]) directly on a StringBuffer.


>
>
>
> 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());
>

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org