Advertisement
ggregory

Apache Commons Text LetterCase

Feb 23rd, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. /*
  2.  * ====================================================================
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  * ====================================================================
  20.  *
  21.  * This software consists of voluntary contributions made by many
  22.  * individuals on behalf of the Apache Software Foundation.  For more
  23.  * information on the Apache Software Foundation, please see
  24.  * <http://www.apache.org/>.
  25.  *
  26.  */
  27. package org.apache.commons.text;
  28.  
  29. import java.util.Locale;
  30.  
  31. /**
  32.  * Enumerates letter cases and converts strings.
  33.  *
  34.  * @author <a href="mailto:ggregory@rocketsoftware.com">Gary Gregory</a>
  35.  */
  36. public enum LetterCase {
  37.  
  38.     LOWER {
  39.         @Override
  40.         public char[] toCaseString(final char[] source, final Locale locale) {
  41.             return String.valueOf(source).toLowerCase(locale).toCharArray();
  42.         }
  43.  
  44.         @Override
  45.         public String toCaseString(final String source, final Locale locale) {
  46.             return source.toLowerCase(locale);
  47.         }
  48.  
  49.     },
  50.     UPPER {
  51.         @Override
  52.         public char[] toCaseString(final char[] source, final Locale locale) {
  53.             return String.valueOf(source).toUpperCase(locale).toCharArray();
  54.         }
  55.  
  56.         @Override
  57.         public String toCaseString(final String source, final Locale locale) {
  58.             return source.toUpperCase(locale);
  59.         }
  60.     };
  61.  
  62.     /**
  63.      * Converts from the given {@code source} string to the case specified by this enum using the default {@code locale}.
  64.      *
  65.      * @param source
  66.      *            the string to convert
  67.      * @param locale
  68.      *            the locale to use for conversion.
  69.      * @return a converted string.
  70.      */
  71.     public char[] toCaseString(final char[] source) {
  72.         return toCaseString(source, Locale.getDefault());
  73.     }
  74.  
  75.     /**
  76.      * Converts from the given {@code source} char[] to the case specified by this enum using the given {@code locale}.
  77.      *
  78.      * @param source
  79.      *            the char[] to convert
  80.      * @param locale
  81.      *            the locale to use for conversion.
  82.      * @return a converted char[].
  83.      */
  84.     public abstract char[] toCaseString(char[] source, Locale locale);
  85.  
  86.     /**
  87.      * Converts from the given {@code source} string to the case specified by this enum using the default {@code locale}.
  88.      *
  89.      * @param source
  90.      *            the string to convert
  91.      * @param locale
  92.      *            the locale to use for conversion.
  93.      * @return a converted string.
  94.      */
  95.     public String toCaseString(final String source) {
  96.         return toCaseString(source, Locale.getDefault());
  97.     }
  98.  
  99.     /**
  100.      * Converts from the given {@code source} string to the case specified by this enum using the given {@code locale}.
  101.      *
  102.      * @param source
  103.      *            the string to convert
  104.      * @param locale
  105.      *            the locale to use for conversion.
  106.      * @return a converted string.
  107.      */
  108.     public abstract String toCaseString(String source, Locale locale);
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement