AsExtensions.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 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 the License.
  12.  * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

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

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

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

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

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

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

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

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