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.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
33 * @author Ing. František Kučera (frantovo.cz)
35 public class Functions {
40 public static boolean equalz(Object a, Object b) {
41 return a == null ? b == null : a.equals(b);
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)
50 public static boolean isEmpty(String text, boolean trim) {
57 return text.isEmpty();
62 * @see #isEmpty(java.lang.String, boolean)
64 public static boolean isNotEmpty(String text, boolean trim) {
65 return !isEmpty(text, trim);
68 public boolean isEmpty(Collection c) {
69 return c == null || c.isEmpty();
72 public boolean isNotEmpty(Collection c) {
76 public boolean isEmpty(Map m) {
77 return m == null || m.isEmpty();
80 public boolean isNotEmpty(Map m) {
85 * @return empty collection if given one is null | or the original one
87 public static <T> Collection<T> notNull(Collection<T> c) {
89 return Collections.emptyList();
95 public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
96 for (T element : notNull(collection)) {
97 if (element != null && equalz(element.getName(), name)) {
106 * Copy file from Java resources to file system.
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)) {
112 String line = reader.readLine();
116 writer.println(line);
123 public static String rpad(String s, int n) {
125 return String.format("%1$-" + n + "s", s);
131 public static String lpad(String s, int n) {
133 return String.format("%1$" + n + "s", s);
139 public static String repeat(char ch, int count) {
140 char[] array = new char[count];
141 Arrays.fill(array, ch);
142 return new String(array);
144 private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
146 public static String toHex(byte[] bytes) {
147 char[] hexChars = new char[bytes.length * 2];
148 for (int j = 0; j < bytes.length; j++) {
149 int v = bytes[j] & 0xFF;
150 hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
151 hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
153 return new String(hexChars);