tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk;
19 import info.globalcode.sql.dk.configuration.NameIdentified;
20 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
21 import info.globalcode.sql.dk.configuration.PropertyDeclarations;
22 import info.globalcode.sql.dk.formatting.Formatter;
23 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.PrintWriter;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
40 * @author Ing. František Kučera (frantovo.cz)
42 public class Functions {
44 private static final String NBSP = " ";
45 private static final Pattern WHITESPACE_TO_REPLACE = Pattern.compile("\\n|\\r|\\t|" + NBSP);
50 public static boolean equalz(Object a, Object b) {
51 return a == null ? b == null : a.equals(b);
56 * @param text String to be examinated
57 * @param trim whether text should be trimmed before examination
58 * @return whether text is not empty and one or more characters long (after prospective trim)
60 public static boolean isEmpty(String text, boolean trim) {
67 return text.isEmpty();
72 * @see #isEmpty(java.lang.String, boolean)
74 public static boolean isNotEmpty(String text, boolean trim) {
75 return !isEmpty(text, trim);
78 public boolean isEmpty(Collection c) {
79 return c == null || c.isEmpty();
82 public boolean isNotEmpty(Collection c) {
86 public boolean isEmpty(Map m) {
87 return m == null || m.isEmpty();
90 public boolean isNotEmpty(Map m) {
95 * @return empty collection if given one is null | or the original one
97 public static <T> Collection<T> notNull(Collection<T> c) {
99 return Collections.emptyList();
105 public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
106 for (T element : notNull(collection)) {
107 if (element != null && equalz(element.getName(), name)) {
116 * Copy file from Java resources to file system.
118 public static void installResource(String resourceName, File target) throws IOException {
119 try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
120 try (PrintWriter writer = new PrintWriter(target)) {
122 String line = reader.readLine();
126 writer.println(line);
133 public static String rpad(String s, int n) {
135 return String.format("%1$-" + n + "s", s);
141 public static String lpad(String s, int n) {
143 return String.format("%1$" + n + "s", s);
149 public static String repeat(char ch, int count) {
150 char[] array = new char[count];
151 Arrays.fill(array, ch);
152 return new String(array);
154 private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
156 public static String toHex(byte[] bytes) {
157 char[] hexChars = new char[bytes.length * 2];
158 for (int j = 0; j < bytes.length; j++) {
159 int v = bytes[j] & 0xFF;
160 hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
161 hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
163 return new String(hexChars);
166 public static String readString(InputStream in) throws IOException {
167 try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
168 StringBuilder result = new StringBuilder();
169 for (String line = br.readLine(); line != null; line = br.readLine()) {
173 return result.toString();
178 * @param <P> type of the last parent
179 * @param <T> type of the examined class
180 * @param type examined class
181 * @param lastParent the last parent type to stop at
182 * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
184 public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
185 List<Class<? extends P>> hierarchy = new ArrayList<>();
187 for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
188 hierarchy.add(current);
194 public static PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
195 PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
197 if (properties == null) {
198 PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
199 return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
201 return properties.value();
206 * TODO: support background or styles and move to ColorfulPrintWriter
213 public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
215 Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
219 while (m.find(start)) {
221 printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
225 out.print(escapeColor, "↲");
228 out.print(escapeColor, "⏎");
231 out.print(escapeColor, "↹");
234 out.print(escapeColor, "⎵");
237 throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
243 printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
246 private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
250 out.print(color, text);