collection, String name) {
- for (T element : notNull(collection)) {
- if (element != null && equalz(element.getName(), name)) {
- return element;
- }
- }
-
- return null;
- }
-
- /**
- * Copy file from Java resources to file system.
- */
- public static void installResource(String resourceName, File target) throws IOException {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
- try (PrintWriter writer = new PrintWriter(target)) {
- while (true) {
- String line = reader.readLine();
- if (line == null) {
- break;
- } else {
- writer.println(line);
- }
- }
- }
- }
- }
-
- public static String rpad(String s, int n) {
- if (n > 0) {
- return String.format("%1$-" + n + "s", s);
- } else {
- return s;
- }
- }
-
- public static String lpad(String s, int n) {
- if (n > 0) {
- return String.format("%1$" + n + "s", s);
- } else {
- return s;
- }
- }
-
- public static String repeat(char ch, int count) {
- char[] array = new char[count];
- Arrays.fill(array, ch);
- return new String(array);
- }
- private final static char[] HEX_ALPHABET = "0123456789abcdef".toCharArray();
-
- public static String toHex(byte[] bytes) {
- char[] hexChars = new char[bytes.length * 2];
- for (int j = 0; j < bytes.length; j++) {
- int v = bytes[j] & 0xFF;
- hexChars[j * 2] = HEX_ALPHABET[v >>> 4];
- hexChars[j * 2 + 1] = HEX_ALPHABET[v & 0x0F];
- }
- return new String(hexChars);
- }
-
- public static String readString(InputStream in) throws IOException {
- try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
- StringBuilder result = new StringBuilder();
- for (String line = br.readLine(); line != null; line = br.readLine()) {
- result.append(line);
- result.append('\n');
- }
- return result.toString();
- }
- }
-
- /**
- * @param type of the last parent
- * @param type of the examined class
- * @param type examined class
- * @param lastParent the last parent type to stop at
- * @return list of types starting with type
and ending with lastParent
- */
- public static List> getClassHierarchy(Class type, Class lastParent) {
- List> hierarchy = new ArrayList<>();
-
- for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
- hierarchy.add(current);
- }
-
- return hierarchy;
- }
-
- public static PropertyDeclaration[] getPropertyDeclarations(Class extends Formatter> formatterClass) {
- PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
-
- if (properties == null) {
- PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
- return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
- } else {
- return properties.value();
- }
- }
-
- /**
- * TODO: support background or styles and move to ColorfulPrintWriter
- *
- * @param out
- * @param valueString
- * @param basicColor
- * @param escapeColor
- */
- public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
-
- Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
-
- int start = 0;
-
- while (m.find(start)) {
-
- printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
-
- switch (m.group()) {
- case "\n":
- out.print(escapeColor, "↲");
- break;
- case "\r":
- out.print(escapeColor, "⏎");
- break;
- case "\t":
- out.print(escapeColor, "↹");
- break;
- case NBSP:
- out.print(escapeColor, "⎵");
- break;
- default:
- throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
- }
-
- start = m.end();
- }
-
- printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
- }
-
- private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
- if (color == null) {
- out.print(text);
- } else {
- out.print(color, text);
- }
- }
-}