LogUtil.java

  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. import javax.annotation.Nonnull;
  29. import javax.annotation.Nullable;
  30. import java.util.Arrays;
  31. import it.tidalwave.role.Identifiable;
  32. import static java.util.stream.Collectors.*;

  33. /***********************************************************************************************************************
  34.  *
  35.  * @author  Fabrizio Giudici
  36.  *
  37.  **********************************************************************************************************************/
  38. public class LogUtil
  39.   {
  40.     @Nonnull
  41.     public static String shortName (@Nonnull final Class<?> clazz)
  42.       {
  43.         return shortName(clazz, false);
  44.       }

  45.     @Nonnull
  46.     public static String shortName (@Nonnull final Class<?> clazz, final boolean expandInterfaces)
  47.         {
  48.         String className = clazz.getName();
  49.         String prefix = "";

  50.         if (className.contains("EnhancerByMockito"))
  51.           {
  52.             prefix = "mock-of-";
  53.             className = className.replaceAll("\\$\\$EnhancerByMockito.*", "");
  54.           }

  55.         final String[] parts = className.split("\\.");
  56.         final StringBuilder s = new StringBuilder();

  57.         for (int i = 0; i < parts.length; i++)
  58.           {
  59.             s.append((i < parts.length - 1) ? parts[i].charAt(0) + "." : parts[i]);
  60.           }

  61.         if (expandInterfaces)
  62.           {
  63.             final Class<?>[] interfaces = clazz.getInterfaces();

  64.             if (interfaces.length > 0)
  65.               {
  66.                 s.append(Arrays.stream(interfaces)
  67.                                .filter(i -> !i.getPackage().getName().startsWith("java"))
  68.                                .map(LogUtil::shortName).collect(joining(", ", "{", "}")));
  69.               }
  70.           }

  71.         return prefix + s;
  72.       }

  73.     @Nonnull
  74.     public static String shortNames (@Nonnull final Iterable<Class<?>> classes)
  75.       {
  76.         final StringBuilder result = new StringBuilder();
  77.         String separator = "";

  78.         for (final Class<?> clazz : classes)
  79.           {
  80.             result.append(separator).append(shortName(clazz));
  81.             separator = ", ";
  82.           }

  83.         return "[" + result + "]";
  84.       }

  85.     @Nonnull
  86.     public static String shortId (@Nullable final Object object)
  87.       {
  88.         if (object == null)
  89.           {
  90.             return "null";
  91.           }

  92.         final StringBuilder s = new StringBuilder();
  93.         s.append(String.format("%s@%x", shortName(object.getClass()), System.identityHashCode(object)));

  94.         if (object instanceof Identifiable)
  95.           {
  96.             s.append("/").append(((Identifiable)object).getId());
  97.           }

  98.         return s.toString();
  99.       }

  100.     @Nonnull
  101.     public static String shortIds (@Nonnull final Iterable<?> objects)
  102.       {
  103.         final StringBuilder result = new StringBuilder();
  104.         String separator = "";

  105.         for (final Object object : objects)
  106.           {
  107.             result.append(separator).append(shortId(object));
  108.             separator = ", ";
  109.           }

  110.         return "[" + result + "]";
  111.       }

  112.     @Nonnull
  113.     public static String shortIds (@Nonnull final Object... objects)
  114.       {
  115.         return shortIds(Arrays.asList(objects));
  116.       }
  117.   }