Skip to contentMethod: AsExtensions()
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 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
52: public static <T> Optional<T> maybeAs (@Nonnull final Object datum, @Nonnull final Class<? extends T> type)
53: {
54: return adapter(datum).maybeAs(type);
55: }
56:
57: @Nonnull
58: public static <T> Collection<T> asMany (@Nonnull final Object datum, @Nonnull final Class<? extends T> type)
59: {
60: return adapter(datum).asMany(type);
61: }
62:
63: @Nonnull
64: public static <T> T as (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
65: {
66: return adapter(datum).as(type);
67: }
68:
69: @Nonnull
70: public static <T> Optional<T> maybeAs (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
71: {
72: return adapter(datum).maybeAs(type);
73: }
74:
75: @Nonnull
76: public static <T> Collection<T> asMany (@Nonnull final Object datum, @Nonnull final As.Type<? extends T> type)
77: {
78: return adapter(datum).asMany(type);
79: }
80:
81: @Nonnull
82: private static As adapter (@Nonnull final Object datum)
83: {
84: return As.forObject(datum);
85: }
86: }