java/sql-dk/src/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 23:31:55 +0100
branchv_0
changeset 34 9335cf31c0f2
parent 33 04db6ccd6c48
child 38 ff5bbc06ed29
permissions -rw-r--r--
first working version
     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.io.BufferedReader;
    22 import java.io.File;
    23 import java.io.IOException;
    24 import java.io.InputStreamReader;
    25 import java.io.PrintWriter;
    26 import java.util.ArrayList;
    27 import java.util.Collection;
    28 import java.util.Map;
    29 
    30 /**
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 public class Functions {
    35 
    36 	private Functions() {
    37 	}
    38 
    39 	public static boolean equalz(Object a, Object b) {
    40 		return a == null ? b == null : a.equals(b);
    41 	}
    42 
    43 	/**
    44 	 *
    45 	 * @param text String to be examinated
    46 	 * @param trim whether text should be trimmed before examination
    47 	 * @return whether text is not empty and one or more characters long (after prospective trim)
    48 	 */
    49 	public static boolean isEmpty(String text, boolean trim) {
    50 		if (text == null) {
    51 			return true;
    52 		} else {
    53 			if (trim) {
    54 				text = text.trim();
    55 			}
    56 			return text.isEmpty();
    57 		}
    58 	}
    59 
    60 	/**
    61 	 * @see #isEmpty(java.lang.String, boolean)
    62 	 */
    63 	public static boolean isNotEmpty(String text, boolean trim) {
    64 		return !isEmpty(text, trim);
    65 	}
    66 
    67 	public boolean isEmpty(Collection c) {
    68 		return c == null || c.isEmpty();
    69 	}
    70 
    71 	public boolean isNotEmpty(Collection c) {
    72 		return !isEmpty(c);
    73 	}
    74 
    75 	public boolean isEmpty(Map m) {
    76 		return m == null || m.isEmpty();
    77 	}
    78 
    79 	public boolean isNotEmpty(Map m) {
    80 		return !isEmpty(m);
    81 	}
    82 
    83 	/**
    84 	 * @return empty collection if given one is null | or the original one
    85 	 */
    86 	public static <T> Collection<T> notNull(Collection<T> c) {
    87 		if (c == null) {
    88 			return new ArrayList<>();
    89 		} else {
    90 			return c;
    91 		}
    92 	}
    93 
    94 	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
    95 		for (T element : collection) {
    96 			if (equalz(element.getName(), name)) {
    97 				return element;
    98 			}
    99 		}
   100 
   101 		return null;
   102 	}
   103 
   104 	/**
   105 	 * Copy file from Java resources to file system.
   106 	 */
   107 	public static void installResource(String resourceName, File target) throws IOException {
   108 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
   109 			try (PrintWriter writer = new PrintWriter(target)) {
   110 				while (true) {
   111 					String line = reader.readLine();
   112 					if (line == null) {
   113 						break;
   114 					} else {
   115 						writer.println(line);
   116 					}
   117 				}
   118 			}
   119 		}
   120 	}
   121 }