java/sql-dk/src/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Dec 2013 20:01:37 +0100
branchv_0
changeset 16 5b8fcd35d4d6
parent 1 f32dac78d13a
child 29 d66858b4b563
permissions -rw-r--r--
license: GNU GPLv3+
franta-hg@16
     1
/**
franta-hg@16
     2
 * SQL-DK
franta-hg@16
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16
     4
 *
franta-hg@16
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@16
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@1
    18
package info.globalcode.sql.dk;
franta-hg@1
    19
franta-hg@1
    20
import java.util.ArrayList;
franta-hg@1
    21
import java.util.Collection;
franta-hg@1
    22
import java.util.Map;
franta-hg@1
    23
franta-hg@1
    24
/**
franta-hg@1
    25
 *
franta-hg@1
    26
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    27
 */
franta-hg@1
    28
public class Functions {
franta-hg@1
    29
franta-hg@1
    30
	private Functions() {
franta-hg@1
    31
	}
franta-hg@1
    32
franta-hg@1
    33
	public static boolean equalz(Object a, Object b) {
franta-hg@1
    34
		return a == null ? b == null : a.equals(b);
franta-hg@1
    35
	}
franta-hg@1
    36
franta-hg@1
    37
	/**
franta-hg@1
    38
	 *
franta-hg@1
    39
	 * @param text String to be examinated
franta-hg@1
    40
	 * @param trim whether text should be trimmed before examination
franta-hg@1
    41
	 * @return whether text is not empty and one or more characters long (after prospective trim)
franta-hg@1
    42
	 */
franta-hg@1
    43
	public static boolean isEmpty(String text, boolean trim) {
franta-hg@1
    44
		if (text == null) {
franta-hg@1
    45
			return true;
franta-hg@1
    46
		} else {
franta-hg@1
    47
			if (trim) {
franta-hg@1
    48
				text = text.trim();
franta-hg@1
    49
			}
franta-hg@1
    50
			return text.isEmpty();
franta-hg@1
    51
		}
franta-hg@1
    52
	}
franta-hg@1
    53
franta-hg@1
    54
	/**
franta-hg@1
    55
	 * @see #isEmpty(java.lang.String, boolean)
franta-hg@1
    56
	 */
franta-hg@1
    57
	public static boolean isNotEmpty(String text, boolean trim) {
franta-hg@1
    58
		return !isEmpty(text, trim);
franta-hg@1
    59
	}
franta-hg@1
    60
franta-hg@1
    61
	public boolean isEmpty(Collection c) {
franta-hg@1
    62
		return c == null || c.isEmpty();
franta-hg@1
    63
	}
franta-hg@1
    64
franta-hg@1
    65
	public boolean isNotEmpty(Collection c) {
franta-hg@1
    66
		return !isEmpty(c);
franta-hg@1
    67
	}
franta-hg@1
    68
franta-hg@1
    69
	public boolean isEmpty(Map m) {
franta-hg@1
    70
		return m == null || m.isEmpty();
franta-hg@1
    71
	}
franta-hg@1
    72
franta-hg@1
    73
	public boolean isNotEmpty(Map m) {
franta-hg@1
    74
		return !isEmpty(m);
franta-hg@1
    75
	}
franta-hg@1
    76
franta-hg@1
    77
	/**
franta-hg@1
    78
	 * @return empty collection if given one is null | or the original one
franta-hg@1
    79
	 */
franta-hg@1
    80
	public static <T> Collection<T> notNull(Collection<T> c) {
franta-hg@1
    81
		if (c == null) {
franta-hg@1
    82
			return new ArrayList<>();
franta-hg@1
    83
		} else {
franta-hg@1
    84
			return c;
franta-hg@1
    85
		}
franta-hg@1
    86
	}
franta-hg@1
    87
}