franta-hg@16: /**
franta-hg@16: * SQL-DK
franta-hg@16: * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16: *
franta-hg@16: * This program is free software: you can redistribute it and/or modify
franta-hg@16: * it under the terms of the GNU General Public License as published by
franta-hg@16: * the Free Software Foundation, either version 3 of the License, or
franta-hg@16: * (at your option) any later version.
franta-hg@16: *
franta-hg@16: * This program is distributed in the hope that it will be useful,
franta-hg@16: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16: * GNU General Public License for more details.
franta-hg@16: *
franta-hg@16: * You should have received a copy of the GNU General Public License
franta-hg@16: * along with this program. If not, see .
franta-hg@16: */
franta-hg@1: package info.globalcode.sql.dk;
franta-hg@1:
franta-hg@29: import info.globalcode.sql.dk.configuration.NameIdentified;
franta-hg@33: import java.io.BufferedReader;
franta-hg@33: import java.io.File;
franta-hg@33: import java.io.IOException;
franta-hg@166: import java.io.InputStream;
franta-hg@33: import java.io.InputStreamReader;
franta-hg@33: import java.io.PrintWriter;
franta-hg@40: import java.util.Arrays;
franta-hg@1: import java.util.Collection;
franta-hg@125: import java.util.Collections;
franta-hg@1: import java.util.Map;
franta-hg@1:
franta-hg@1: /**
franta-hg@1: *
franta-hg@1: * @author Ing. František Kučera (frantovo.cz)
franta-hg@1: */
franta-hg@1: public class Functions {
franta-hg@1:
franta-hg@1: private Functions() {
franta-hg@1: }
franta-hg@1:
franta-hg@1: public static boolean equalz(Object a, Object b) {
franta-hg@1: return a == null ? b == null : a.equals(b);
franta-hg@1: }
franta-hg@1:
franta-hg@1: /**
franta-hg@1: *
franta-hg@1: * @param text String to be examinated
franta-hg@1: * @param trim whether text should be trimmed before examination
franta-hg@1: * @return whether text is not empty and one or more characters long (after prospective trim)
franta-hg@1: */
franta-hg@1: public static boolean isEmpty(String text, boolean trim) {
franta-hg@1: if (text == null) {
franta-hg@1: return true;
franta-hg@1: } else {
franta-hg@1: if (trim) {
franta-hg@1: text = text.trim();
franta-hg@1: }
franta-hg@1: return text.isEmpty();
franta-hg@1: }
franta-hg@1: }
franta-hg@1:
franta-hg@1: /**
franta-hg@1: * @see #isEmpty(java.lang.String, boolean)
franta-hg@1: */
franta-hg@1: public static boolean isNotEmpty(String text, boolean trim) {
franta-hg@1: return !isEmpty(text, trim);
franta-hg@1: }
franta-hg@1:
franta-hg@1: public boolean isEmpty(Collection c) {
franta-hg@1: return c == null || c.isEmpty();
franta-hg@1: }
franta-hg@1:
franta-hg@1: public boolean isNotEmpty(Collection c) {
franta-hg@1: return !isEmpty(c);
franta-hg@1: }
franta-hg@1:
franta-hg@1: public boolean isEmpty(Map m) {
franta-hg@1: return m == null || m.isEmpty();
franta-hg@1: }
franta-hg@1:
franta-hg@1: public boolean isNotEmpty(Map m) {
franta-hg@1: return !isEmpty(m);
franta-hg@1: }
franta-hg@1:
franta-hg@1: /**
franta-hg@1: * @return empty collection if given one is null | or the original one
franta-hg@1: */
franta-hg@1: public static Collection notNull(Collection c) {
franta-hg@1: if (c == null) {
franta-hg@125: return Collections.emptyList();
franta-hg@1: } else {
franta-hg@1: return c;
franta-hg@1: }
franta-hg@1: }
franta-hg@29:
franta-hg@29: public static T findByName(Collection collection, String name) {
franta-hg@104: for (T element : notNull(collection)) {
franta-hg@54: if (element != null && equalz(element.getName(), name)) {
franta-hg@29: return element;
franta-hg@29: }
franta-hg@29: }
franta-hg@29:
franta-hg@29: return null;
franta-hg@29: }
franta-hg@33:
franta-hg@33: /**
franta-hg@33: * Copy file from Java resources to file system.
franta-hg@33: */
franta-hg@33: public static void installResource(String resourceName, File target) throws IOException {
franta-hg@33: try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
franta-hg@33: try (PrintWriter writer = new PrintWriter(target)) {
franta-hg@33: while (true) {
franta-hg@33: String line = reader.readLine();
franta-hg@33: if (line == null) {
franta-hg@33: break;
franta-hg@33: } else {
franta-hg@33: writer.println(line);
franta-hg@33: }
franta-hg@33: }
franta-hg@33: }
franta-hg@33: }
franta-hg@33: }
franta-hg@38:
franta-hg@38: public static String rpad(String s, int n) {
franta-hg@88: if (n > 0) {
franta-hg@88: return String.format("%1$-" + n + "s", s);
franta-hg@88: } else {
franta-hg@88: return s;
franta-hg@88: }
franta-hg@38: }
franta-hg@38:
franta-hg@38: public static String lpad(String s, int n) {
franta-hg@88: if (n > 0) {
franta-hg@88: return String.format("%1$" + n + "s", s);
franta-hg@88: } else {
franta-hg@88: return s;
franta-hg@88: }
franta-hg@38: }
franta-hg@40:
franta-hg@40: public static String repeat(char ch, int count) {
franta-hg@40: char[] array = new char[count];
franta-hg@40: Arrays.fill(array, ch);
franta-hg@40: return new String(array);
franta-hg@40: }
franta-hg@127: private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
franta-hg@127:
franta-hg@127: public static String toHex(byte[] bytes) {
franta-hg@127: char[] hexChars = new char[bytes.length * 2];
franta-hg@127: for (int j = 0; j < bytes.length; j++) {
franta-hg@127: int v = bytes[j] & 0xFF;
franta-hg@127: hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
franta-hg@127: hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
franta-hg@127: }
franta-hg@127: return new String(hexChars);
franta-hg@127: }
franta-hg@166:
franta-hg@166: public static String readString(InputStream in) throws IOException {
franta-hg@166: try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
franta-hg@166: StringBuilder result = new StringBuilder();
franta-hg@166: for (String line = br.readLine(); line != null; line = br.readLine()) {
franta-hg@166: result.append(line);
franta-hg@166: result.append('\n');
franta-hg@166: }
franta-hg@166: return result.toString();
franta-hg@166: }
franta-hg@166: }
franta-hg@1: }