On Fri, Feb 23, 2018 at 7:03 AM, Matt Benson <mben...@apache.org> wrote:

> On Feb 23, 2018 4:26 AM, "sebb" <seb...@gmail.com> wrote:
>
> On 23 February 2018 at 00:41, Gary Gregory <garydgreg...@gmail.com> wrote:
> > On Thu, Feb 22, 2018 at 4:27 PM, sebb <seb...@gmail.com> wrote:
> >
> >> On 22 February 2018 at 23:15, Gary Gregory <garydgreg...@gmail.com>
> wrote:
> >> > On Thu, Feb 22, 2018 at 4:11 PM, sebb <seb...@gmail.com> wrote:
> >> >
> >> >> On 22 February 2018 at 22:27, Gary Gregory <garydgreg...@gmail.com>
> >> 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:ggreg...@rocketsoftware.com";>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 <
> ottobackwa...@gmail.com
> >> >
> >> >> > wrote:
> >> >> >
> >> >> >> What problem does it solve?
> >> >> >>
> >> >> >> On February 22, 2018 at 17:02:34, Gary Gregory (
> >> garydgreg...@gmail.com)
> >> >> >> 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:ggreg...@rocketsoftware.com";>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: dev-unsubscr...@commons.apache.org
> >> >> For additional commands, e-mail: dev-h...@commons.apache.org
> >> >>
> >> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> >> For additional commands, e-mail: dev-h...@commons.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org
>

Reply via email to