Skip to contentMethod: shortNames(Iterable)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.role.spi.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.util.Arrays;
32: import it.tidalwave.role.Identifiable;
33: import static java.util.stream.Collectors.*;
34:
35: /***********************************************************************************************************************
36: *
37: * @author Fabrizio Giudici
38: *
39: **********************************************************************************************************************/
40: public class LogUtil
41: {
42: @Nonnull
43: public static String shortName (@Nonnull final Class<?> clazz)
44: {
45: return shortName(clazz, false);
46: }
47:
48: @Nonnull
49: public static String shortName (@Nonnull final Class<?> clazz, final boolean expandInterfaces)
50: {
51: String className = clazz.getName();
52: String prefix = "";
53:
54: if (className.contains("EnhancerByMockito"))
55: {
56: prefix = "mock-of-";
57: className = className.replaceAll("\\$\\$EnhancerByMockito.*", "");
58: }
59:
60: final String[] parts = className.split("\\.");
61: final StringBuilder s = new StringBuilder();
62:
63: for (int i = 0; i < parts.length; i++)
64: {
65: s.append((i < parts.length - 1) ? parts[i].charAt(0) + "." : parts[i]);
66: }
67:
68: if (expandInterfaces)
69: {
70: final Class<?>[] interfaces = clazz.getInterfaces();
71:
72: if (interfaces.length > 0)
73: {
74: s.append(Arrays.stream(interfaces)
75: .filter(i -> !i.getPackage().getName().startsWith("java"))
76: .map(LogUtil::shortName).collect(joining(", ", "{", "}")));
77: }
78: }
79:
80: return prefix + s;
81: }
82:
83: @Nonnull
84: public static String shortNames (@Nonnull final Iterable<Class<?>> classes)
85: {
86: final StringBuilder result = new StringBuilder();
87: String separator = "";
88:
89:• for (final Class<?> clazz : classes)
90: {
91: result.append(separator).append(shortName(clazz));
92: separator = ", ";
93: }
94:
95: return "[" + result + "]";
96: }
97:
98: @Nonnull
99: public static String shortId (@Nullable final Object object)
100: {
101: if (object == null)
102: {
103: return "null";
104: }
105:
106: final StringBuilder s = new StringBuilder();
107: s.append(String.format("%s@%x", shortName(object.getClass()), System.identityHashCode(object)));
108:
109: if (object instanceof Identifiable)
110: {
111: s.append("/").append(((Identifiable)object).getId());
112: }
113:
114: return s.toString();
115: }
116:
117: @Nonnull
118: public static String shortIds (@Nonnull final Iterable<?> objects)
119: {
120: final StringBuilder result = new StringBuilder();
121: String separator = "";
122:
123: for (final Object object : objects)
124: {
125: result.append(separator).append(shortId(object));
126: separator = ", ";
127: }
128:
129: return "[" + result + "]";
130: }
131:
132: @Nonnull
133: public static String shortIds (@Nonnull final Object... objects)
134: {
135: return shortIds(Arrays.asList(objects));
136: }
137: }