Logging.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl;

  30. import javax.annotation.Nonnull;
  31. import java.util.Collection;
  32. import java.util.Optional;
  33. import it.tidalwave.util.As;
  34. import it.tidalwave.util.Finder;
  35. import it.tidalwave.role.Aggregate;
  36. import it.tidalwave.role.Composite;
  37. import it.tidalwave.role.ui.Displayable;
  38. import lombok.extern.slf4j.Slf4j;
  39. import static java.util.stream.Collectors.toList;

  40. /***********************************************************************************************************************
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  *
  44.  **********************************************************************************************************************/
  45. @Slf4j
  46. public final class Logging
  47.   {
  48.     public static final String INDENT;

  49.     static
  50.       {
  51.         final StringBuilder sb = new StringBuilder();

  52.         for (int i = 0; i < 100; i++)
  53.           {
  54.             sb.append(" ");
  55.           }

  56.         INDENT = sb.toString();
  57.       }

  58.     public static void logObjects (final @Nonnull String prefix, final @Nonnull Collection<?> objects)
  59.       {
  60.         if (!log.isDebugEnabled())
  61.           {
  62.             return;
  63.           }

  64.         if (objects.isEmpty())
  65.           {
  66.             log.debug(">>>>{} <empty>", prefix);
  67.           }
  68.         else
  69.           {
  70.             for (Object object : objects)
  71.               {
  72.                 logObject(prefix, object);
  73.               }
  74.           }
  75.       }

  76.     public static void logObject (final @Nonnull String indent, final @Nonnull Object object)
  77.       {
  78.         if (!log.isDebugEnabled())
  79.           {
  80.             return;
  81.           }

  82.         if (object instanceof Displayable)
  83.           {
  84.             log.debug(">>>>     {}{}: {}", indent, object.getClass().getName(), ((Displayable)object).getDisplayName());
  85.           }
  86.         else
  87.           {
  88.             log.debug(">>>>     {}{}", indent, object);
  89.           }

  90.         if (object instanceof Aggregate)
  91.           {
  92.             final Aggregate<?> aggregate = (Aggregate<?>)object;
  93.             // FIXME: should iterate on all values
  94.             final Optional<?> name = aggregate.getByName("Name");
  95.             final Optional<?> value = aggregate.getByName("Value");
  96.             name.ifPresent(n -> logObject(indent + "    name  ", n));
  97.             value.ifPresent(v -> logObject(indent + "    value ", v));
  98.           }

  99.         if (object instanceof As)
  100.           {
  101.             log.debug(">>>>    {} Composite roles", indent);
  102.             logObjects(indent + "    ",
  103.                        ((As)object).asMany(Object.class).stream().filter(o -> o != object).collect(toList()));
  104.           }

  105.         if (object instanceof Composite)
  106.           {
  107.             final Finder<?> finder = ((Composite<?, ?>)object).findChildren();
  108.             log.debug(">>>>    {} Composite children", indent);
  109.             logObjects(indent + "    ", finder.results().stream().filter(o -> o != object).collect(toList()));
  110.           }
  111.       }
  112.   }