Skip to content

Method: JOPALazyUtils()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.utils;
19:
20: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
21: import cz.cvut.kbss.jopa.proxy.lazy.gen.LazyLoadingEntityProxy;
22:
23: import java.util.Objects;
24:
25: /**
26: * Utilities related to lazy loading.
27: */
28: public class JOPALazyUtils {
29:
30: private JOPALazyUtils() {
31: throw new AssertionError();
32: }
33:
34: /**
35: * Checks if this specified object is a lazy loading proxy (instance of {@link LazyLoadingProxy}).
36: *
37: * @param object Object to investigate
38: * @return {@code true} if argument is lazy loading proxy, {@code false} otherwise
39: */
40: public static boolean isLazyLoadingProxy(Object object) {
41: return object instanceof LazyLoadingProxy<?>
42: }
43:
44: /**
45: * Triggers loading on the specified lazy loading proxy.
46: * <p>
47: * If the specified object is not a {@link cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy} or is a proxy that is
48: * already loaded, nothing happens.
49: *
50: * @param proxy Proxy to load
51: */
52: public static void load(Object proxy) {
53: if (proxy instanceof LazyLoadingProxy<?> p) {
54: p.triggerLazyLoading();
55: }
56: }
57:
58: /**
59: * Checks whether the specified object has been loaded or is still a lazy loading proxy.
60: *
61: * @param proxy Proxy to check
62: * @return {@code true} if the object is a {@code LazyLoadingProxy} and is not loaded, {@code false} otherwise
63: */
64: public static boolean isLoaded(Object proxy) {
65: return !(proxy instanceof LazyLoadingProxy<?> lazyLoadingProxy) || (lazyLoadingProxy.isLoaded());
66: }
67:
68: /**
69: * Gets the class represented by the specified lazy loading proxy.
70: * <p>
71: * If the specified object proxies an entity, the corresponding entity class is returned. If the object proxies a
72: * collection, the corresponding interface is returned. If the specified object is not a
73: * {@link cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy}, its runtime class is returned.
74: *
75: * @param proxy Proxy whose class to get
76: * @return Runtime class unwrapped from a lazy loading proxy
77: */
78: public static Class<?> getClass(Object proxy) {
79: Objects.requireNonNull(proxy);
80: return (proxy instanceof LazyLoadingEntityProxy<?> entityProxy) ? entityProxy.getProxiedClass() : proxy.getClass();
81: }
82: }