java/sql-dk/src/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 16:12:06 +0200
branchv_0
changeset 218 8e38caf43ca8
parent 213 39d154429f7a
child 220 0bc544b38cfa
permissions -rw-r--r--
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
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;
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    21
import java.io.BufferedReader;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    22
import java.io.File;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    23
import java.io.IOException;
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
    24
import java.io.InputStream;
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    25
import java.io.InputStreamReader;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    26
import java.io.PrintWriter;
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
    27
import java.util.ArrayList;
40
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    28
import java.util.Arrays;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
import java.util.Collection;
125
46eb1925f2bb better notNull() function
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    30
import java.util.Collections;
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
    31
import java.util.List;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
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
    33
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
    34
import java.util.regex.Pattern;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
/**
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
 * @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
    39
 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
public class Functions {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
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
    42
	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
    43
	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
    44
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	private Functions() {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	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
    49
		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
    50
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
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
	 * @param text String to be examinated
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	 * @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
    56
	 * @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
    57
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	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
    59
		if (text == null) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			return true;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		} else {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			if (trim) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
				text = text.trim();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			return text.isEmpty();
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
	}
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
	 * @see #isEmpty(java.lang.String, boolean)
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
	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
    73
		return !isEmpty(text, trim);
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
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	public boolean isEmpty(Collection c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		return c == null || c.isEmpty();
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
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	public boolean isNotEmpty(Collection c) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		return !isEmpty(c);
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
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	public boolean isEmpty(Map m) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		return m == null || m.isEmpty();
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
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	public boolean isNotEmpty(Map m) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		return !isEmpty(m);
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
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
	 * @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
    94
	 */
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	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
    96
		if (c == null) {
125
46eb1925f2bb better notNull() function
František Kučera <franta-hg@frantovo.cz>
parents: 104
diff changeset
    97
			return Collections.emptyList();
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		} else {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
			return c;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	}
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   102
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   103
	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
   104
		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
   105
			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
   106
				return element;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   107
			}
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   108
		}
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
		return null;
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   111
	}
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   112
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   113
	/**
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   114
	 * 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
   115
	 */
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   116
	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
   117
		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
   118
			try (PrintWriter writer = new PrintWriter(target)) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   119
				while (true) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   120
					String line = reader.readLine();
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   121
					if (line == null) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   122
						break;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   123
					} else {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   124
						writer.println(line);
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   125
					}
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   126
				}
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
	}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   130
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   131
	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
   132
		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
   133
			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
   134
		} 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
   135
			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
   136
		}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   137
	}
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   138
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   139
	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
   140
		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
   141
			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
   142
		} 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
   143
			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
   144
		}
38
ff5bbc06ed29 String padding functions: lpad(), rpad()
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   145
	}
40
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   146
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   147
	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
   148
		char[] array = new char[count];
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   149
		Arrays.fill(array, ch);
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   150
		return new String(array);
a9db7fb3ce65 TabularFormatter: print colorful tables\!
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   151
	}
127
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   152
	private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   153
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   154
	public static String toHex(byte[] bytes) {
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   155
		char[] hexChars = new char[bytes.length * 2];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   156
		for (int j = 0; j < bytes.length; j++) {
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   157
			int v = bytes[j] & 0xFF;
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   158
			hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   159
			hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   160
		}
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   161
		return new String(hexChars);
d63de8a0a61f new function: foHex()
František Kučera <franta-hg@frantovo.cz>
parents: 125
diff changeset
   162
	}
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   163
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   164
	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
   165
		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
   166
			StringBuilder result = new StringBuilder();
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   167
			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
   168
				result.append(line);
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   169
				result.append('\n');
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   170
			}
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 127
diff changeset
   171
			return result.toString();
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
	}
213
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   174
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   175
	/**
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   176
	 * @param <P> type of the last parent
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   177
	 * @param <T> type of the examined class
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   178
	 * @param type examined class
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   179
	 * @param lastParent the last parent type to stop at
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   180
	 * @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
   181
	 */
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   182
	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
   183
		List<Class<? extends P>> hierarchy = new ArrayList<>();
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
		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
   186
			hierarchy.add(current);
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
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   189
		return hierarchy;
39d154429f7a getClassHierarchy function
František Kučera <franta-hg@frantovo.cz>
parents: 181
diff changeset
   190
	}
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
   191
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   192
	/**
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
	 * 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
   194
	 *
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   195
	 * @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
   196
	 * @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
   197
	 * @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
   198
	 * @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
   199
	 */
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   200
	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
   201
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   202
		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
   203
8e38caf43ca8 SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
František Kučera <franta-hg@frantovo.cz>
parents: 213
diff changeset
   204
		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
   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
		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
   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
			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
   209
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
			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
   211
				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
   212
					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
   213
					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
   214
				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
   215
					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
   216
					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
   217
				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
   218
					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
   219
					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
   220
				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
   221
					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
   222
					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
   223
				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
   224
					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
   225
			}
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
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
			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
   228
		}
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
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
		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
   231
	}
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
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
	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
   234
		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
   235
			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
   236
		} 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
   237
			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
   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
	}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
}