java/sql-dk/src/info/globalcode/sql/dk/Functions.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 29 Sep 2014 10:34:54 +0200
branchv_0
changeset 181 80333d0ae763
parent 166 5488c2dcf680
child 213 39d154429f7a
permissions -rw-r--r--
fix imports
     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 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class Functions {
    37 
    38 	private Functions() {
    39 	}
    40 
    41 	public static boolean equalz(Object a, Object b) {
    42 		return a == null ? b == null : a.equals(b);
    43 	}
    44 
    45 	/**
    46 	 *
    47 	 * @param text String to be examinated
    48 	 * @param trim whether text should be trimmed before examination
    49 	 * @return whether text is not empty and one or more characters long (after prospective trim)
    50 	 */
    51 	public static boolean isEmpty(String text, boolean trim) {
    52 		if (text == null) {
    53 			return true;
    54 		} else {
    55 			if (trim) {
    56 				text = text.trim();
    57 			}
    58 			return text.isEmpty();
    59 		}
    60 	}
    61 
    62 	/**
    63 	 * @see #isEmpty(java.lang.String, boolean)
    64 	 */
    65 	public static boolean isNotEmpty(String text, boolean trim) {
    66 		return !isEmpty(text, trim);
    67 	}
    68 
    69 	public boolean isEmpty(Collection c) {
    70 		return c == null || c.isEmpty();
    71 	}
    72 
    73 	public boolean isNotEmpty(Collection c) {
    74 		return !isEmpty(c);
    75 	}
    76 
    77 	public boolean isEmpty(Map m) {
    78 		return m == null || m.isEmpty();
    79 	}
    80 
    81 	public boolean isNotEmpty(Map m) {
    82 		return !isEmpty(m);
    83 	}
    84 
    85 	/**
    86 	 * @return empty collection if given one is null | or the original one
    87 	 */
    88 	public static <T> Collection<T> notNull(Collection<T> c) {
    89 		if (c == null) {
    90 			return Collections.emptyList();
    91 		} else {
    92 			return c;
    93 		}
    94 	}
    95 
    96 	public static <T extends NameIdentified> T findByName(Collection<T> collection, String name) {
    97 		for (T element : notNull(collection)) {
    98 			if (element != null && equalz(element.getName(), name)) {
    99 				return element;
   100 			}
   101 		}
   102 
   103 		return null;
   104 	}
   105 
   106 	/**
   107 	 * Copy file from Java resources to file system.
   108 	 */
   109 	public static void installResource(String resourceName, File target) throws IOException {
   110 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
   111 			try (PrintWriter writer = new PrintWriter(target)) {
   112 				while (true) {
   113 					String line = reader.readLine();
   114 					if (line == null) {
   115 						break;
   116 					} else {
   117 						writer.println(line);
   118 					}
   119 				}
   120 			}
   121 		}
   122 	}
   123 
   124 	public static String rpad(String s, int n) {
   125 		if (n > 0) {
   126 			return String.format("%1$-" + n + "s", s);
   127 		} else {
   128 			return s;
   129 		}
   130 	}
   131 
   132 	public static String lpad(String s, int n) {
   133 		if (n > 0) {
   134 			return String.format("%1$" + n + "s", s);
   135 		} else {
   136 			return s;
   137 		}
   138 	}
   139 
   140 	public static String repeat(char ch, int count) {
   141 		char[] array = new char[count];
   142 		Arrays.fill(array, ch);
   143 		return new String(array);
   144 	}
   145 	private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
   146 
   147 	public static String toHex(byte[] bytes) {
   148 		char[] hexChars = new char[bytes.length * 2];
   149 		for (int j = 0; j < bytes.length; j++) {
   150 			int v = bytes[j] & 0xFF;
   151 			hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
   152 			hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
   153 		}
   154 		return new String(hexChars);
   155 	}
   156 
   157 	public static String readString(InputStream in) throws IOException {
   158 		try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
   159 			StringBuilder result = new StringBuilder();
   160 			for (String line = br.readLine(); line != null; line = br.readLine()) {
   161 				result.append(line);
   162 				result.append('\n');
   163 			}
   164 			return result.toString();
   165 		}
   166 	}
   167 }