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 info.globalcode.sql.dk.configuration.PropertyDeclaration;
22 import info.globalcode.sql.dk.configuration.PropertyDeclarations;
23 import info.globalcode.sql.dk.formatting.Formatter;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
41 * @author Ing. František Kučera (frantovo.cz)
43 public class Functions {
45 private static final String NBSP = " ";
46 private static final Pattern WHITESPACE_TO_REPLACE = Pattern.compile("\\n|\\r|\\t|" + NBSP);
51 public static boolean equalz(Object a, Object b) {
52 return a == null ? b == null : a.equals(b);
57 * @param text String to be examinated
58 * @param trim whether text should be trimmed before examination
59 * @return whether text is not empty and one or more characters long (after prospective trim)
61 public static boolean isEmpty(String text, boolean trim) {
68 return text.isEmpty();
73 * @see #isEmpty(java.lang.String, boolean)
75 public static boolean isNotEmpty(String text, boolean trim) {
76 return !isEmpty(text, trim);
79 public boolean isEmpty(Collection c) {
80 return c == null || c.isEmpty();
83 public boolean isNotEmpty(Collection c) {
87 public boolean isEmpty(Map m) {
88 return m == null || m.isEmpty();
91 public boolean isNotEmpty(Map m) {
96 * @return empty collection if given one is null | or the original one
98 public static <T> Collection<T> notNull(Collection<T> c) {
100 return Collections.emptyList();
106 public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
107 for (T element : notNull(collection)) {
108 if (element != null && equalz(element.getName(), name)) {
117 * Copy file from Java resources to file system.
119 public static void installResource(String resourceName, File target) throws IOException {
120 try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
121 try (PrintWriter writer = new PrintWriter(target)) {
123 String line = reader.readLine();
127 writer.println(line);
134 public static String rpad(String s, int n) {
136 return String.format("%1$-" + n + "s", s);
142 public static String lpad(String s, int n) {
144 return String.format("%1$" + n + "s", s);
150 public static String repeat(char ch, int count) {
151 char[] array = new char[count];
152 Arrays.fill(array, ch);
153 return new String(array);
155 private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
157 public static String toHex(byte[] bytes) {
158 char[] hexChars = new char[bytes.length * 2];
159 for (int j = 0; j < bytes.length; j++) {
160 int v = bytes[j] & 0xFF;
161 hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
162 hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
164 return new String(hexChars);
167 public static String readString(InputStream in) throws IOException {
168 try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
169 StringBuilder result = new StringBuilder();
170 for (String line = br.readLine(); line != null; line = br.readLine()) {
174 return result.toString();
179 * @param <P> type of the last parent
180 * @param <T> type of the examined class
181 * @param type examined class
182 * @param lastParent the last parent type to stop at
183 * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
185 public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
186 List<Class<? extends P>> hierarchy = new ArrayList<>();
188 for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
189 hierarchy.add(current);
195 public static PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
196 PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
198 if (properties == null) {
199 PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
200 return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
202 return properties.value();
207 * TODO: support background or styles and move to ColorfulPrintWriter
214 public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
216 Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
220 while (m.find(start)) {
222 printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
226 out.print(escapeColor, "↲");
229 out.print(escapeColor, "⏎");
232 out.print(escapeColor, "↹");
235 out.print(escapeColor, "⎵");
238 throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
244 printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
247 private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
251 out.print(color, text);