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