KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > LazyClassDescriptorDecorator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.reflect;
20
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.cayenne.map.ObjEntity;
24
25 /**
26  * A ClassDescriptor wrapper that compiles decoarated descriptor lazily on first access.
27  *
28  * @since 3.0
29  * @author Andrus Adamchik
30  */

31 public class LazyClassDescriptorDecorator implements ClassDescriptor {
32
33     protected ClassDescriptor descriptor;
34     protected ClassDescriptorMap descriptorMap;
35     protected String JavaDoc entityName;
36
37     public LazyClassDescriptorDecorator(ClassDescriptorMap descriptorMap,
38             String JavaDoc entityName) {
39         this.descriptorMap = descriptorMap;
40         this.entityName = entityName;
41     }
42
43     /**
44      * Checks whether decorated descriptor is initialized, and if not, creates it using
45      * parent {@link ClassDescriptorMap}.
46      */

47     protected void checkDescriptorInitialized() {
48         if (descriptor == null) {
49             descriptor = descriptorMap.createDescriptor(entityName);
50         }
51     }
52
53     /**
54      * Returns underlying descriptor used to delegate all processing, resolving it if
55      * needed.
56      */

57     public ClassDescriptor getDescriptor() {
58         checkDescriptorInitialized();
59         return descriptor;
60     }
61
62     public Object JavaDoc createObject() {
63         checkDescriptorInitialized();
64         return descriptor.createObject();
65     }
66
67     public Property getDeclaredProperty(String JavaDoc propertyName) {
68         checkDescriptorInitialized();
69         return descriptor.getDeclaredProperty(propertyName);
70     }
71
72     public ObjEntity getEntity() {
73         checkDescriptorInitialized();
74         return descriptor.getEntity();
75     }
76
77     public Class JavaDoc getObjectClass() {
78         checkDescriptorInitialized();
79         return descriptor.getObjectClass();
80     }
81
82     /**
83      * @deprecated since 3.0. Use {@link #visitProperties(PropertyVisitor)} method
84      * instead.
85      */

86     public Iterator JavaDoc getProperties() {
87         checkDescriptorInitialized();
88         return descriptor.getProperties();
89     }
90
91     public Iterator JavaDoc getIdProperties() {
92         checkDescriptorInitialized();
93         return descriptor.getIdProperties();
94     }
95
96     public Property getProperty(String JavaDoc propertyName) {
97         checkDescriptorInitialized();
98         return descriptor.getProperty(propertyName);
99     }
100
101     public ClassDescriptor getSubclassDescriptor(Class JavaDoc objectClass) {
102         checkDescriptorInitialized();
103         return descriptor.getSubclassDescriptor(objectClass);
104     }
105
106     public ClassDescriptor getSuperclassDescriptor() {
107         checkDescriptorInitialized();
108         return descriptor.getSuperclassDescriptor();
109     }
110
111     public void injectValueHolders(Object JavaDoc object) throws PropertyException {
112         checkDescriptorInitialized();
113         descriptor.injectValueHolders(object);
114     }
115
116     public boolean isFault(Object JavaDoc object) {
117         checkDescriptorInitialized();
118         return descriptor.isFault(object);
119     }
120
121     public void shallowMerge(Object JavaDoc from, Object JavaDoc to) throws PropertyException {
122         checkDescriptorInitialized();
123         descriptor.shallowMerge(from, to);
124     }
125
126     public boolean visitDeclaredProperties(PropertyVisitor visitor) {
127         checkDescriptorInitialized();
128         return descriptor.visitDeclaredProperties(visitor);
129     }
130
131     public boolean visitProperties(PropertyVisitor visitor) {
132         checkDescriptorInitialized();
133         return descriptor.visitProperties(visitor);
134     }
135
136     public boolean visitAllProperties(PropertyVisitor visitor) {
137         checkDescriptorInitialized();
138         return descriptor.visitAllProperties(visitor);
139     }
140 }
141
Popular Tags