AsExtensions.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.util;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collection;
  30. import java.util.Optional;

  31. /***********************************************************************************************************************
  32.  *
  33.  * An extension to be used with Lombok in order to provide "as" support to classes that don't implement the {@link As}
  34.  * interface. The typical usage is to retrofit legacy code.
  35.  *
  36.  * FIXME: this class doesn't cache - every as*() call instantiates new objects.
  37.  *
  38.  * @author  Fabrizio Giudici
  39.  *
  40.  **********************************************************************************************************************/
  41. public class AsExtensions
  42.   {
  43.     @Nonnull
  44.     public static <T> T as (@Nonnull final Object datum, @Nonnull final Class<T> roleType)
  45.       {
  46.         return adapter(datum).as(roleType);
  47.       }

  48.     @Nonnull
  49.     public static <T> Optional<T> maybeAs (@Nonnull final Object datum, @Nonnull final Class<? extends T> type)
  50.       {
  51.         return adapter(datum).maybeAs(type);
  52.       }

  53.     @Nonnull
  54.     public static <T> Collection<T> asMany (@Nonnull final Object datum, @Nonnull final Class<? extends T> type)
  55.       {
  56.         return adapter(datum).asMany(type);
  57.       }

  58.     @Nonnull
  59.     public static <T> T as (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
  60.       {
  61.         return adapter(datum).as(type);
  62.       }

  63.     @Nonnull
  64.     public static <T> Optional<T> maybeAs (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
  65.       {
  66.         return adapter(datum).maybeAs(type);
  67.       }

  68.     @Nonnull
  69.     public static <T> Collection<T> asMany (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
  70.       {
  71.         return adapter(datum).asMany(type);
  72.       }

  73.     @Nonnull
  74.     private static As adapter (@Nonnull final Object datum)
  75.       {
  76.         return As.forObject(datum);
  77.       }
  78.   }