java/sql-dk/src/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 18:22:19 +0100
branchv_0
changeset 83 9563232ea0b7
parent 54 53020d0bd2e4
child 88 102ba0fcb07f
permissions -rw-r--r--
bash completion: license
     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.Arrays;
    28 import java.util.Collection;
    29 import java.util.Map;
    30 
    31 /**
    32  *
    33  * @author Ing. František Kučera (frantovo.cz)
    34  */
    35 public class Functions {
    36 
    37 	private Functions() {
    38 	}
    39 
    40 	public static boolean equalz(Object a, Object b) {
    41 		return a == null ? b == null : a.equals(b);
    42 	}
    43 
    44 	/**
    45 	 *
    46 	 * @param text String to be examinated
    47 	 * @param trim whether text should be trimmed before examination
    48 	 * @return whether text is not empty and one or more characters long (after prospective trim)
    49 	 */
    50 	public static boolean isEmpty(String text, boolean trim) {
    51 		if (text == null) {
    52 			return true;
    53 		} else {
    54 			if (trim) {
    55 				text = text.trim();
    56 			}
    57 			return text.isEmpty();
    58 		}
    59 	}
    60 
    61 	/**
    62 	 * @see #isEmpty(java.lang.String, boolean)
    63 	 */
    64 	public static boolean isNotEmpty(String text, boolean trim) {
    65 		return !isEmpty(text, trim);
    66 	}
    67 
    68 	public boolean isEmpty(Collection c) {
    69 		return c == null || c.isEmpty();
    70 	}
    71 
    72 	public boolean isNotEmpty(Collection c) {
    73 		return !isEmpty(c);
    74 	}
    75 
    76 	public boolean isEmpty(Map m) {
    77 		return m == null || m.isEmpty();
    78 	}
    79 
    80 	public boolean isNotEmpty(Map m) {
    81 		return !isEmpty(m);
    82 	}
    83 
    84 	/**
    85 	 * @return empty collection if given one is null | or the original one
    86 	 */
    87 	public static <T> Collection<T> notNull(Collection<T> c) {
    88 		if (c == null) {
    89 			return new ArrayList<>();
    90 		} else {
    91 			return c;
    92 		}
    93 	}
    94 
    95 	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
    96 		for (T element : collection) {
    97 			if (element != null && equalz(element.getName(), name)) {
    98 				return element;
    99 			}
   100 		}
   101 
   102 		return null;
   103 	}
   104 
   105 	/**
   106 	 * Copy file from Java resources to file system.
   107 	 */
   108 	public static void installResource(String resourceName, File target) throws IOException {
   109 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
   110 			try (PrintWriter writer = new PrintWriter(target)) {
   111 				while (true) {
   112 					String line = reader.readLine();
   113 					if (line == null) {
   114 						break;
   115 					} else {
   116 						writer.println(line);
   117 					}
   118 				}
   119 			}
   120 		}
   121 	}
   122 
   123 	public static String rpad(String s, int n) {
   124 		return String.format("%1$-" + n + "s", s);
   125 	}
   126 
   127 	public static String lpad(String s, int n) {
   128 		return String.format("%1$" + n + "s", s);
   129 	}
   130 
   131 	public static String repeat(char ch, int count) {
   132 		char[] array = new char[count];
   133 		Arrays.fill(array, ch);
   134 		return new String(array);
   135 	}
   136 }