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