java/sql-dk/src/main/java/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3
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
250
aae5009bd0af fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     8
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     9
 * 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
    10
 * 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
    11
 * 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
    12
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    13
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    14
 * 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
    15
 * 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
    16
 */
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
package info.globalcode.sql.dk;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    19
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
    20
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
    21
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
    22
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
    23
import java.io.BufferedReader;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    24
import java.io.File;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    25
import java.io.IOException;
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
    26
import java.io.InputStream;
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    27
import java.io.InputStreamReader;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    28
import java.io.PrintWriter;
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
    29
import java.util.ArrayList;
40
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    30
import java.util.Arrays;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
import java.util.Collection;
125
46eb1925f2bb better notNull() function
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    32
import java.util.Collections;
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
    33
import java.util.List;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
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
    35
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
    36
import java.util.regex.Pattern;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
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
 * @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
    41
 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
public class Functions {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
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
    44
	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
    45
	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
    46
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	private Functions() {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	}
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
	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
    51
		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
    52
	}
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
	 * @param text String to be examinated
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 * @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
    58
	 * @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
    59
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	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
    61
		if (text == null) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			return true;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		} else {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			if (trim) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
				text = text.trim();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			return text.isEmpty();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		}
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
	 * @see #isEmpty(java.lang.String, boolean)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	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
    75
		return !isEmpty(text, trim);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	}
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
	public boolean isEmpty(Collection c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		return c == null || c.isEmpty();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	}
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
	public boolean isNotEmpty(Collection c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		return !isEmpty(c);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	}
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
	public boolean isEmpty(Map m) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		return m == null || m.isEmpty();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
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
	public boolean isNotEmpty(Map m) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		return !isEmpty(m);
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	}
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
	 * @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
    96
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	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
    98
		if (c == null) {
125
46eb1925f2bb better notNull() function
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    99
			return Collections.emptyList();
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		} else {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
			return c;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
		}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	}
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   104
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   105
	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
   106
		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
   107
			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
   108
				return element;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   109
			}
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
		return null;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   113
	}
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   114
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
	 * 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
   117
	 */
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   118
	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
   119
		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
   120
			try (PrintWriter writer = new PrintWriter(target)) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   121
				while (true) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   122
					String line = reader.readLine();
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   123
					if (line == null) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   124
						break;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   125
					} else {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   126
						writer.println(line);
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   127
					}
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
	}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   132
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   133
	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
   134
		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
   135
			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
   136
		} 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
   137
			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
   138
		}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   139
	}
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
	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
   142
		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
   143
			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
   144
		} 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
   145
			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
   146
		}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   147
	}
40
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   148
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   149
	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
   150
		char[] array = new char[count];
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   151
		Arrays.fill(array, ch);
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   152
		return new String(array);
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   153
	}
127
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   154
	private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   155
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   156
	public static String toHex(byte[] bytes) {
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   157
		char[] hexChars = new char[bytes.length * 2];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   158
		for (int j = 0; j < bytes.length; j++) {
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   159
			int v = bytes[j] & 0xFF;
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   160
			hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   161
			hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   162
		}
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   163
		return new String(hexChars);
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   164
	}
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   165
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   166
	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
   167
		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
   168
			StringBuilder result = new StringBuilder();
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   169
			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
   170
				result.append(line);
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   171
				result.append('\n');
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   172
			}
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   173
			return result.toString();
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   174
		}
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   175
	}
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   176
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
	 * @param <P> type of the last parent
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   179
	 * @param <T> type of the examined class
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   180
	 * @param type examined class
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   181
	 * @param lastParent the last parent type to stop at
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   182
	 * @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
   183
	 */
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   184
	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
   185
		List<Class<? extends P>> hierarchy = new ArrayList<>();
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   186
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   187
		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
   188
			hierarchy.add(current);
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   189
		}
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
		return hierarchy;
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   192
	}
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
   193
220
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   194
	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
   195
		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   196
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   197
		if (properties == null) {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   198
			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   199
			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
   200
		} else {
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   201
			return properties.value();
0bc544b38cfa bash-completion: --formatter-property names
František Kučera <franta-hg@frantovo.cz>
parents: 218
diff changeset
   202
		}
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
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
   205
	/**
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
	 * 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
   207
	 *
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
	 * @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
   209
	 * @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
   210
	 * @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
   211
	 * @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
   212
	 */
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
	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
   214
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
		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
   216
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
		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
   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
   219
		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
   220
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
			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
   222
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
			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
   224
				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
   225
					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
   226
					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
   227
				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
   228
					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
   229
					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
   230
				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
   231
					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
   232
					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
   233
				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
   234
					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
   235
					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
   236
				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
   237
					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
   238
			}
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
			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
   241
		}
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
		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
   244
	}
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
	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
   247
		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
   248
			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
   249
		} 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
   250
			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
   251
		}
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
	}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   253
}