java/sql-dk/src/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 19 Jan 2014 18:30:21 +0100
branchv_0
changeset 167 84aaa91642bf
parent 166 5488c2dcf680
child 181 80333d0ae763
permissions -rw-r--r--
fix Tabular: table was broken if value ended with \n
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@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@1
    18
package info.globalcode.sql.dk;
franta-hg@1
    19
franta-hg@29
    20
import info.globalcode.sql.dk.configuration.NameIdentified;
franta-hg@33
    21
import java.io.BufferedReader;
franta-hg@33
    22
import java.io.File;
franta-hg@33
    23
import java.io.IOException;
franta-hg@166
    24
import java.io.InputStream;
franta-hg@33
    25
import java.io.InputStreamReader;
franta-hg@33
    26
import java.io.PrintWriter;
franta-hg@40
    27
import java.util.Arrays;
franta-hg@1
    28
import java.util.Collection;
franta-hg@125
    29
import java.util.Collections;
franta-hg@1
    30
import java.util.Map;
franta-hg@166
    31
import java.util.Scanner;
franta-hg@1
    32
franta-hg@1
    33
/**
franta-hg@1
    34
 *
franta-hg@1
    35
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    36
 */
franta-hg@1
    37
public class Functions {
franta-hg@1
    38
franta-hg@1
    39
	private Functions() {
franta-hg@1
    40
	}
franta-hg@1
    41
franta-hg@1
    42
	public static boolean equalz(Object a, Object b) {
franta-hg@1
    43
		return a == null ? b == null : a.equals(b);
franta-hg@1
    44
	}
franta-hg@1
    45
franta-hg@1
    46
	/**
franta-hg@1
    47
	 *
franta-hg@1
    48
	 * @param text String to be examinated
franta-hg@1
    49
	 * @param trim whether text should be trimmed before examination
franta-hg@1
    50
	 * @return whether text is not empty and one or more characters long (after prospective trim)
franta-hg@1
    51
	 */
franta-hg@1
    52
	public static boolean isEmpty(String text, boolean trim) {
franta-hg@1
    53
		if (text == null) {
franta-hg@1
    54
			return true;
franta-hg@1
    55
		} else {
franta-hg@1
    56
			if (trim) {
franta-hg@1
    57
				text = text.trim();
franta-hg@1
    58
			}
franta-hg@1
    59
			return text.isEmpty();
franta-hg@1
    60
		}
franta-hg@1
    61
	}
franta-hg@1
    62
franta-hg@1
    63
	/**
franta-hg@1
    64
	 * @see #isEmpty(java.lang.String, boolean)
franta-hg@1
    65
	 */
franta-hg@1
    66
	public static boolean isNotEmpty(String text, boolean trim) {
franta-hg@1
    67
		return !isEmpty(text, trim);
franta-hg@1
    68
	}
franta-hg@1
    69
franta-hg@1
    70
	public boolean isEmpty(Collection c) {
franta-hg@1
    71
		return c == null || c.isEmpty();
franta-hg@1
    72
	}
franta-hg@1
    73
franta-hg@1
    74
	public boolean isNotEmpty(Collection c) {
franta-hg@1
    75
		return !isEmpty(c);
franta-hg@1
    76
	}
franta-hg@1
    77
franta-hg@1
    78
	public boolean isEmpty(Map m) {
franta-hg@1
    79
		return m == null || m.isEmpty();
franta-hg@1
    80
	}
franta-hg@1
    81
franta-hg@1
    82
	public boolean isNotEmpty(Map m) {
franta-hg@1
    83
		return !isEmpty(m);
franta-hg@1
    84
	}
franta-hg@1
    85
franta-hg@1
    86
	/**
franta-hg@1
    87
	 * @return empty collection if given one is null | or the original one
franta-hg@1
    88
	 */
franta-hg@1
    89
	public static <T> Collection<T> notNull(Collection<T> c) {
franta-hg@1
    90
		if (c == null) {
franta-hg@125
    91
			return Collections.emptyList();
franta-hg@1
    92
		} else {
franta-hg@1
    93
			return c;
franta-hg@1
    94
		}
franta-hg@1
    95
	}
franta-hg@29
    96
franta-hg@29
    97
	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
franta-hg@104
    98
		for (T element : notNull(collection)) {
franta-hg@54
    99
			if (element != null && equalz(element.getName(), name)) {
franta-hg@29
   100
				return element;
franta-hg@29
   101
			}
franta-hg@29
   102
		}
franta-hg@29
   103
franta-hg@29
   104
		return null;
franta-hg@29
   105
	}
franta-hg@33
   106
franta-hg@33
   107
	/**
franta-hg@33
   108
	 * Copy file from Java resources to file system.
franta-hg@33
   109
	 */
franta-hg@33
   110
	public static void installResource(String resourceName, File target) throws IOException {
franta-hg@33
   111
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
franta-hg@33
   112
			try (PrintWriter writer = new PrintWriter(target)) {
franta-hg@33
   113
				while (true) {
franta-hg@33
   114
					String line = reader.readLine();
franta-hg@33
   115
					if (line == null) {
franta-hg@33
   116
						break;
franta-hg@33
   117
					} else {
franta-hg@33
   118
						writer.println(line);
franta-hg@33
   119
					}
franta-hg@33
   120
				}
franta-hg@33
   121
			}
franta-hg@33
   122
		}
franta-hg@33
   123
	}
franta-hg@38
   124
franta-hg@38
   125
	public static String rpad(String s, int n) {
franta-hg@88
   126
		if (n > 0) {
franta-hg@88
   127
			return String.format("%1$-" + n + "s", s);
franta-hg@88
   128
		} else {
franta-hg@88
   129
			return s;
franta-hg@88
   130
		}
franta-hg@38
   131
	}
franta-hg@38
   132
franta-hg@38
   133
	public static String lpad(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@40
   140
franta-hg@40
   141
	public static String repeat(char ch, int count) {
franta-hg@40
   142
		char[] array = new char[count];
franta-hg@40
   143
		Arrays.fill(array, ch);
franta-hg@40
   144
		return new String(array);
franta-hg@40
   145
	}
franta-hg@127
   146
	private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
franta-hg@127
   147
franta-hg@127
   148
	public static String toHex(byte[] bytes) {
franta-hg@127
   149
		char[] hexChars = new char[bytes.length * 2];
franta-hg@127
   150
		for (int j = 0; j < bytes.length; j++) {
franta-hg@127
   151
			int v = bytes[j] & 0xFF;
franta-hg@127
   152
			hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
franta-hg@127
   153
			hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
franta-hg@127
   154
		}
franta-hg@127
   155
		return new String(hexChars);
franta-hg@127
   156
	}
franta-hg@166
   157
franta-hg@166
   158
	public static String readString(InputStream in) throws IOException {
franta-hg@166
   159
		try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
franta-hg@166
   160
			StringBuilder result = new StringBuilder();
franta-hg@166
   161
			for (String line = br.readLine(); line != null; line = br.readLine()) {
franta-hg@166
   162
				result.append(line);
franta-hg@166
   163
				result.append('\n');
franta-hg@166
   164
			}
franta-hg@166
   165
			return result.toString();
franta-hg@166
   166
		}
franta-hg@166
   167
	}
franta-hg@1
   168
}