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
28 import javax.annotation.Nonnull;
29 import java.util.Collection;
30 import java.util.Optional;
31
32 /***************************************************************************************************************************************************************
33 *
34 * An extension to be used with Lombok in order to provide "as" support to classes that don't implement the {@link As}
35 * interface. The typical usage is to retrofit legacy code.
36 *
37 * FIXME: this class doesn't cache - every as*() call instantiates new objects.
38 *
39 * @author Fabrizio Giudici
40 *
41 **************************************************************************************************************************************************************/
42 public class AsExtensions
43 {
44 @Nonnull
45 public static <T> T as (@Nonnull final Object datum, @Nonnull final Class<T> roleType)
46 {
47 return adapter(datum).as(roleType);
48 }
49
50 @Nonnull
51 public static <T> Optional<T> maybeAs (@Nonnull final Object datum, @Nonnull final Class<? extends T> type)
52 {
53 return adapter(datum).maybeAs(type);
54 }
55
56 @Nonnull
57 public static <T> Collection<T> asMany (@Nonnull final Object datum, @Nonnull final Class<? extends T> type)
58 {
59 return adapter(datum).asMany(type);
60 }
61
62 @Nonnull
63 public static <T> T as (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
64 {
65 return adapter(datum).as(type);
66 }
67
68 @Nonnull
69 public static <T> Optional<T> maybeAs (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
70 {
71 return adapter(datum).maybeAs(type);
72 }
73
74 @Nonnull
75 public static <T> Collection<T> asMany (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
76 {
77 return adapter(datum).asMany(type);
78 }
79
80 @Nonnull
81 private static As adapter (@Nonnull final Object datum)
82 {
83 return As.forObject(datum);
84 }
85 }