001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.nullify;
017
018import org.apache.commons.lang3.StringUtils;
019
020import com.google.common.base.Optional;
021
022public class NullUtils {
023
024        public static final String NONE = "NONE";
025        public static final String NULL = "NULL";
026
027        /**
028         * Return true if object is a non null CharSequence that is pure whitespace
029         */
030        public static boolean isBlank(Object object) {
031                return (object instanceof CharSequence) && StringUtils.isBlank((CharSequence) object);
032        }
033
034        /**
035         * Return true if object is null OR a non null CharSequence that is pure whitespace
036         */
037        public static boolean isNullOrBlank(Object object) {
038                if (object == null) {
039                        return true;
040                } else {
041                        return isBlank(object);
042                }
043        }
044
045        /**
046         * Return true if:
047         * 
048         * <pre>
049         *  s == null
050         *  StringUtils.equalsIgnoreCase("null", s) == true
051         * </pre>
052         */
053        public static final boolean isNull(String s) {
054                return s == null || StringUtils.equalsIgnoreCase(NULL, s);
055        }
056
057        /**
058         * Return true if:
059         * 
060         * <pre>
061         * StringUtils.equalsIgnoreCase(&quot;none&quot;, s) == true
062         * </pre>
063         */
064        public static final boolean isNone(String s) {
065                return StringUtils.equalsIgnoreCase(NONE, s);
066        }
067
068        /**
069         * Return true iff:
070         * 
071         * <pre>
072         *  s == null
073         *  StringUtils.equalsIgnoreCase("null", s) == true
074         *  StringUtils.equalsIgnoreCase("none", s) == true
075         * </pre>
076         */
077        public static final boolean isNullOrNone(String s) {
078                return isNull(s) || isNone(s);
079        }
080
081        /**
082         * Returns <code>Optional.absent()</code> if s is null, the empty string, pure whitespace, NONE, or NULL
083         */
084        public static final Optional<String> toAbsent(String s) {
085                return Optional.fromNullable(trimToNull(s));
086        }
087
088        /**
089         * Returns <code>NONE</code> if optional is not present OR if it contains the empty string, pure whitespace, NONE, or NULL
090         */
091        public static final String toNone(Optional<String> optional) {
092                return trimToNone(optional.orNull());
093        }
094
095        /**
096         * Returns <code>null</code> if s is null, the empty string, pure whitespace, NONE, or NULL
097         */
098        public static final String trimToNull(String s) {
099                String trimmed = StringUtils.trimToNull(s);
100                return isNullOrNone(trimmed) ? null : trimmed;
101        }
102
103        /**
104         * Returns <code>NONE</code> if s is null, the empty string, pure whitespace, NONE, or NULL
105         */
106        public static final String trimToNone(String s) {
107                if (trimToNull(s) == null) {
108                        return NONE;
109                } else {
110                        return trimToNull(s);
111                }
112        }
113
114}