CharacterPredicates.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text;

  18. /**
  19.  * Commonly used implementations of {@link CharacterPredicate}. Per the interface
  20.  * requirements, all implementations are thread safe.
  21.  *
  22.  * @since 1.0
  23.  */
  24. public enum CharacterPredicates implements CharacterPredicate {

  25.     /**
  26.      * Tests code points against {@link Character#isLetter(int)}.
  27.      *
  28.      * @since 1.0
  29.      */
  30.     LETTERS {
  31.         @Override
  32.         public boolean test(final int codePoint) {
  33.             return Character.isLetter(codePoint);
  34.         }
  35.     },

  36.     /**
  37.      * Tests code points against {@link Character#isDigit(int)}.
  38.      *
  39.      * @since 1.0
  40.      */
  41.     DIGITS {
  42.         @Override
  43.         public boolean test(final int codePoint) {
  44.             return Character.isDigit(codePoint);
  45.         }
  46.     },

  47.     /**
  48.      * Tests if the code points represents a number between 0 and 9.
  49.      *
  50.      * @since 1.2
  51.      */
  52.     ARABIC_NUMERALS {
  53.         @Override
  54.         public boolean test(final int codePoint) {
  55.             return codePoint >= '0' && codePoint <= '9';
  56.         }
  57.     },

  58.     /**
  59.      * Tests if the code points represents a letter between a and z.
  60.      *
  61.      * @since 1.2
  62.      */
  63.     ASCII_LOWERCASE_LETTERS {
  64.         @Override
  65.         public boolean test(final int codePoint) {
  66.             return codePoint >= 'a' && codePoint <= 'z';
  67.         }
  68.     },

  69.     /**
  70.      * Tests if the code points represents a letter between A and Z.
  71.      *
  72.      * @since 1.2
  73.      */
  74.     ASCII_UPPERCASE_LETTERS {
  75.         @Override
  76.         public boolean test(final int codePoint) {
  77.             return codePoint >= 'A' && codePoint <= 'Z';
  78.         }
  79.     },

  80.     /**
  81.      * Tests if the code points represents a letter between a and Z.
  82.      *
  83.      * @since 1.2
  84.      */
  85.     ASCII_LETTERS {
  86.         @Override
  87.         public boolean test(final int codePoint) {
  88.             return ASCII_LOWERCASE_LETTERS.test(codePoint) || ASCII_UPPERCASE_LETTERS.test(codePoint);
  89.         }
  90.     },

  91.     /**
  92.      * Tests if the code points represents a letter between a and Z or a number between 0 and 9.
  93.      *
  94.      * @since 1.2
  95.      */
  96.     ASCII_ALPHA_NUMERALS {
  97.         @Override
  98.         public boolean test(final int codePoint) {
  99.             return ASCII_LOWERCASE_LETTERS.test(codePoint) || ASCII_UPPERCASE_LETTERS.test(codePoint)
  100.                     || ARABIC_NUMERALS.test(codePoint);
  101.         }
  102.     }
  103. }