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