3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.configuration.NameIdentified;
21 import java.io.BufferedReader;
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;
32 * @author Ing. František Kučera (frantovo.cz)
34 public class Functions {
39 public static boolean equalz(Object a, Object b) {
40 return a == null ? b == null : a.equals(b);
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)
49 public static boolean isEmpty(String text, boolean trim) {
56 return text.isEmpty();
61 * @see #isEmpty(java.lang.String, boolean)
63 public static boolean isNotEmpty(String text, boolean trim) {
64 return !isEmpty(text, trim);
67 public boolean isEmpty(Collection c) {
68 return c == null || c.isEmpty();
71 public boolean isNotEmpty(Collection c) {
75 public boolean isEmpty(Map m) {
76 return m == null || m.isEmpty();
79 public boolean isNotEmpty(Map m) {
84 * @return empty collection if given one is null | or the original one
86 public static <T> Collection<T> notNull(Collection<T> c) {
88 return new ArrayList<>();
94 public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
95 for (T element : collection) {
96 if (equalz(element.getName(), name)) {
105 * Copy file from Java resources to file system.
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)) {
111 String line = reader.readLine();
115 writer.println(line);
122 public static String rpad(String s, int n) {
123 return String.format("%1$-" + n + "s", s);
126 public static String lpad(String s, int n) {
127 return String.format("%1$" + n + "s", s);