KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > valueholder > ValueHolderProperty


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
20 package org.apache.cayenne.reflect.valueholder;
21
22 import org.apache.cayenne.Persistent;
23 import org.apache.cayenne.ValueHolder;
24 import org.apache.cayenne.reflect.Accessor;
25 import org.apache.cayenne.reflect.BaseToOneProperty;
26 import org.apache.cayenne.reflect.ClassDescriptor;
27 import org.apache.cayenne.reflect.PropertyException;
28 import org.apache.cayenne.util.PersistentObjectHolder;
29
30 /**
31  * Provides access to a property implemented as a {@link ValueHolder} Field. This
32  * implementation hides the fact of the ValueHolder existence. I.e. it never returns it
33  * from 'readPropertyDirectly', returning held value instead.
34  *
35  * @since 3.0
36  * @author Andrus Adamchik
37  */

38 class ValueHolderProperty extends BaseToOneProperty {
39
40     ValueHolderProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor,
41             Accessor accessor, String JavaDoc reverseName) {
42         super(owner, targetDescriptor, accessor, reverseName);
43     }
44
45     /**
46      * Returns true if a property ValueHolder is not initialized or is itself a fault.
47      */

48     public boolean isFault(Object JavaDoc object) {
49         ValueHolder holder = (ValueHolder) accessor.getValue(object);
50         return holder == null || holder.isFault();
51     }
52
53     public void invalidate(Object JavaDoc object) {
54         ValueHolder holder = (ValueHolder) accessor.getValue(object);
55         if (holder != null && !holder.isFault()) {
56             holder.invalidate();
57         }
58     }
59
60     public Object JavaDoc readPropertyDirectly(Object JavaDoc object) throws PropertyException {
61         ValueHolder holder = (ValueHolder) accessor.getValue(object);
62
63         // TODO: Andrus, 2/9/2006 ValueHolder will resolve an object in a call to
64
// 'getValue'; this is inconsistent with 'readPropertyDirectly' contract
65
return (holder != null) ? holder.getValueDirectly() : null;
66     }
67
68     public Object JavaDoc readProperty(Object JavaDoc object) throws PropertyException {
69         return ensureValueHolderSet(object).getValue();
70     }
71
72     public void writePropertyDirectly(Object JavaDoc object, Object JavaDoc oldValue, Object JavaDoc newValue)
73             throws PropertyException {
74
75         ValueHolder holder = (ValueHolder) accessor.getValue(object);
76         if (holder == null) {
77             holder = createValueHolder(object);
78             accessor.setValue(object, holder);
79         }
80
81         holder.setValueDirectly(newValue);
82     }
83
84     public void writeProperty(Object JavaDoc object, Object JavaDoc oldValue, Object JavaDoc newValue)
85             throws PropertyException {
86         ensureValueHolderSet(object).setValueDirectly(newValue);
87     }
88
89     /**
90      * Injects a ValueHolder in the object if it hasn't been done yet.
91      */

92     public void injectValueHolder(Object JavaDoc object) throws PropertyException {
93         ensureValueHolderSet(object);
94     }
95
96     /**
97      * Checks that an object's ValueHolder field described by this property is set,
98      * injecting a ValueHolder if needed.
99      */

100     protected ValueHolder ensureValueHolderSet(Object JavaDoc object) throws PropertyException {
101
102         ValueHolder holder = (ValueHolder) accessor.getValue(object);
103         if (holder == null) {
104             holder = createValueHolder(object);
105             accessor.setValue(object, holder);
106         }
107
108         return holder;
109     }
110
111     /**
112      * Creates a ValueHolder for an object. Default implementation requires that an object
113      * implements Persistent interface.
114      */

115     protected ValueHolder createValueHolder(Object JavaDoc object) throws PropertyException {
116         if (!(object instanceof Persistent)) {
117
118             throw new PropertyException(
119                     "ValueHolders for non-persistent objects are not supported.",
120                     this,
121                     object);
122         }
123
124         return new PersistentObjectHolder((Persistent) object, getName());
125     }
126 }
127
Popular Tags