001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.util;
019
020
021/**
022 * String utilities. Replicates Apache Commons Lang 3.
023 */
024public final class StringUtils {
025        
026        
027        /**
028         * Returns {@code true} if the specified char sequence is all blank,
029         * empty or {@code null}.
030         *
031         * @param cs The char sequence. May be {@code null}.
032         *
033         * @return {@code true} if the specified char sequence is all blank,
034         *         empty or {@code null}, else {@code false}.
035         */
036        public static boolean isBlank(final CharSequence cs) {
037                
038                int strLen;
039                if (cs != null && (strLen = cs.length()) != 0) {
040                        for(int i = 0; i < strLen; ++i) {
041                                if (!Character.isWhitespace(cs.charAt(i))) {
042                                        return false;
043                                }
044                        }
045                        
046                }
047                return true;
048        }
049        
050        
051        /**
052         * Returns {@code true} if the specified char sequence is not all
053         * blank, not empty and not {@code null}.
054         *
055         * @param cs The char sequence. May be {@code null}.
056         *
057         * @return {@code true} if the specified char sequence is not all
058         *         blank, not empty and not {@code null}, else {@code false}.
059         */
060        public static boolean isNotBlank(final CharSequence cs) {
061                
062                return !isBlank(cs);
063        }
064        
065        
066        /**
067         * Returns {@code true} if the specified char sequence is all
068         * alphabetic letters.
069         *
070         * @param cs The char sequence. May be {@code null}.
071         *
072         * @return {@code true} if the specified char sequence is all
073         *         alphabetic letters, empty or {@code null}, else
074         *         {@code false}.
075         */
076        public static boolean isAlpha(final CharSequence cs) {
077                
078                int strLen;
079                if (cs != null && (strLen = cs.length()) != 0) {
080                        for(int i = 0; i < strLen; ++i) {
081                                if (!Character.isAlphabetic(cs.charAt(i))) {
082                                        return false;
083                                }
084                        }
085                        
086                }
087                return true;
088        }
089        
090        
091        /**
092         * Returns {@code true} if the specified char sequence is all numeric
093         * letters.
094         *
095         * @param cs The char sequence. May be {@code null}.
096         *
097         * @return {@code true} if the specified char sequence is all numeric
098         *         letters, empty or {@code null}, else {@code false}.
099         */
100        public static boolean isNumeric(final CharSequence cs) {
101                
102                int strLen;
103                if (cs != null && (strLen = cs.length()) != 0) {
104                        for(int i = 0; i < strLen; ++i) {
105                                if (!Character.isDigit(cs.charAt(i))) {
106                                        return false;
107                                }
108                        }
109                        
110                }
111                return true;
112        }
113        
114        
115        /**
116         * Prevents public instantiation.
117         */
118        private StringUtils() {}
119}