[text] Upper/Lower case enum

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

[text] Upper/Lower case enum

garydgregory
Does anyone think this is useful and general enough to add to Commons Text:

/**
 * Enumerates letter cases and converts strings.
 *
 * @author <a href="mailto:[hidden email]">Gary Gregory</a>
 */
public enum LetterCase {
    LOWER {
        @Override
        public String toCaseString(final String source, final Locale
locale) {
            return source.toLowerCase(locale);
        }

    },
    UPPER {
        @Override
        public String toCaseString(final String source, final Locale
locale) {
            return source.toUpperCase(locale);
        }
    };

    /**
     * Converts from the given {@code source} string to the case specified
by this enum using the given {@code locale}.
     *
     * @param source
     *            the string to convert
     * @param locale
     *            the locale to use for conversion.
     * @return a converted string.
     */
    public abstract String toCaseString(String source, Locale locale);
}

?

Thank you,
Gary
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

Otto Fowler
What problem does it solve?

On February 22, 2018 at 17:02:34, Gary Gregory ([hidden email])
wrote:

Does anyone think this is useful and general enough to add to Commons Text:

/**
* Enumerates letter cases and converts strings.
*
* @author <a href="mailto:[hidden email]">Gary Gregory</a>
*/
public enum LetterCase {
LOWER {
@Override
public String toCaseString(final String source, final Locale
locale) {
return source.toLowerCase(locale);
}

},
UPPER {
@Override
public String toCaseString(final String source, final Locale
locale) {
return source.toUpperCase(locale);
}
};

/**
* Converts from the given {@code source} string to the case specified
by this enum using the given {@code locale}.
*
* @param source
* the string to convert
* @param locale
* the locale to use for conversion.
* @return a converted string.
*/
public abstract String toCaseString(String source, Locale locale);
}

?

Thank you,
Gary
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
Use your imagination ;-)

I use it in the following contexts:
- To pass to a hex dump method to configure where the alpha chars should be
in lower case or upper case.
- To configure a parameterized JUnit test class to configure the case of
HTTP headers and values.
- To normalize input

Gary


On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <[hidden email]>
wrote:

> What problem does it solve?
>
> On February 22, 2018 at 17:02:34, Gary Gregory ([hidden email])
> wrote:
>
> Does anyone think this is useful and general enough to add to Commons
> Text:
>
> /**
> * Enumerates letter cases and converts strings.
> *
> * @author <a href="mailto:[hidden email]">Gary Gregory</a>
> */
> public enum LetterCase {
> LOWER {
> @Override
> public String toCaseString(final String source, final Locale
> locale) {
> return source.toLowerCase(locale);
> }
>
> },
> UPPER {
> @Override
> public String toCaseString(final String source, final Locale
> locale) {
> return source.toUpperCase(locale);
> }
> };
>
> /**
> * Converts from the given {@code source} string to the case specified
> by this enum using the given {@code locale}.
> *
> * @param source
> * the string to convert
> * @param locale
> * the locale to use for conversion.
> * @return a converted string.
> */
> public abstract String toCaseString(String source, Locale locale);
> }
>
> ?
>
> Thank you,
> Gary
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

sebb-2-2
On 22 February 2018 at 22:27, Gary Gregory <[hidden email]> wrote:
> Use your imagination ;-)

What would the new code look like?

How does it compare with code that does not have the enum?

> I use it in the following contexts:
> - To pass to a hex dump method to configure where the alpha chars should be
> in lower case or upper case.
> - To configure a parameterized JUnit test class to configure the case of
> HTTP headers and values.
> - To normalize input
>
> Gary
>
>
> On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <[hidden email]>
> wrote:
>
>> What problem does it solve?
>>
>> On February 22, 2018 at 17:02:34, Gary Gregory ([hidden email])
>> wrote:
>>
>> Does anyone think this is useful and general enough to add to Commons
>> Text:
>>
>> /**
>> * Enumerates letter cases and converts strings.
>> *
>> * @author <a href="mailto:[hidden email]">Gary Gregory</a>
>> */
>> public enum LetterCase {
>> LOWER {
>> @Override
>> public String toCaseString(final String source, final Locale
>> locale) {
>> return source.toLowerCase(locale);
>> }
>>
>> },
>> UPPER {
>> @Override
>> public String toCaseString(final String source, final Locale
>> locale) {
>> return source.toUpperCase(locale);
>> }
>> };
>>
>> /**
>> * Converts from the given {@code source} string to the case specified
>> by this enum using the given {@code locale}.
>> *
>> * @param source
>> * the string to convert
>> * @param locale
>> * the locale to use for conversion.
>> * @return a converted string.
>> */
>> public abstract String toCaseString(String source, Locale locale);
>> }
>>
>> ?
>>
>> Thank you,
>> Gary
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:

> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]> wrote:
> > Use your imagination ;-)
>
> What would the new code look like?
>

I posted the code at the start of this thread...

Gary


>
> How does it compare with code that does not have the enum?
>
> > I use it in the following contexts:
> > - To pass to a hex dump method to configure where the alpha chars should
> be
> > in lower case or upper case.
> > - To configure a parameterized JUnit test class to configure the case of
> > HTTP headers and values.
> > - To normalize input
> >
> > Gary
> >
> >
> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <[hidden email]>
> > wrote:
> >
> >> What problem does it solve?
> >>
> >> On February 22, 2018 at 17:02:34, Gary Gregory ([hidden email])
> >> wrote:
> >>
> >> Does anyone think this is useful and general enough to add to Commons
> >> Text:
> >>
> >> /**
> >> * Enumerates letter cases and converts strings.
> >> *
> >> * @author <a href="mailto:[hidden email]">Gary Gregory</a>
> >> */
> >> public enum LetterCase {
> >> LOWER {
> >> @Override
> >> public String toCaseString(final String source, final Locale
> >> locale) {
> >> return source.toLowerCase(locale);
> >> }
> >>
> >> },
> >> UPPER {
> >> @Override
> >> public String toCaseString(final String source, final Locale
> >> locale) {
> >> return source.toUpperCase(locale);
> >> }
> >> };
> >>
> >> /**
> >> * Converts from the given {@code source} string to the case specified
> >> by this enum using the given {@code locale}.
> >> *
> >> * @param source
> >> * the string to convert
> >> * @param locale
> >> * the locale to use for conversion.
> >> * @return a converted string.
> >> */
> >> public abstract String toCaseString(String source, Locale locale);
> >> }
> >>
> >> ?
> >>
> >> Thank you,
> >> Gary
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

sebb-2-2
On 22 February 2018 at 23:15, Gary Gregory <[hidden email]> wrote:
> On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
>
>> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]> wrote:
>> > Use your imagination ;-)
>>
>> What would the new code look like?

I mean the user code before and after the enum is introduced.

>>
>
> I posted the code at the start of this thread...
>
> Gary
>
>
>>
>> How does it compare with code that does not have the enum?
>>
>> > I use it in the following contexts:
>> > - To pass to a hex dump method to configure where the alpha chars should
>> be
>> > in lower case or upper case.
>> > - To configure a parameterized JUnit test class to configure the case of
>> > HTTP headers and values.
>> > - To normalize input
>> >
>> > Gary
>> >
>> >
>> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <[hidden email]>
>> > wrote:
>> >
>> >> What problem does it solve?
>> >>
>> >> On February 22, 2018 at 17:02:34, Gary Gregory ([hidden email])
>> >> wrote:
>> >>
>> >> Does anyone think this is useful and general enough to add to Commons
>> >> Text:
>> >>
>> >> /**
>> >> * Enumerates letter cases and converts strings.
>> >> *
>> >> * @author <a href="mailto:[hidden email]">Gary Gregory</a>
>> >> */
>> >> public enum LetterCase {
>> >> LOWER {
>> >> @Override
>> >> public String toCaseString(final String source, final Locale
>> >> locale) {
>> >> return source.toLowerCase(locale);
>> >> }
>> >>
>> >> },
>> >> UPPER {
>> >> @Override
>> >> public String toCaseString(final String source, final Locale
>> >> locale) {
>> >> return source.toUpperCase(locale);
>> >> }
>> >> };
>> >>
>> >> /**
>> >> * Converts from the given {@code source} string to the case specified
>> >> by this enum using the given {@code locale}.
>> >> *
>> >> * @param source
>> >> * the string to convert
>> >> * @param locale
>> >> * the locale to use for conversion.
>> >> * @return a converted string.
>> >> */
>> >> public abstract String toCaseString(String source, Locale locale);
>> >> }
>> >>
>> >> ?
>> >>
>> >> Thank you,
>> >> Gary
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:

> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]> wrote:
> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
> >
> >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]>
> wrote:
> >> > Use your imagination ;-)
> >>
> >> What would the new code look like?
>
> I mean the user code before and after the enum is introduced.
>

I don't have code for the _before_ since I wrote the enum to avoid it.

I have a different util class that gets called like this:

HexDump(byte[] data, LetterCase letterCase, more details...)

This is much clearer than using a boolean for the letter case.

Gary


>
> >>
> >
> > I posted the code at the start of this thread...
> >
> > Gary
> >
> >
> >>
> >> How does it compare with code that does not have the enum?
> >>
> >> > I use it in the following contexts:
> >> > - To pass to a hex dump method to configure where the alpha chars
> should
> >> be
> >> > in lower case or upper case.
> >> > - To configure a parameterized JUnit test class to configure the case
> of
> >> > HTTP headers and values.
> >> > - To normalize input
> >> >
> >> > Gary
> >> >
> >> >
> >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <[hidden email]
> >
> >> > wrote:
> >> >
> >> >> What problem does it solve?
> >> >>
> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
> [hidden email])
> >> >> wrote:
> >> >>
> >> >> Does anyone think this is useful and general enough to add to Commons
> >> >> Text:
> >> >>
> >> >> /**
> >> >> * Enumerates letter cases and converts strings.
> >> >> *
> >> >> * @author <a href="mailto:[hidden email]">Gary
> Gregory</a>
> >> >> */
> >> >> public enum LetterCase {
> >> >> LOWER {
> >> >> @Override
> >> >> public String toCaseString(final String source, final Locale
> >> >> locale) {
> >> >> return source.toLowerCase(locale);
> >> >> }
> >> >>
> >> >> },
> >> >> UPPER {
> >> >> @Override
> >> >> public String toCaseString(final String source, final Locale
> >> >> locale) {
> >> >> return source.toUpperCase(locale);
> >> >> }
> >> >> };
> >> >>
> >> >> /**
> >> >> * Converts from the given {@code source} string to the case specified
> >> >> by this enum using the given {@code locale}.
> >> >> *
> >> >> * @param source
> >> >> * the string to convert
> >> >> * @param locale
> >> >> * the locale to use for conversion.
> >> >> * @return a converted string.
> >> >> */
> >> >> public abstract String toCaseString(String source, Locale locale);
> >> >> }
> >> >>
> >> >> ?
> >> >>
> >> >> Thank you,
> >> >> Gary
> >> >>
> >> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

Peter Burka
This feels like it might have been more useful pre-Java 8. Why not use a
lamda function, which would permit arbitrary string transformations?

/peter

On Thu, Feb 22, 2018, 7:42 PM Gary Gregory <[hidden email]> wrote:

> On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
>
> > On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
> wrote:
> > > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
> > >
> > >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]>
> > wrote:
> > >> > Use your imagination ;-)
> > >>
> > >> What would the new code look like?
> >
> > I mean the user code before and after the enum is introduced.
> >
>
> I don't have code for the _before_ since I wrote the enum to avoid it.
>
> I have a different util class that gets called like this:
>
> HexDump(byte[] data, LetterCase letterCase, more details...)
>
> This is much clearer than using a boolean for the letter case.
>
> Gary
>
>
> >
> > >>
> > >
> > > I posted the code at the start of this thread...
> > >
> > > Gary
> > >
> > >
> > >>
> > >> How does it compare with code that does not have the enum?
> > >>
> > >> > I use it in the following contexts:
> > >> > - To pass to a hex dump method to configure where the alpha chars
> > should
> > >> be
> > >> > in lower case or upper case.
> > >> > - To configure a parameterized JUnit test class to configure the
> case
> > of
> > >> > HTTP headers and values.
> > >> > - To normalize input
> > >> >
> > >> > Gary
> > >> >
> > >> >
> > >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
> [hidden email]
> > >
> > >> > wrote:
> > >> >
> > >> >> What problem does it solve?
> > >> >>
> > >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
> > [hidden email])
> > >> >> wrote:
> > >> >>
> > >> >> Does anyone think this is useful and general enough to add to
> Commons
> > >> >> Text:
> > >> >>
> > >> >> /**
> > >> >> * Enumerates letter cases and converts strings.
> > >> >> *
> > >> >> * @author <a href="mailto:[hidden email]">Gary
> > Gregory</a>
> > >> >> */
> > >> >> public enum LetterCase {
> > >> >> LOWER {
> > >> >> @Override
> > >> >> public String toCaseString(final String source, final Locale
> > >> >> locale) {
> > >> >> return source.toLowerCase(locale);
> > >> >> }
> > >> >>
> > >> >> },
> > >> >> UPPER {
> > >> >> @Override
> > >> >> public String toCaseString(final String source, final Locale
> > >> >> locale) {
> > >> >> return source.toUpperCase(locale);
> > >> >> }
> > >> >> };
> > >> >>
> > >> >> /**
> > >> >> * Converts from the given {@code source} string to the case
> specified
> > >> >> by this enum using the given {@code locale}.
> > >> >> *
> > >> >> * @param source
> > >> >> * the string to convert
> > >> >> * @param locale
> > >> >> * the locale to use for conversion.
> > >> >> * @return a converted string.
> > >> >> */
> > >> >> public abstract String toCaseString(String source, Locale locale);
> > >> >> }
> > >> >>
> > >> >> ?
> > >> >>
> > >> >> Thank you,
> > >> >> Gary
> > >> >>
> > >> >>
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe, e-mail: [hidden email]
> > >> For additional commands, e-mail: [hidden email]
> > >>
> > >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

jochen-2
In reply to this post by Otto Fowler
On Thu, Feb 22, 2018 at 11:14 PM, Otto Fowler <[hidden email]> wrote:
> What problem does it solve?

Good for functional programming?

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

sebb-2-2
In reply to this post by garydgregory
On 23 February 2018 at 00:41, Gary Gregory <[hidden email]> wrote:

> On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
>
>> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]> wrote:
>> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
>> >
>> >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]>
>> wrote:
>> >> > Use your imagination ;-)
>> >>
>> >> What would the new code look like?
>>
>> I mean the user code before and after the enum is introduced.
>>
>
> I don't have code for the _before_ since I wrote the enum to avoid it.
>
> I have a different util class that gets called like this:
>
> HexDump(byte[] data, LetterCase letterCase, more details...)

The above is only using the enum as a way to provide the requested
type of transform.

The important bit is the code that uses the enum to do the transform.

> This is much clearer than using a boolean for the letter case.

YMMV

> Gary
>
>
>>
>> >>
>> >
>> > I posted the code at the start of this thread...
>> >
>> > Gary
>> >
>> >
>> >>
>> >> How does it compare with code that does not have the enum?
>> >>
>> >> > I use it in the following contexts:
>> >> > - To pass to a hex dump method to configure where the alpha chars
>> should
>> >> be
>> >> > in lower case or upper case.
>> >> > - To configure a parameterized JUnit test class to configure the case
>> of
>> >> > HTTP headers and values.
>> >> > - To normalize input
>> >> >
>> >> > Gary
>> >> >
>> >> >
>> >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <[hidden email]
>> >
>> >> > wrote:
>> >> >
>> >> >> What problem does it solve?
>> >> >>
>> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
>> [hidden email])
>> >> >> wrote:
>> >> >>
>> >> >> Does anyone think this is useful and general enough to add to Commons
>> >> >> Text:
>> >> >>
>> >> >> /**
>> >> >> * Enumerates letter cases and converts strings.
>> >> >> *
>> >> >> * @author <a href="mailto:[hidden email]">Gary
>> Gregory</a>
>> >> >> */
>> >> >> public enum LetterCase {
>> >> >> LOWER {
>> >> >> @Override
>> >> >> public String toCaseString(final String source, final Locale
>> >> >> locale) {
>> >> >> return source.toLowerCase(locale);
>> >> >> }
>> >> >>
>> >> >> },
>> >> >> UPPER {
>> >> >> @Override
>> >> >> public String toCaseString(final String source, final Locale
>> >> >> locale) {
>> >> >> return source.toUpperCase(locale);
>> >> >> }
>> >> >> };
>> >> >>
>> >> >> /**
>> >> >> * Converts from the given {@code source} string to the case specified
>> >> >> by this enum using the given {@code locale}.
>> >> >> *
>> >> >> * @param source
>> >> >> * the string to convert
>> >> >> * @param locale
>> >> >> * the locale to use for conversion.
>> >> >> * @return a converted string.
>> >> >> */
>> >> >> public abstract String toCaseString(String source, Locale locale);
>> >> >> }
>> >> >>
>> >> >> ?
>> >> >>
>> >> >> Thank you,
>> >> >> Gary
>> >> >>
>> >> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: [hidden email]
>> >> For additional commands, e-mail: [hidden email]
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

Matt Benson-3
On Feb 23, 2018 4:26 AM, "sebb" <[hidden email]> wrote:

On 23 February 2018 at 00:41, Gary Gregory <[hidden email]> wrote:
> On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
>
>> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
wrote:

>> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
>> >
>> >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]>
>> wrote:
>> >> > Use your imagination ;-)
>> >>
>> >> What would the new code look like?
>>
>> I mean the user code before and after the enum is introduced.
>>
>
> I don't have code for the _before_ since I wrote the enum to avoid it.
>
> I have a different util class that gets called like this:
>
> HexDump(byte[] data, LetterCase letterCase, more details...)

The above is only using the enum as a way to provide the requested
type of transform.

The important bit is the code that uses the enum to do the transform.


Obviously the method body would call #toCaseString() passing a String and
Locale as arguments. This question feels like trolling, as does the
insinuation that true/false *might not* be less clear in intent than
UPPER/LOWER, or vice versa as, again, we don't know what the hypothetical
API author was thinking in the case of boolean. WTH

The comment about Java 8 is fine as far as it goes, but doesn't really
invalidate this as the method supplied can easily be used as a BiFunction.
Gary, I'm thinking you might as well "underload" the method to pass the
default Locale to the two-argument variant so then you also implement
Function in the simple case. Then I'm sold. For a bonus you might provide
char method variants as well.

Matt


> This is much clearer than using a boolean for the letter case.

YMMV

> Gary
>
>
>>
>> >>
>> >
>> > I posted the code at the start of this thread...
>> >
>> > Gary
>> >
>> >
>> >>
>> >> How does it compare with code that does not have the enum?
>> >>
>> >> > I use it in the following contexts:
>> >> > - To pass to a hex dump method to configure where the alpha chars
>> should
>> >> be
>> >> > in lower case or upper case.
>> >> > - To configure a parameterized JUnit test class to configure the
case
>> of
>> >> > HTTP headers and values.
>> >> > - To normalize input
>> >> >
>> >> > Gary
>> >> >
>> >> >
>> >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
[hidden email]

>> >
>> >> > wrote:
>> >> >
>> >> >> What problem does it solve?
>> >> >>
>> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
>> [hidden email])
>> >> >> wrote:
>> >> >>
>> >> >> Does anyone think this is useful and general enough to add to
Commons

>> >> >> Text:
>> >> >>
>> >> >> /**
>> >> >> * Enumerates letter cases and converts strings.
>> >> >> *
>> >> >> * @author <a href="mailto:[hidden email]">Gary
>> Gregory</a>
>> >> >> */
>> >> >> public enum LetterCase {
>> >> >> LOWER {
>> >> >> @Override
>> >> >> public String toCaseString(final String source, final Locale
>> >> >> locale) {
>> >> >> return source.toLowerCase(locale);
>> >> >> }
>> >> >>
>> >> >> },
>> >> >> UPPER {
>> >> >> @Override
>> >> >> public String toCaseString(final String source, final Locale
>> >> >> locale) {
>> >> >> return source.toUpperCase(locale);
>> >> >> }
>> >> >> };
>> >> >>
>> >> >> /**
>> >> >> * Converts from the given {@code source} string to the case
specified

>> >> >> by this enum using the given {@code locale}.
>> >> >> *
>> >> >> * @param source
>> >> >> * the string to convert
>> >> >> * @param locale
>> >> >> * the locale to use for conversion.
>> >> >> * @return a converted string.
>> >> >> */
>> >> >> public abstract String toCaseString(String source, Locale locale);
>> >> >> }
>> >> >>
>> >> >> ?
>> >> >>
>> >> >> Thank you,
>> >> >> Gary
>> >> >>
>> >> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: [hidden email]
>> >> For additional commands, e-mail: [hidden email]
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
On Fri, Feb 23, 2018 at 7:03 AM, Matt Benson <[hidden email]> wrote:

> On Feb 23, 2018 4:26 AM, "sebb" <[hidden email]> wrote:
>
> On 23 February 2018 at 00:41, Gary Gregory <[hidden email]> wrote:
> > On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
> >
> >> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
> wrote:
> >> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
> >> >
> >> >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]>
> >> wrote:
> >> >> > Use your imagination ;-)
> >> >>
> >> >> What would the new code look like?
> >>
> >> I mean the user code before and after the enum is introduced.
> >>
> >
> > I don't have code for the _before_ since I wrote the enum to avoid it.
> >
> > I have a different util class that gets called like this:
> >
> > HexDump(byte[] data, LetterCase letterCase, more details...)
>
> The above is only using the enum as a way to provide the requested
> type of transform.
>
> The important bit is the code that uses the enum to do the transform.
>
>
> Obviously the method body would call #toCaseString() passing a String and
> Locale as arguments. This question feels like trolling, as does the
> insinuation that true/false *might not* be less clear in intent than
> UPPER/LOWER, or vice versa as, again, we don't know what the hypothetical
> API author was thinking in the case of boolean. WTH
>

Thanks Matt for pointing that out. The comment did feel trollish to me as
well but I choose not to engage. Aside from that I really like Sebb's
contributions to our community, his diligence and attention to detail.
I did not think I needed to make some pedantic point about an enum being
much better than a boolean to express letter case or toggles in general.


> The comment about Java 8 is fine as far as it goes, but doesn't really
> invalidate this as the method supplied can easily be used as a BiFunction.
> Gary, I'm thinking you might as well "underload" the method to pass the
> default Locale to the two-argument variant so then you also implement
> Function in the simple case. Then I'm sold. For a bonus you might provide
> char method variants as well.
>

OK, sounds good. Like this then:
[https://pastebin.com/mJw2tDHj]

import java.util.Locale;

/**
 * Enumerates letter cases and converts strings.
 *
 * @author <a href="mailto:[hidden email]">Gary Gregory</a>
 */
public enum LetterCase {

    LOWER {
        @Override
        public char[] toCaseString(final char[] source, final Locale
locale) {
            return String.valueOf(source).toLowerCase(locale).toCharArray();
        }

        @Override
        public String toCaseString(final String source, final Locale
locale) {
            return source.toLowerCase(locale);
        }

    },
    UPPER {
        @Override
        public char[] toCaseString(final char[] source, final Locale
locale) {
            return String.valueOf(source).toUpperCase(locale).toCharArray();
        }

        @Override
        public String toCaseString(final String source, final Locale
locale) {
            return source.toUpperCase(locale);
        }
    };

    /**
     * Converts from the given {@code source} string to the case specified
by this enum using the default {@code locale}.
     *
     * @param source
     *            the string to convert
     * @param locale
     *            the locale to use for conversion.
     * @return a converted string.
     */
    public char[] toCaseString(final char[] source) {
        return toCaseString(source, Locale.getDefault());
    }

    /**
     * Converts from the given {@code source} char[] to the case specified
by this enum using the given {@code locale}.
     *
     * @param source
     *            the char[] to convert
     * @param locale
     *            the locale to use for conversion.
     * @return a converted char[].
     */
    public abstract char[] toCaseString(char[] source, Locale locale);

    /**
     * Converts from the given {@code source} string to the case specified
by this enum using the default {@code locale}.
     *
     * @param source
     *            the string to convert
     * @param locale
     *            the locale to use for conversion.
     * @return a converted string.
     */
    public String toCaseString(final String source) {
        return toCaseString(source, Locale.getDefault());
    }

    /**
     * Converts from the given {@code source} string to the case specified
by this enum using the given {@code locale}.
     *
     * @param source
     *            the string to convert
     * @param locale
     *            the locale to use for conversion.
     * @return a converted string.
     */
    public abstract String toCaseString(String source, Locale locale);

}

Gary


> Matt
>
>
> > This is much clearer than using a boolean for the letter case.
>
> YMMV
>
> > Gary
> >
> >
> >>
> >> >>
> >> >
> >> > I posted the code at the start of this thread...
> >> >
> >> > Gary
> >> >
> >> >
> >> >>
> >> >> How does it compare with code that does not have the enum?
> >> >>
> >> >> > I use it in the following contexts:
> >> >> > - To pass to a hex dump method to configure where the alpha chars
> >> should
> >> >> be
> >> >> > in lower case or upper case.
> >> >> > - To configure a parameterized JUnit test class to configure the
> case
> >> of
> >> >> > HTTP headers and values.
> >> >> > - To normalize input
> >> >> >
> >> >> > Gary
> >> >> >
> >> >> >
> >> >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
> [hidden email]
> >> >
> >> >> > wrote:
> >> >> >
> >> >> >> What problem does it solve?
> >> >> >>
> >> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
> >> [hidden email])
> >> >> >> wrote:
> >> >> >>
> >> >> >> Does anyone think this is useful and general enough to add to
> Commons
> >> >> >> Text:
> >> >> >>
> >> >> >> /**
> >> >> >> * Enumerates letter cases and converts strings.
> >> >> >> *
> >> >> >> * @author <a href="mailto:[hidden email]">Gary
> >> Gregory</a>
> >> >> >> */
> >> >> >> public enum LetterCase {
> >> >> >> LOWER {
> >> >> >> @Override
> >> >> >> public String toCaseString(final String source, final Locale
> >> >> >> locale) {
> >> >> >> return source.toLowerCase(locale);
> >> >> >> }
> >> >> >>
> >> >> >> },
> >> >> >> UPPER {
> >> >> >> @Override
> >> >> >> public String toCaseString(final String source, final Locale
> >> >> >> locale) {
> >> >> >> return source.toUpperCase(locale);
> >> >> >> }
> >> >> >> };
> >> >> >>
> >> >> >> /**
> >> >> >> * Converts from the given {@code source} string to the case
> specified
> >> >> >> by this enum using the given {@code locale}.
> >> >> >> *
> >> >> >> * @param source
> >> >> >> * the string to convert
> >> >> >> * @param locale
> >> >> >> * the locale to use for conversion.
> >> >> >> * @return a converted string.
> >> >> >> */
> >> >> >> public abstract String toCaseString(String source, Locale locale);
> >> >> >> }
> >> >> >>
> >> >> >> ?
> >> >> >>
> >> >> >> Thank you,
> >> >> >> Gary
> >> >> >>
> >> >> >>
> >> >>
> >> >> ------------------------------------------------------------
> ---------
> >> >> To unsubscribe, e-mail: [hidden email]
> >> >> For additional commands, e-mail: [hidden email]
> >> >>
> >> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
On Fri, Feb 23, 2018 at 10:04 AM, Gary Gregory <[hidden email]>
wrote:

>
>
> On Fri, Feb 23, 2018 at 7:03 AM, Matt Benson <[hidden email]> wrote:
>
>> On Feb 23, 2018 4:26 AM, "sebb" <[hidden email]> wrote:
>>
>> On 23 February 2018 at 00:41, Gary Gregory <[hidden email]>
>> wrote:
>> > On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
>> >
>> >> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
>> wrote:
>> >> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
>> >> >
>> >> >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]>
>> >> wrote:
>> >> >> > Use your imagination ;-)
>> >> >>
>> >> >> What would the new code look like?
>> >>
>> >> I mean the user code before and after the enum is introduced.
>> >>
>> >
>> > I don't have code for the _before_ since I wrote the enum to avoid it.
>> >
>> > I have a different util class that gets called like this:
>> >
>> > HexDump(byte[] data, LetterCase letterCase, more details...)
>>
>> The above is only using the enum as a way to provide the requested
>> type of transform.
>>
>> The important bit is the code that uses the enum to do the transform.
>>
>>
>> Obviously the method body would call #toCaseString() passing a String and
>> Locale as arguments. This question feels like trolling, as does the
>> insinuation that true/false *might not* be less clear in intent than
>> UPPER/LOWER, or vice versa as, again, we don't know what the hypothetical
>> API author was thinking in the case of boolean. WTH
>>
>
> Thanks Matt for pointing that out. The comment did feel trollish to me as
> well but I choose not to engage. Aside from that I really like Sebb's
> contributions to our community, his diligence and attention to detail.
> I did not think I needed to make some pedantic point about an enum being
> much better than a boolean to express letter case or toggles in general.
>
>
>> The comment about Java 8 is fine as far as it goes, but doesn't really
>> invalidate this as the method supplied can easily be used as a BiFunction.
>> Gary, I'm thinking you might as well "underload" the method to pass the
>> default Locale to the two-argument variant so then you also implement
>> Function in the simple case. Then I'm sold. For a bonus you might provide
>> char method variants as well.
>>
>
> OK, sounds good. Like this then:
> [https://pastebin.com/mJw2tDHj]
>

Without the @author tag of course.

G

>
> import java.util.Locale;
>
> /**
>  * Enumerates letter cases and converts strings.
>  *
>  * @author <a href="mailto:[hidden email]">Gary Gregory</a>
>  */
> public enum LetterCase {
>
>     LOWER {
>         @Override
>         public char[] toCaseString(final char[] source, final Locale
> locale) {
>             return String.valueOf(source).toLowerCase(locale).
> toCharArray();
>         }
>
>         @Override
>         public String toCaseString(final String source, final Locale
> locale) {
>             return source.toLowerCase(locale);
>         }
>
>     },
>     UPPER {
>         @Override
>         public char[] toCaseString(final char[] source, final Locale
> locale) {
>             return String.valueOf(source).toUpperCase(locale).
> toCharArray();
>         }
>
>         @Override
>         public String toCaseString(final String source, final Locale
> locale) {
>             return source.toUpperCase(locale);
>         }
>     };
>
>     /**
>      * Converts from the given {@code source} string to the case specified
> by this enum using the default {@code locale}.
>      *
>      * @param source
>      *            the string to convert
>      * @param locale
>      *            the locale to use for conversion.
>      * @return a converted string.
>      */
>     public char[] toCaseString(final char[] source) {
>         return toCaseString(source, Locale.getDefault());
>     }
>
>     /**
>      * Converts from the given {@code source} char[] to the case specified
> by this enum using the given {@code locale}.
>      *
>      * @param source
>      *            the char[] to convert
>      * @param locale
>      *            the locale to use for conversion.
>      * @return a converted char[].
>      */
>     public abstract char[] toCaseString(char[] source, Locale locale);
>
>     /**
>      * Converts from the given {@code source} string to the case specified
> by this enum using the default {@code locale}.
>      *
>      * @param source
>      *            the string to convert
>      * @param locale
>      *            the locale to use for conversion.
>      * @return a converted string.
>      */
>     public String toCaseString(final String source) {
>         return toCaseString(source, Locale.getDefault());
>     }
>
>     /**
>      * Converts from the given {@code source} string to the case specified
> by this enum using the given {@code locale}.
>      *
>      * @param source
>      *            the string to convert
>      * @param locale
>      *            the locale to use for conversion.
>      * @return a converted string.
>      */
>     public abstract String toCaseString(String source, Locale locale);
>
> }
>
> Gary
>
>
>> Matt
>>
>>
>> > This is much clearer than using a boolean for the letter case.
>>
>> YMMV
>>
>> > Gary
>> >
>> >
>> >>
>> >> >>
>> >> >
>> >> > I posted the code at the start of this thread...
>> >> >
>> >> > Gary
>> >> >
>> >> >
>> >> >>
>> >> >> How does it compare with code that does not have the enum?
>> >> >>
>> >> >> > I use it in the following contexts:
>> >> >> > - To pass to a hex dump method to configure where the alpha chars
>> >> should
>> >> >> be
>> >> >> > in lower case or upper case.
>> >> >> > - To configure a parameterized JUnit test class to configure the
>> case
>> >> of
>> >> >> > HTTP headers and values.
>> >> >> > - To normalize input
>> >> >> >
>> >> >> > Gary
>> >> >> >
>> >> >> >
>> >> >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
>> [hidden email]
>> >> >
>> >> >> > wrote:
>> >> >> >
>> >> >> >> What problem does it solve?
>> >> >> >>
>> >> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
>> >> [hidden email])
>> >> >> >> wrote:
>> >> >> >>
>> >> >> >> Does anyone think this is useful and general enough to add to
>> Commons
>> >> >> >> Text:
>> >> >> >>
>> >> >> >> /**
>> >> >> >> * Enumerates letter cases and converts strings.
>> >> >> >> *
>> >> >> >> * @author <a href="mailto:[hidden email]">Gary
>> >> Gregory</a>
>> >> >> >> */
>> >> >> >> public enum LetterCase {
>> >> >> >> LOWER {
>> >> >> >> @Override
>> >> >> >> public String toCaseString(final String source, final Locale
>> >> >> >> locale) {
>> >> >> >> return source.toLowerCase(locale);
>> >> >> >> }
>> >> >> >>
>> >> >> >> },
>> >> >> >> UPPER {
>> >> >> >> @Override
>> >> >> >> public String toCaseString(final String source, final Locale
>> >> >> >> locale) {
>> >> >> >> return source.toUpperCase(locale);
>> >> >> >> }
>> >> >> >> };
>> >> >> >>
>> >> >> >> /**
>> >> >> >> * Converts from the given {@code source} string to the case
>> specified
>> >> >> >> by this enum using the given {@code locale}.
>> >> >> >> *
>> >> >> >> * @param source
>> >> >> >> * the string to convert
>> >> >> >> * @param locale
>> >> >> >> * the locale to use for conversion.
>> >> >> >> * @return a converted string.
>> >> >> >> */
>> >> >> >> public abstract String toCaseString(String source, Locale
>> locale);
>> >> >> >> }
>> >> >> >>
>> >> >> >> ?
>> >> >> >>
>> >> >> >> Thank you,
>> >> >> >> Gary
>> >> >> >>
>> >> >> >>
>> >> >>
>> >> >> ------------------------------------------------------------
>> ---------
>> >> >> To unsubscribe, e-mail: [hidden email]
>> >> >> For additional commands, e-mail: [hidden email]
>> >> >>
>> >> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: [hidden email]
>> >> For additional commands, e-mail: [hidden email]
>> >>
>> >>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
On Fri, Feb 23, 2018 at 10:05 AM, Gary Gregory <[hidden email]>
wrote:

>
>
> On Fri, Feb 23, 2018 at 10:04 AM, Gary Gregory <[hidden email]>
> wrote:
>
>>
>>
>> On Fri, Feb 23, 2018 at 7:03 AM, Matt Benson <[hidden email]> wrote:
>>
>>> On Feb 23, 2018 4:26 AM, "sebb" <[hidden email]> wrote:
>>>
>>> On 23 February 2018 at 00:41, Gary Gregory <[hidden email]>
>>> wrote:
>>> > On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
>>> >
>>> >> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
>>> wrote:
>>> >> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
>>> >> >
>>> >> >> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]
>>> >
>>> >> wrote:
>>> >> >> > Use your imagination ;-)
>>> >> >>
>>> >> >> What would the new code look like?
>>> >>
>>> >> I mean the user code before and after the enum is introduced.
>>> >>
>>> >
>>> > I don't have code for the _before_ since I wrote the enum to avoid it.
>>> >
>>> > I have a different util class that gets called like this:
>>> >
>>> > HexDump(byte[] data, LetterCase letterCase, more details...)
>>>
>>> The above is only using the enum as a way to provide the requested
>>> type of transform.
>>>
>>> The important bit is the code that uses the enum to do the transform.
>>>
>>>
>>> Obviously the method body would call #toCaseString() passing a String and
>>> Locale as arguments. This question feels like trolling, as does the
>>> insinuation that true/false *might not* be less clear in intent than
>>> UPPER/LOWER, or vice versa as, again, we don't know what the hypothetical
>>> API author was thinking in the case of boolean. WTH
>>>
>>
>> Thanks Matt for pointing that out. The comment did feel trollish to me as
>> well but I choose not to engage. Aside from that I really like Sebb's
>> contributions to our community, his diligence and attention to detail.
>> I did not think I needed to make some pedantic point about an enum being
>> much better than a boolean to express letter case or toggles in general.
>>
>>
>>> The comment about Java 8 is fine as far as it goes, but doesn't really
>>> invalidate this as the method supplied can easily be used as a
>>> BiFunction.
>>> Gary, I'm thinking you might as well "underload" the method to pass the
>>> default Locale to the two-argument variant so then you also implement
>>> Function in the simple case. Then I'm sold. For a bonus you might provide
>>> char method variants as well.
>>>
>>
>> OK, sounds good. Like this then:
>> [https://pastebin.com/mJw2tDHj]
>>
>
> Without the @author tag of course.
>

There is also CharBuffer... I only have use for String.

Gary


>
> G
>
>>
>> import java.util.Locale;
>>
>> /**
>>  * Enumerates letter cases and converts strings.
>>  *
>>  * @author <a href="mailto:[hidden email]">Gary Gregory</a>
>>  */
>> public enum LetterCase {
>>
>>     LOWER {
>>         @Override
>>         public char[] toCaseString(final char[] source, final Locale
>> locale) {
>>             return String.valueOf(source).toLower
>> Case(locale).toCharArray();
>>         }
>>
>>         @Override
>>         public String toCaseString(final String source, final Locale
>> locale) {
>>             return source.toLowerCase(locale);
>>         }
>>
>>     },
>>     UPPER {
>>         @Override
>>         public char[] toCaseString(final char[] source, final Locale
>> locale) {
>>             return String.valueOf(source).toUpper
>> Case(locale).toCharArray();
>>         }
>>
>>         @Override
>>         public String toCaseString(final String source, final Locale
>> locale) {
>>             return source.toUpperCase(locale);
>>         }
>>     };
>>
>>     /**
>>      * Converts from the given {@code source} string to the case
>> specified by this enum using the default {@code locale}.
>>      *
>>      * @param source
>>      *            the string to convert
>>      * @param locale
>>      *            the locale to use for conversion.
>>      * @return a converted string.
>>      */
>>     public char[] toCaseString(final char[] source) {
>>         return toCaseString(source, Locale.getDefault());
>>     }
>>
>>     /**
>>      * Converts from the given {@code source} char[] to the case
>> specified by this enum using the given {@code locale}.
>>      *
>>      * @param source
>>      *            the char[] to convert
>>      * @param locale
>>      *            the locale to use for conversion.
>>      * @return a converted char[].
>>      */
>>     public abstract char[] toCaseString(char[] source, Locale locale);
>>
>>     /**
>>      * Converts from the given {@code source} string to the case
>> specified by this enum using the default {@code locale}.
>>      *
>>      * @param source
>>      *            the string to convert
>>      * @param locale
>>      *            the locale to use for conversion.
>>      * @return a converted string.
>>      */
>>     public String toCaseString(final String source) {
>>         return toCaseString(source, Locale.getDefault());
>>     }
>>
>>     /**
>>      * Converts from the given {@code source} string to the case
>> specified by this enum using the given {@code locale}.
>>      *
>>      * @param source
>>      *            the string to convert
>>      * @param locale
>>      *            the locale to use for conversion.
>>      * @return a converted string.
>>      */
>>     public abstract String toCaseString(String source, Locale locale);
>>
>> }
>>
>> Gary
>>
>>
>>> Matt
>>>
>>>
>>> > This is much clearer than using a boolean for the letter case.
>>>
>>> YMMV
>>>
>>> > Gary
>>> >
>>> >
>>> >>
>>> >> >>
>>> >> >
>>> >> > I posted the code at the start of this thread...
>>> >> >
>>> >> > Gary
>>> >> >
>>> >> >
>>> >> >>
>>> >> >> How does it compare with code that does not have the enum?
>>> >> >>
>>> >> >> > I use it in the following contexts:
>>> >> >> > - To pass to a hex dump method to configure where the alpha chars
>>> >> should
>>> >> >> be
>>> >> >> > in lower case or upper case.
>>> >> >> > - To configure a parameterized JUnit test class to configure the
>>> case
>>> >> of
>>> >> >> > HTTP headers and values.
>>> >> >> > - To normalize input
>>> >> >> >
>>> >> >> > Gary
>>> >> >> >
>>> >> >> >
>>> >> >> > On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
>>> [hidden email]
>>> >> >
>>> >> >> > wrote:
>>> >> >> >
>>> >> >> >> What problem does it solve?
>>> >> >> >>
>>> >> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
>>> >> [hidden email])
>>> >> >> >> wrote:
>>> >> >> >>
>>> >> >> >> Does anyone think this is useful and general enough to add to
>>> Commons
>>> >> >> >> Text:
>>> >> >> >>
>>> >> >> >> /**
>>> >> >> >> * Enumerates letter cases and converts strings.
>>> >> >> >> *
>>> >> >> >> * @author <a href="mailto:[hidden email]">Gary
>>> >> Gregory</a>
>>> >> >> >> */
>>> >> >> >> public enum LetterCase {
>>> >> >> >> LOWER {
>>> >> >> >> @Override
>>> >> >> >> public String toCaseString(final String source, final Locale
>>> >> >> >> locale) {
>>> >> >> >> return source.toLowerCase(locale);
>>> >> >> >> }
>>> >> >> >>
>>> >> >> >> },
>>> >> >> >> UPPER {
>>> >> >> >> @Override
>>> >> >> >> public String toCaseString(final String source, final Locale
>>> >> >> >> locale) {
>>> >> >> >> return source.toUpperCase(locale);
>>> >> >> >> }
>>> >> >> >> };
>>> >> >> >>
>>> >> >> >> /**
>>> >> >> >> * Converts from the given {@code source} string to the case
>>> specified
>>> >> >> >> by this enum using the given {@code locale}.
>>> >> >> >> *
>>> >> >> >> * @param source
>>> >> >> >> * the string to convert
>>> >> >> >> * @param locale
>>> >> >> >> * the locale to use for conversion.
>>> >> >> >> * @return a converted string.
>>> >> >> >> */
>>> >> >> >> public abstract String toCaseString(String source, Locale
>>> locale);
>>> >> >> >> }
>>> >> >> >>
>>> >> >> >> ?
>>> >> >> >>
>>> >> >> >> Thank you,
>>> >> >> >> Gary
>>> >> >> >>
>>> >> >> >>
>>> >> >>
>>> >> >> ------------------------------------------------------------
>>> ---------
>>> >> >> To unsubscribe, e-mail: [hidden email]
>>> >> >> For additional commands, e-mail: [hidden email]
>>> >> >>
>>> >> >>
>>> >>
>>> >> ---------------------------------------------------------------------
>>> >> To unsubscribe, e-mail: [hidden email]
>>> >> For additional commands, e-mail: [hidden email]
>>> >>
>>> >>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

Adam Soroka
Perhaps <T extends CharSequence>?

ajs6f

> On Feb 23, 2018, at 12:12 PM, Gary Gregory <[hidden email]> wrote:
>
> On Fri, Feb 23, 2018 at 10:05 AM, Gary Gregory <[hidden email]>
> wrote:
>
>>
>>
>> On Fri, Feb 23, 2018 at 10:04 AM, Gary Gregory <[hidden email]>
>> wrote:
>>
>>>
>>>
>>> On Fri, Feb 23, 2018 at 7:03 AM, Matt Benson <[hidden email]> wrote:
>>>
>>>> On Feb 23, 2018 4:26 AM, "sebb" <[hidden email]> wrote:
>>>>
>>>> On 23 February 2018 at 00:41, Gary Gregory <[hidden email]>
>>>> wrote:
>>>>> On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
>>>>>
>>>>>> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
>>>> wrote:
>>>>>>> On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
>>>>>>>
>>>>>>>> On 22 February 2018 at 22:27, Gary Gregory <[hidden email]
>>>>>
>>>>>> wrote:
>>>>>>>>> Use your imagination ;-)
>>>>>>>>
>>>>>>>> What would the new code look like?
>>>>>>
>>>>>> I mean the user code before and after the enum is introduced.
>>>>>>
>>>>>
>>>>> I don't have code for the _before_ since I wrote the enum to avoid it.
>>>>>
>>>>> I have a different util class that gets called like this:
>>>>>
>>>>> HexDump(byte[] data, LetterCase letterCase, more details...)
>>>>
>>>> The above is only using the enum as a way to provide the requested
>>>> type of transform.
>>>>
>>>> The important bit is the code that uses the enum to do the transform.
>>>>
>>>>
>>>> Obviously the method body would call #toCaseString() passing a String and
>>>> Locale as arguments. This question feels like trolling, as does the
>>>> insinuation that true/false *might not* be less clear in intent than
>>>> UPPER/LOWER, or vice versa as, again, we don't know what the hypothetical
>>>> API author was thinking in the case of boolean. WTH
>>>>
>>>
>>> Thanks Matt for pointing that out. The comment did feel trollish to me as
>>> well but I choose not to engage. Aside from that I really like Sebb's
>>> contributions to our community, his diligence and attention to detail.
>>> I did not think I needed to make some pedantic point about an enum being
>>> much better than a boolean to express letter case or toggles in general.
>>>
>>>
>>>> The comment about Java 8 is fine as far as it goes, but doesn't really
>>>> invalidate this as the method supplied can easily be used as a
>>>> BiFunction.
>>>> Gary, I'm thinking you might as well "underload" the method to pass the
>>>> default Locale to the two-argument variant so then you also implement
>>>> Function in the simple case. Then I'm sold. For a bonus you might provide
>>>> char method variants as well.
>>>>
>>>
>>> OK, sounds good. Like this then:
>>> [https://pastebin.com/mJw2tDHj]
>>>
>>
>> Without the @author tag of course.
>>
>
> There is also CharBuffer... I only have use for String.
>
> Gary
>
>
>>
>> G
>>
>>>
>>> import java.util.Locale;
>>>
>>> /**
>>> * Enumerates letter cases and converts strings.
>>> *
>>> * @author <a href="mailto:[hidden email]">Gary Gregory</a>
>>> */
>>> public enum LetterCase {
>>>
>>>    LOWER {
>>>        @Override
>>>        public char[] toCaseString(final char[] source, final Locale
>>> locale) {
>>>            return String.valueOf(source).toLower
>>> Case(locale).toCharArray();
>>>        }
>>>
>>>        @Override
>>>        public String toCaseString(final String source, final Locale
>>> locale) {
>>>            return source.toLowerCase(locale);
>>>        }
>>>
>>>    },
>>>    UPPER {
>>>        @Override
>>>        public char[] toCaseString(final char[] source, final Locale
>>> locale) {
>>>            return String.valueOf(source).toUpper
>>> Case(locale).toCharArray();
>>>        }
>>>
>>>        @Override
>>>        public String toCaseString(final String source, final Locale
>>> locale) {
>>>            return source.toUpperCase(locale);
>>>        }
>>>    };
>>>
>>>    /**
>>>     * Converts from the given {@code source} string to the case
>>> specified by this enum using the default {@code locale}.
>>>     *
>>>     * @param source
>>>     *            the string to convert
>>>     * @param locale
>>>     *            the locale to use for conversion.
>>>     * @return a converted string.
>>>     */
>>>    public char[] toCaseString(final char[] source) {
>>>        return toCaseString(source, Locale.getDefault());
>>>    }
>>>
>>>    /**
>>>     * Converts from the given {@code source} char[] to the case
>>> specified by this enum using the given {@code locale}.
>>>     *
>>>     * @param source
>>>     *            the char[] to convert
>>>     * @param locale
>>>     *            the locale to use for conversion.
>>>     * @return a converted char[].
>>>     */
>>>    public abstract char[] toCaseString(char[] source, Locale locale);
>>>
>>>    /**
>>>     * Converts from the given {@code source} string to the case
>>> specified by this enum using the default {@code locale}.
>>>     *
>>>     * @param source
>>>     *            the string to convert
>>>     * @param locale
>>>     *            the locale to use for conversion.
>>>     * @return a converted string.
>>>     */
>>>    public String toCaseString(final String source) {
>>>        return toCaseString(source, Locale.getDefault());
>>>    }
>>>
>>>    /**
>>>     * Converts from the given {@code source} string to the case
>>> specified by this enum using the given {@code locale}.
>>>     *
>>>     * @param source
>>>     *            the string to convert
>>>     * @param locale
>>>     *            the locale to use for conversion.
>>>     * @return a converted string.
>>>     */
>>>    public abstract String toCaseString(String source, Locale locale);
>>>
>>> }
>>>
>>> Gary
>>>
>>>
>>>> Matt
>>>>
>>>>
>>>>> This is much clearer than using a boolean for the letter case.
>>>>
>>>> YMMV
>>>>
>>>>> Gary
>>>>>
>>>>>
>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> I posted the code at the start of this thread...
>>>>>>>
>>>>>>> Gary
>>>>>>>
>>>>>>>
>>>>>>>>
>>>>>>>> How does it compare with code that does not have the enum?
>>>>>>>>
>>>>>>>>> I use it in the following contexts:
>>>>>>>>> - To pass to a hex dump method to configure where the alpha chars
>>>>>> should
>>>>>>>> be
>>>>>>>>> in lower case or upper case.
>>>>>>>>> - To configure a parameterized JUnit test class to configure the
>>>> case
>>>>>> of
>>>>>>>>> HTTP headers and values.
>>>>>>>>> - To normalize input
>>>>>>>>>
>>>>>>>>> Gary
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
>>>> [hidden email]
>>>>>>>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> What problem does it solve?
>>>>>>>>>>
>>>>>>>>>> On February 22, 2018 at 17:02:34, Gary Gregory (
>>>>>> [hidden email])
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>> Does anyone think this is useful and general enough to add to
>>>> Commons
>>>>>>>>>> Text:
>>>>>>>>>>
>>>>>>>>>> /**
>>>>>>>>>> * Enumerates letter cases and converts strings.
>>>>>>>>>> *
>>>>>>>>>> * @author <a href="mailto:[hidden email]">Gary
>>>>>> Gregory</a>
>>>>>>>>>> */
>>>>>>>>>> public enum LetterCase {
>>>>>>>>>> LOWER {
>>>>>>>>>> @Override
>>>>>>>>>> public String toCaseString(final String source, final Locale
>>>>>>>>>> locale) {
>>>>>>>>>> return source.toLowerCase(locale);
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> },
>>>>>>>>>> UPPER {
>>>>>>>>>> @Override
>>>>>>>>>> public String toCaseString(final String source, final Locale
>>>>>>>>>> locale) {
>>>>>>>>>> return source.toUpperCase(locale);
>>>>>>>>>> }
>>>>>>>>>> };
>>>>>>>>>>
>>>>>>>>>> /**
>>>>>>>>>> * Converts from the given {@code source} string to the case
>>>> specified
>>>>>>>>>> by this enum using the given {@code locale}.
>>>>>>>>>> *
>>>>>>>>>> * @param source
>>>>>>>>>> * the string to convert
>>>>>>>>>> * @param locale
>>>>>>>>>> * the locale to use for conversion.
>>>>>>>>>> * @return a converted string.
>>>>>>>>>> */
>>>>>>>>>> public abstract String toCaseString(String source, Locale
>>>> locale);
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> ?
>>>>>>>>>>
>>>>>>>>>> Thank you,
>>>>>>>>>> Gary
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>
>>>>>>>> ------------------------------------------------------------
>>>> ---------
>>>>>>>> To unsubscribe, e-mail: [hidden email]
>>>>>>>> For additional commands, e-mail: [hidden email]
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: [hidden email]
>>>>>> For additional commands, e-mail: [hidden email]
>>>>>>
>>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>
>>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [text] Upper/Lower case enum

garydgregory
On Fri, Feb 23, 2018 at 12:53 PM, ajs6f <[hidden email]> wrote:

> Perhaps <T extends CharSequence>?
>

Try it, I do not think it is so nice, mostly because just like with char[]
that means converting everything to a String to get to
String#toUpper/LowerCase. Unless we reimplement the algorithm ourselves.

Gary


> ajs6f
>
> > On Feb 23, 2018, at 12:12 PM, Gary Gregory <[hidden email]>
> wrote:
> >
> > On Fri, Feb 23, 2018 at 10:05 AM, Gary Gregory <[hidden email]>
> > wrote:
> >
> >>
> >>
> >> On Fri, Feb 23, 2018 at 10:04 AM, Gary Gregory <[hidden email]>
> >> wrote:
> >>
> >>>
> >>>
> >>> On Fri, Feb 23, 2018 at 7:03 AM, Matt Benson <[hidden email]>
> wrote:
> >>>
> >>>> On Feb 23, 2018 4:26 AM, "sebb" <[hidden email]> wrote:
> >>>>
> >>>> On 23 February 2018 at 00:41, Gary Gregory <[hidden email]>
> >>>> wrote:
> >>>>> On Thu, Feb 22, 2018 at 4:27 PM, sebb <[hidden email]> wrote:
> >>>>>
> >>>>>> On 22 February 2018 at 23:15, Gary Gregory <[hidden email]>
> >>>> wrote:
> >>>>>>> On Thu, Feb 22, 2018 at 4:11 PM, sebb <[hidden email]> wrote:
> >>>>>>>
> >>>>>>>> On 22 February 2018 at 22:27, Gary Gregory <
> [hidden email]
> >>>>>
> >>>>>> wrote:
> >>>>>>>>> Use your imagination ;-)
> >>>>>>>>
> >>>>>>>> What would the new code look like?
> >>>>>>
> >>>>>> I mean the user code before and after the enum is introduced.
> >>>>>>
> >>>>>
> >>>>> I don't have code for the _before_ since I wrote the enum to avoid
> it.
> >>>>>
> >>>>> I have a different util class that gets called like this:
> >>>>>
> >>>>> HexDump(byte[] data, LetterCase letterCase, more details...)
> >>>>
> >>>> The above is only using the enum as a way to provide the requested
> >>>> type of transform.
> >>>>
> >>>> The important bit is the code that uses the enum to do the transform.
> >>>>
> >>>>
> >>>> Obviously the method body would call #toCaseString() passing a String
> and
> >>>> Locale as arguments. This question feels like trolling, as does the
> >>>> insinuation that true/false *might not* be less clear in intent than
> >>>> UPPER/LOWER, or vice versa as, again, we don't know what the
> hypothetical
> >>>> API author was thinking in the case of boolean. WTH
> >>>>
> >>>
> >>> Thanks Matt for pointing that out. The comment did feel trollish to me
> as
> >>> well but I choose not to engage. Aside from that I really like Sebb's
> >>> contributions to our community, his diligence and attention to detail.
> >>> I did not think I needed to make some pedantic point about an enum
> being
> >>> much better than a boolean to express letter case or toggles in
> general.
> >>>
> >>>
> >>>> The comment about Java 8 is fine as far as it goes, but doesn't really
> >>>> invalidate this as the method supplied can easily be used as a
> >>>> BiFunction.
> >>>> Gary, I'm thinking you might as well "underload" the method to pass
> the
> >>>> default Locale to the two-argument variant so then you also implement
> >>>> Function in the simple case. Then I'm sold. For a bonus you might
> provide
> >>>> char method variants as well.
> >>>>
> >>>
> >>> OK, sounds good. Like this then:
> >>> [https://pastebin.com/mJw2tDHj]
> >>>
> >>
> >> Without the @author tag of course.
> >>
> >
> > There is also CharBuffer... I only have use for String.
> >
> > Gary
> >
> >
> >>
> >> G
> >>
> >>>
> >>> import java.util.Locale;
> >>>
> >>> /**
> >>> * Enumerates letter cases and converts strings.
> >>> *
> >>> * @author <a href="mailto:[hidden email]">Gary
> Gregory</a>
> >>> */
> >>> public enum LetterCase {
> >>>
> >>>    LOWER {
> >>>        @Override
> >>>        public char[] toCaseString(final char[] source, final Locale
> >>> locale) {
> >>>            return String.valueOf(source).toLower
> >>> Case(locale).toCharArray();
> >>>        }
> >>>
> >>>        @Override
> >>>        public String toCaseString(final String source, final Locale
> >>> locale) {
> >>>            return source.toLowerCase(locale);
> >>>        }
> >>>
> >>>    },
> >>>    UPPER {
> >>>        @Override
> >>>        public char[] toCaseString(final char[] source, final Locale
> >>> locale) {
> >>>            return String.valueOf(source).toUpper
> >>> Case(locale).toCharArray();
> >>>        }
> >>>
> >>>        @Override
> >>>        public String toCaseString(final String source, final Locale
> >>> locale) {
> >>>            return source.toUpperCase(locale);
> >>>        }
> >>>    };
> >>>
> >>>    /**
> >>>     * Converts from the given {@code source} string to the case
> >>> specified by this enum using the default {@code locale}.
> >>>     *
> >>>     * @param source
> >>>     *            the string to convert
> >>>     * @param locale
> >>>     *            the locale to use for conversion.
> >>>     * @return a converted string.
> >>>     */
> >>>    public char[] toCaseString(final char[] source) {
> >>>        return toCaseString(source, Locale.getDefault());
> >>>    }
> >>>
> >>>    /**
> >>>     * Converts from the given {@code source} char[] to the case
> >>> specified by this enum using the given {@code locale}.
> >>>     *
> >>>     * @param source
> >>>     *            the char[] to convert
> >>>     * @param locale
> >>>     *            the locale to use for conversion.
> >>>     * @return a converted char[].
> >>>     */
> >>>    public abstract char[] toCaseString(char[] source, Locale locale);
> >>>
> >>>    /**
> >>>     * Converts from the given {@code source} string to the case
> >>> specified by this enum using the default {@code locale}.
> >>>     *
> >>>     * @param source
> >>>     *            the string to convert
> >>>     * @param locale
> >>>     *            the locale to use for conversion.
> >>>     * @return a converted string.
> >>>     */
> >>>    public String toCaseString(final String source) {
> >>>        return toCaseString(source, Locale.getDefault());
> >>>    }
> >>>
> >>>    /**
> >>>     * Converts from the given {@code source} string to the case
> >>> specified by this enum using the given {@code locale}.
> >>>     *
> >>>     * @param source
> >>>     *            the string to convert
> >>>     * @param locale
> >>>     *            the locale to use for conversion.
> >>>     * @return a converted string.
> >>>     */
> >>>    public abstract String toCaseString(String source, Locale locale);
> >>>
> >>> }
> >>>
> >>> Gary
> >>>
> >>>
> >>>> Matt
> >>>>
> >>>>
> >>>>> This is much clearer than using a boolean for the letter case.
> >>>>
> >>>> YMMV
> >>>>
> >>>>> Gary
> >>>>>
> >>>>>
> >>>>>>
> >>>>>>>>
> >>>>>>>
> >>>>>>> I posted the code at the start of this thread...
> >>>>>>>
> >>>>>>> Gary
> >>>>>>>
> >>>>>>>
> >>>>>>>>
> >>>>>>>> How does it compare with code that does not have the enum?
> >>>>>>>>
> >>>>>>>>> I use it in the following contexts:
> >>>>>>>>> - To pass to a hex dump method to configure where the alpha chars
> >>>>>> should
> >>>>>>>> be
> >>>>>>>>> in lower case or upper case.
> >>>>>>>>> - To configure a parameterized JUnit test class to configure the
> >>>> case
> >>>>>> of
> >>>>>>>>> HTTP headers and values.
> >>>>>>>>> - To normalize input
> >>>>>>>>>
> >>>>>>>>> Gary
> >>>>>>>>>
> >>>>>>>>>
> >>>>>>>>> On Thu, Feb 22, 2018 at 3:14 PM, Otto Fowler <
> >>>> [hidden email]
> >>>>>>>
> >>>>>>>>> wrote:
> >>>>>>>>>
> >>>>>>>>>> What problem does it solve?
> >>>>>>>>>>
> >>>>>>>>>> On February 22, 2018 at 17:02:34, Gary Gregory (
> >>>>>> [hidden email])
> >>>>>>>>>> wrote:
> >>>>>>>>>>
> >>>>>>>>>> Does anyone think this is useful and general enough to add to
> >>>> Commons
> >>>>>>>>>> Text:
> >>>>>>>>>>
> >>>>>>>>>> /**
> >>>>>>>>>> * Enumerates letter cases and converts strings.
> >>>>>>>>>> *
> >>>>>>>>>> * @author <a href="mailto:[hidden email]">Gary
> >>>>>> Gregory</a>
> >>>>>>>>>> */
> >>>>>>>>>> public enum LetterCase {
> >>>>>>>>>> LOWER {
> >>>>>>>>>> @Override
> >>>>>>>>>> public String toCaseString(final String source, final Locale
> >>>>>>>>>> locale) {
> >>>>>>>>>> return source.toLowerCase(locale);
> >>>>>>>>>> }
> >>>>>>>>>>
> >>>>>>>>>> },
> >>>>>>>>>> UPPER {
> >>>>>>>>>> @Override
> >>>>>>>>>> public String toCaseString(final String source, final Locale
> >>>>>>>>>> locale) {
> >>>>>>>>>> return source.toUpperCase(locale);
> >>>>>>>>>> }
> >>>>>>>>>> };
> >>>>>>>>>>
> >>>>>>>>>> /**
> >>>>>>>>>> * Converts from the given {@code source} string to the case
> >>>> specified
> >>>>>>>>>> by this enum using the given {@code locale}.
> >>>>>>>>>> *
> >>>>>>>>>> * @param source
> >>>>>>>>>> * the string to convert
> >>>>>>>>>> * @param locale
> >>>>>>>>>> * the locale to use for conversion.
> >>>>>>>>>> * @return a converted string.
> >>>>>>>>>> */
> >>>>>>>>>> public abstract String toCaseString(String source, Locale
> >>>> locale);
> >>>>>>>>>> }
> >>>>>>>>>>
> >>>>>>>>>> ?
> >>>>>>>>>>
> >>>>>>>>>> Thank you,
> >>>>>>>>>> Gary
> >>>>>>>>>>
> >>>>>>>>>>
> >>>>>>>>
> >>>>>>>> ------------------------------------------------------------
> >>>> ---------
> >>>>>>>> To unsubscribe, e-mail: [hidden email]
> >>>>>>>> For additional commands, e-mail: [hidden email]
> >>>>>>>>
> >>>>>>>>
> >>>>>>
> >>>>>> ------------------------------------------------------------
> ---------
> >>>>>> To unsubscribe, e-mail: [hidden email]
> >>>>>> For additional commands, e-mail: [hidden email]
> >>>>>>
> >>>>>>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: [hidden email]
> >>>> For additional commands, e-mail: [hidden email]
> >>>>
> >>>
> >>>
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>