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.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.PrintWriter;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
31 import java.util.Scanner;
35 * @author Ing. František Kučera (frantovo.cz)
37 public class Functions {
42 public static boolean equalz(Object a, Object b) {
43 return a == null ? b == null : a.equals(b);
48 * @param text String to be examinated
49 * @param trim whether text should be trimmed before examination
50 * @return whether text is not empty and one or more characters long (after prospective trim)
52 public static boolean isEmpty(String text, boolean trim) {
59 return text.isEmpty();
64 * @see #isEmpty(java.lang.String, boolean)
66 public static boolean isNotEmpty(String text, boolean trim) {
67 return !isEmpty(text, trim);
70 public boolean isEmpty(Collection c) {
71 return c == null || c.isEmpty();
74 public boolean isNotEmpty(Collection c) {
78 public boolean isEmpty(Map m) {
79 return m == null || m.isEmpty();
82 public boolean isNotEmpty(Map m) {
87 * @return empty collection if given one is null | or the original one
89 public static <T> Collection<T> notNull(Collection<T> c) {
91 return Collections.emptyList();
97 public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
98 for (T element : notNull(collection)) {
99 if (element != null && equalz(element.getName(), name)) {
108 * Copy file from Java resources to file system.
110 public static void installResource(String resourceName, File target) throws IOException {
111 try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
112 try (PrintWriter writer = new PrintWriter(target)) {
114 String line = reader.readLine();
118 writer.println(line);
125 public static String rpad(String s, int n) {
127 return String.format("%1$-" + n + "s", s);
133 public static String lpad(String s, int n) {
135 return String.format("%1$" + n + "s", s);
141 public static String repeat(char ch, int count) {
142 char[] array = new char[count];
143 Arrays.fill(array, ch);
144 return new String(array);
146 private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
148 public static String toHex(byte[] bytes) {
149 char[] hexChars = new char[bytes.length * 2];
150 for (int j = 0; j < bytes.length; j++) {
151 int v = bytes[j] & 0xFF;
152 hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
153 hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
155 return new String(hexChars);
158 public static String readString(InputStream in) throws IOException {
159 try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
160 StringBuilder result = new StringBuilder();
161 for (String line = br.readLine(); line != null; line = br.readLine()) {
165 return result.toString();