Debian Codegraph @

lang/java/src/com/sleepycat/persist/model/PersistentProxy.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.model;
9
10 import com.sleepycat.persist.evolve.Converter; // for javadoc
11 import com.sleepycat.persist.raw.RawStore; // for javadoc
12
13 /**
14 * Implemented by a proxy class to represent the persistent state of a
15 * (non-persistent) proxied class. Normally classes that are outside the scope
16 * of the developer's control must be proxied since they cannot be annotated,
17 * and because it is desirable to insulate the stored format from changes to
18 * the instance fields of the proxied class. This is useful for classes in the
19 * standard Java libraries, for example.
20 *
21 * <p>{@code PersistentProxy} objects are not required to be thread-safe. A
22 * single thread will create and call the methods of a given {@code
23 * PersistentProxy} object.</p>
24 *
25 * <p>There are three requirements for a proxy class:</p>
26 * <ol>
27 * <li>It must implement the <code>PersistentProxy</code> interface.</li>
28 * <li>It must be specified as a persistent proxy class in the entity model.
29 * When using the {@link AnnotationModel}, a proxy class is indicated by the
30 * {@link Persistent} annotation with the {@link Persistent#proxyFor}
31 * property.</li>
32 * <li>It must be explicitly registered by calling {@link
33 * EntityModel#registerClass} before opening the store.</li>
34 * </ol>
35 *
36 * <p>In order to serialize an instance of the proxied class before it is
37 * stored, an instance of the proxy class is created. The proxied instance is
38 * then passed to the proxy's {@link #initializeProxy initializeProxy} method.
39 * When this method returns, the proxy instance contains the state of the
40 * proxied instance. The proxy instance is then serialized and stored in the
41 * same way as for any persistent object.</p>
42 *
43 * <p>When an instance of the proxy object is deserialized after it is
44 * retrieved from storage, its {@link #convertProxy} method is called. The
45 * instance of the proxied class returned by this method is then returned as a
46 * field in the persistent instance.</p>
47 *
48 * <p>For example:</p>
49 * <pre class="code">
50 * import java.util.Locale;
51 *
52 * {@literal @Persistent(proxyFor=Locale.class)}
53 * class LocaleProxy implements {@literal PersistentProxy<Locale>} {
54 *
55 * String language;
56 * String country;
57 * String variant;
58 *
59 * private LocaleProxy() {}
60 *
61 * public void initializeProxy(Locale object) {
62 * language = object.getLanguage();
63 * country = object.getCountry();
64 * variant = object.getVariant();
65 * }
66 *
67 * public Locale convertProxy() {
68 * return new Locale(language, country, variant);
69 * }
70 * }</pre>
71 *
72 * <p>The above definition allows the {@code Locale} class to be used in any
73 * persistent class, for example:</p>
74 * <pre class="code">
75 * {@literal @Persistent}
76 * class LocalizedText {
77 * String text;
78 * Locale locale;
79 * }</pre>
80 *
81 * <p>A proxied class may not be used as a superclass for a persistent class or
82 * entity class. For example, the following is not allowed.</p>
83 * <pre class="code">
84 * {@literal @Persistent}
85 * class LocalizedText extends Locale { // NOT ALLOWED
86 * String text;
87 * }</pre>
88 *
89 * <p>A proxy for proxied class P does not handle instances of subclasses of P.
90 * To proxy a subclass of P, a separate proxy class is needed.</p>
91 *
92 * <p>Several {@link <a href="Entity.html#proxyTypes">built in proxy types</a>}
93 * are used implicitly. An application defined proxy will be used instead of a
94 * built-in proxy, if both exist for the same proxied class.</p>
95 *
96 * <p>With respect to class evolution, a proxy instance is no different than
97 * any other persistent instance. When using a {@link RawStore} or {@link
98 * Converter}, only the raw data of the proxy instance will be visible. Raw
99 * data for the proxied instance never exists.</p>
100 *
101 * <p>Currently a proxied object may not contain a reference to itself. For
102 * simple proxied objects such as the Locale class shown above, this naturally
103 * won't occur. But for proxied objects that are containers -- the built-in
104 * Collection and Map classes for example -- this can occur if the container is
105 * added as an element of itself. This should be avoided. If an attempt to
106 * store such an object is made, an {@code IllegalArgumentException} will be
107 * thrown.</p>
108 *
109 * <p>Note that a proxy class may not be a subclass of an entity class.</p>
110 *
111 * @author Mark Hayes
112 */
113 public interface PersistentProxy<T> {
114
115 /**
116 * Copies the state of a given proxied class instance to this proxy
117 * instance.
118 */
119 void initializeProxy(T object);
120
121 /**
122 * Returns a new proxied class instance to which the state of this proxy
123 * instance has been copied.
124 */
125 T convertProxy();
126 }