java/sql-dk/src/main/java/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 220 java/sql-dk/src/info/globalcode/sql/dk/Functions.java@0bc544b38cfa
child 250 aae5009bd0af
permissions -rw-r--r--
mavenized: sql-dk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     1
/**
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     2
 * SQL-DK
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     4
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     8
 * (at your option) any later version.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     9
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    13
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    14
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    15
 * You should have received a copy of the GNU General Public License
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    17
 */
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    20
import info.globalcode.sql.dk.configuration.NameIdentified;
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
    21
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
    22
import info.globalcode.sql.dk.configuration.PropertyDeclarations;
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
    23
import info.globalcode.sql.dk.formatting.Formatter;
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    24
import java.io.BufferedReader;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    25
import java.io.File;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    26
import java.io.IOException;
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
    27
import java.io.InputStream;
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    28
import java.io.InputStreamReader;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    29
import java.io.PrintWriter;
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
    30
import java.util.ArrayList;
40
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    31
import java.util.Arrays;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
import java.util.Collection;
125
46eb1925f2bb better notNull() function
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    33
import java.util.Collections;
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
    34
import java.util.List;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
import java.util.Map;
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
    36
import java.util.regex.Matcher;
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
    37
import java.util.regex.Pattern;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
/**
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
 *
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
 * @author Ing. František Kučera (frantovo.cz)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
public class Functions {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
    45
	private static final String NBSP = " ";
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
    46
	private static final Pattern WHITESPACE_TO_REPLACE = Pattern.compile("\\n|\\r|\\t|" + NBSP);
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
    47
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	private Functions() {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	public static boolean equalz(Object a, Object b) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		return a == null ? b == null : a.equals(b);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	/**
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 *
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 * @param text String to be examinated
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	 * @param trim whether text should be trimmed before examination
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	 * @return whether text is not empty and one or more characters long (after prospective trim)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	public static boolean isEmpty(String text, boolean trim) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		if (text == null) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			return true;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		} else {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			if (trim) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
				text = text.trim();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			return text.isEmpty();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	/**
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	 * @see #isEmpty(java.lang.String, boolean)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	public static boolean isNotEmpty(String text, boolean trim) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		return !isEmpty(text, trim);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	public boolean isEmpty(Collection c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		return c == null || c.isEmpty();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	public boolean isNotEmpty(Collection c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		return !isEmpty(c);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	public boolean isEmpty(Map m) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		return m == null || m.isEmpty();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	public boolean isNotEmpty(Map m) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		return !isEmpty(m);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	/**
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	 * @return empty collection if given one is null | or the original one
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	public static <T> Collection<T> notNull(Collection<T> c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		if (c == null) {
125
46eb1925f2bb better notNull() function
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
   100
			return Collections.emptyList();
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		} else {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
			return c;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	}
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   105
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   106
	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
104
245f1b88a3e6 formatter/database properties
František Kučera <franta-hg@frantovo.cz>
parents: 88
diff changeset
   107
		for (T element : notNull(collection)) {
54
53020d0bd2e4 named parameters: prefix/suffix are now regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 51
diff changeset
   108
			if (element != null && equalz(element.getName(), name)) {
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   109
				return element;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   110
			}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   111
		}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   112
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   113
		return null;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   114
	}
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   115
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   116
	/**
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   117
	 * Copy file from Java resources to file system.
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   118
	 */
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   119
	public static void installResource(String resourceName, File target) throws IOException {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   120
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   121
			try (PrintWriter writer = new PrintWriter(target)) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   122
				while (true) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   123
					String line = reader.readLine();
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   124
					if (line == null) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   125
						break;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   126
					} else {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   127
						writer.println(line);
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   128
					}
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   129
				}
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   130
			}
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   131
		}
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   132
	}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   133
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   134
	public static String rpad(String s, int n) {
88
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   135
		if (n > 0) {
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   136
			return String.format("%1$-" + n + "s", s);
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   137
		} else {
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   138
			return s;
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   139
		}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   140
	}
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   141
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   142
	public static String lpad(String s, int n) {
88
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   143
		if (n > 0) {
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   144
			return String.format("%1$" + n + "s", s);
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   145
		} else {
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   146
			return s;
102ba0fcb07f TabularPrefetchingFormatter: prefetch whole result set to avoid value overflow the cell
František Kučera <franta-hg@frantovo.cz>
parents: 54
diff changeset
   147
		}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   148
	}
40
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   149
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   150
	public static String repeat(char ch, int count) {
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   151
		char[] array = new char[count];
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   152
		Arrays.fill(array, ch);
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   153
		return new String(array);
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   154
	}
127
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   155
	private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   156
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   157
	public static String toHex(byte[] bytes) {
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   158
		char[] hexChars = new char[bytes.length * 2];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   159
		for (int j = 0; j < bytes.length; j++) {
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   160
			int v = bytes[j] & 0xFF;
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   161
			hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   162
			hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   163
		}
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   164
		return new String(hexChars);
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   165
	}
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   167
	public static String readString(InputStream in) throws IOException {
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   168
		try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   169
			StringBuilder result = new StringBuilder();
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   170
			for (String line = br.readLine(); line != null; line = br.readLine()) {
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   171
				result.append(line);
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   172
				result.append('\n');
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   173
			}
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   174
			return result.toString();
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   175
		}
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   176
	}
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   177
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   178
	/**
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   179
	 * @param <P> type of the last parent
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   180
	 * @param <T> type of the examined class
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   181
	 * @param type examined class
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   182
	 * @param lastParent the last parent type to stop at
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   183
	 * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   184
	 */
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   185
	public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   186
		List<Class<? extends P>> hierarchy = new ArrayList<>();
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   187
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   188
		for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   189
			hierarchy.add(current);
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   190
		}
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   191
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   192
		return hierarchy;
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   193
	}
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   194
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   195
	public static PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   196
		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   197
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   198
		if (properties == null) {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   199
			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   200
			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   201
		} else {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   202
			return properties.value();
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   203
		}
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   204
	}
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   205
218
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   206
	/**
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   207
	 * TODO: support background or styles and move to ColorfulPrintWriter
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   208
	 *
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   209
	 * @param out
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   210
	 * @param valueString
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   211
	 * @param basicColor
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   212
	 * @param escapeColor
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   213
	 */
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   214
	public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   215
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   216
		Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   217
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   218
		int start = 0;
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   219
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   220
		while (m.find(start)) {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   221
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   222
			printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   223
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   224
			switch (m.group()) {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   225
				case "\n":
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   226
					out.print(escapeColor, "↲");
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   227
					break;
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   228
				case "\r":
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   229
					out.print(escapeColor, "⏎");
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   230
					break;
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   231
				case "\t":
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   232
					out.print(escapeColor, "↹");
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   233
					break;
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   234
				case NBSP:
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   235
					out.print(escapeColor, "⎵");
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   236
					break;
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   237
				default:
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   238
					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   239
			}
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   240
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   241
			start = m.end();
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   242
		}
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   243
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   244
		printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   245
	}
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   246
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   247
	private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   248
		if (color == null) {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   249
			out.print(text);
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   250
		} else {
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   251
			out.print(color, text);
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   252
		}
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   253
	}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   254
}