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@213:
franta-hg@213: /**
franta-hg@213: * @param type of the last parent
franta-hg@213: * @param type of the examined class
franta-hg@213: * @param type examined class
franta-hg@213: * @param lastParent the last parent type to stop at
franta-hg@213: * @return list of types starting with type
and ending with lastParent
franta-hg@213: */
franta-hg@213: public static List> getClassHierarchy(Class type, Class lastParent) {
franta-hg@213: List> hierarchy = new ArrayList<>();
franta-hg@213:
franta-hg@213: for (Class current = type; current != null && lastParent.isAssignableFrom(current); current = current.getSuperclass()) {
franta-hg@213: hierarchy.add(current);
franta-hg@213: }
franta-hg@213:
franta-hg@213: return hierarchy;
franta-hg@213: }
franta-hg@218:
franta-hg@220: public static PropertyDeclaration[] getPropertyDeclarations(Class extends Formatter> formatterClass) {
franta-hg@220: PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
franta-hg@220:
franta-hg@220: if (properties == null) {
franta-hg@220: PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
franta-hg@220: return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
franta-hg@220: } else {
franta-hg@220: return properties.value();
franta-hg@220: }
franta-hg@220: }
franta-hg@220:
franta-hg@218: /**
franta-hg@218: * TODO: support background or styles and move to ColorfulPrintWriter
franta-hg@218: *
franta-hg@218: * @param out
franta-hg@218: * @param valueString
franta-hg@218: * @param basicColor
franta-hg@218: * @param escapeColor
franta-hg@218: */
franta-hg@218: public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
franta-hg@218:
franta-hg@218: Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
franta-hg@218:
franta-hg@218: int start = 0;
franta-hg@218:
franta-hg@218: while (m.find(start)) {
franta-hg@218:
franta-hg@218: printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
franta-hg@218:
franta-hg@218: switch (m.group()) {
franta-hg@218: case "\n":
franta-hg@218: out.print(escapeColor, "↲");
franta-hg@218: break;
franta-hg@218: case "\r":
franta-hg@218: out.print(escapeColor, "⏎");
franta-hg@218: break;
franta-hg@218: case "\t":
franta-hg@218: out.print(escapeColor, "↹");
franta-hg@218: break;
franta-hg@218: case NBSP:
franta-hg@218: out.print(escapeColor, "⎵");
franta-hg@218: break;
franta-hg@218: default:
franta-hg@218: throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
franta-hg@218: }
franta-hg@218:
franta-hg@218: start = m.end();
franta-hg@218: }
franta-hg@218:
franta-hg@218: printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
franta-hg@218: }
franta-hg@218:
franta-hg@218: private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
franta-hg@218: if (color == null) {
franta-hg@218: out.print(text);
franta-hg@218: } else {
franta-hg@218: out.print(color, text);
franta-hg@218: }
franta-hg@218: }
franta-hg@1: }