java/sql-dk/src/main/java/info/globalcode/sql/dk/Functions.java
branchv_0
changeset 238 4a1864c3e867
parent 220 0bc544b38cfa
child 250 aae5009bd0af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/Functions.java	Mon Mar 04 20:15:24 2019 +0100
     1.3 @@ -0,0 +1,254 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk;
    1.22 +
    1.23 +import info.globalcode.sql.dk.configuration.NameIdentified;
    1.24 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
    1.25 +import info.globalcode.sql.dk.configuration.PropertyDeclarations;
    1.26 +import info.globalcode.sql.dk.formatting.Formatter;
    1.27 +import java.io.BufferedReader;
    1.28 +import java.io.File;
    1.29 +import java.io.IOException;
    1.30 +import java.io.InputStream;
    1.31 +import java.io.InputStreamReader;
    1.32 +import java.io.PrintWriter;
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.Arrays;
    1.35 +import java.util.Collection;
    1.36 +import java.util.Collections;
    1.37 +import java.util.List;
    1.38 +import java.util.Map;
    1.39 +import java.util.regex.Matcher;
    1.40 +import java.util.regex.Pattern;
    1.41 +
    1.42 +/**
    1.43 + *
    1.44 + * @author Ing. František Kučera (frantovo.cz)
    1.45 + */
    1.46 +public class Functions {
    1.47 +
    1.48 +	private static final String NBSP = " ";
    1.49 +	private static final Pattern WHITESPACE_TO_REPLACE = Pattern.compile("\\n|\\r|\\t|" + NBSP);
    1.50 +
    1.51 +	private Functions() {
    1.52 +	}
    1.53 +
    1.54 +	public static boolean equalz(Object a, Object b) {
    1.55 +		return a == null ? b == null : a.equals(b);
    1.56 +	}
    1.57 +
    1.58 +	/**
    1.59 +	 *
    1.60 +	 * @param text String to be examinated
    1.61 +	 * @param trim whether text should be trimmed before examination
    1.62 +	 * @return whether text is not empty and one or more characters long (after prospective trim)
    1.63 +	 */
    1.64 +	public static boolean isEmpty(String text, boolean trim) {
    1.65 +		if (text == null) {
    1.66 +			return true;
    1.67 +		} else {
    1.68 +			if (trim) {
    1.69 +				text = text.trim();
    1.70 +			}
    1.71 +			return text.isEmpty();
    1.72 +		}
    1.73 +	}
    1.74 +
    1.75 +	/**
    1.76 +	 * @see #isEmpty(java.lang.String, boolean)
    1.77 +	 */
    1.78 +	public static boolean isNotEmpty(String text, boolean trim) {
    1.79 +		return !isEmpty(text, trim);
    1.80 +	}
    1.81 +
    1.82 +	public boolean isEmpty(Collection c) {
    1.83 +		return c == null || c.isEmpty();
    1.84 +	}
    1.85 +
    1.86 +	public boolean isNotEmpty(Collection c) {
    1.87 +		return !isEmpty(c);
    1.88 +	}
    1.89 +
    1.90 +	public boolean isEmpty(Map m) {
    1.91 +		return m == null || m.isEmpty();
    1.92 +	}
    1.93 +
    1.94 +	public boolean isNotEmpty(Map m) {
    1.95 +		return !isEmpty(m);
    1.96 +	}
    1.97 +
    1.98 +	/**
    1.99 +	 * @return empty collection if given one is null | or the original one
   1.100 +	 */
   1.101 +	public static <T> Collection<T> notNull(Collection<T> c) {
   1.102 +		if (c == null) {
   1.103 +			return Collections.emptyList();
   1.104 +		} else {
   1.105 +			return c;
   1.106 +		}
   1.107 +	}
   1.108 +
   1.109 +	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
   1.110 +		for (T element : notNull(collection)) {
   1.111 +			if (element != null && equalz(element.getName(), name)) {
   1.112 +				return element;
   1.113 +			}
   1.114 +		}
   1.115 +
   1.116 +		return null;
   1.117 +	}
   1.118 +
   1.119 +	/**
   1.120 +	 * Copy file from Java resources to file system.
   1.121 +	 */
   1.122 +	public static void installResource(String resourceName, File target) throws IOException {
   1.123 +		try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
   1.124 +			try (PrintWriter writer = new PrintWriter(target)) {
   1.125 +				while (true) {
   1.126 +					String line = reader.readLine();
   1.127 +					if (line == null) {
   1.128 +						break;
   1.129 +					} else {
   1.130 +						writer.println(line);
   1.131 +					}
   1.132 +				}
   1.133 +			}
   1.134 +		}
   1.135 +	}
   1.136 +
   1.137 +	public static String rpad(String s, int n) {
   1.138 +		if (n > 0) {
   1.139 +			return String.format("%1$-" + n + "s", s);
   1.140 +		} else {
   1.141 +			return s;
   1.142 +		}
   1.143 +	}
   1.144 +
   1.145 +	public static String lpad(String s, int n) {
   1.146 +		if (n > 0) {
   1.147 +			return String.format("%1$" + n + "s", s);
   1.148 +		} else {
   1.149 +			return s;
   1.150 +		}
   1.151 +	}
   1.152 +
   1.153 +	public static String repeat(char ch, int count) {
   1.154 +		char[] array = new char[count];
   1.155 +		Arrays.fill(array, ch);
   1.156 +		return new String(array);
   1.157 +	}
   1.158 +	private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
   1.159 +
   1.160 +	public static String toHex(byte[] bytes) {
   1.161 +		char[] hexChars = new char[bytes.length * 2];
   1.162 +		for (int j = 0; j < bytes.length; j++) {
   1.163 +			int v = bytes[j] & 0xFF;
   1.164 +			hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
   1.165 +			hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
   1.166 +		}
   1.167 +		return new String(hexChars);
   1.168 +	}
   1.169 +
   1.170 +	public static String readString(InputStream in) throws IOException {
   1.171 +		try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
   1.172 +			StringBuilder result = new StringBuilder();
   1.173 +			for (String line = br.readLine(); line != null; line = br.readLine()) {
   1.174 +				result.append(line);
   1.175 +				result.append('\n');
   1.176 +			}
   1.177 +			return result.toString();
   1.178 +		}
   1.179 +	}
   1.180 +
   1.181 +	/**
   1.182 +	 * @param <P> type of the last parent
   1.183 +	 * @param <T> type of the examined class
   1.184 +	 * @param type examined class
   1.185 +	 * @param lastParent the last parent type to stop at
   1.186 +	 * @return list of types starting with <code>type</code> and ending with <code>lastParent</code>
   1.187 +	 */
   1.188 +	public static <P, T extends P> List<Class<? extends P>> getClassHierarchy(Class<T> type, Class<P> lastParent) {
   1.189 +		List<Class<? extends P>> hierarchy = new ArrayList<>();
   1.190 +
   1.191 +		for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
   1.192 +			hierarchy.add(current);
   1.193 +		}
   1.194 +
   1.195 +		return hierarchy;
   1.196 +	}
   1.197 +
   1.198 +	public static PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
   1.199 +		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
   1.200 +
   1.201 +		if (properties == null) {
   1.202 +			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
   1.203 +			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
   1.204 +		} else {
   1.205 +			return properties.value();
   1.206 +		}
   1.207 +	}
   1.208 +
   1.209 +	/**
   1.210 +	 * TODO: support background or styles and move to ColorfulPrintWriter
   1.211 +	 *
   1.212 +	 * @param out
   1.213 +	 * @param valueString
   1.214 +	 * @param basicColor
   1.215 +	 * @param escapeColor
   1.216 +	 */
   1.217 +	public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
   1.218 +
   1.219 +		Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
   1.220 +
   1.221 +		int start = 0;
   1.222 +
   1.223 +		while (m.find(start)) {
   1.224 +
   1.225 +			printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
   1.226 +
   1.227 +			switch (m.group()) {
   1.228 +				case "\n":
   1.229 +					out.print(escapeColor, "↲");
   1.230 +					break;
   1.231 +				case "\r":
   1.232 +					out.print(escapeColor, "⏎");
   1.233 +					break;
   1.234 +				case "\t":
   1.235 +					out.print(escapeColor, "↹");
   1.236 +					break;
   1.237 +				case NBSP:
   1.238 +					out.print(escapeColor, "⎵");
   1.239 +					break;
   1.240 +				default:
   1.241 +					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
   1.242 +			}
   1.243 +
   1.244 +			start = m.end();
   1.245 +		}
   1.246 +
   1.247 +		printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
   1.248 +	}
   1.249 +
   1.250 +	private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
   1.251 +		if (color == null) {
   1.252 +			out.print(text);
   1.253 +		} else {
   1.254 +			out.print(color, text);
   1.255 +		}
   1.256 +	}
   1.257 +}