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