Skip to content

Method: adapter(Object)

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