View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.impl.model.jdo;
18  
19  import org.apache.jdo.impl.model.jdo.util.TypeSupport;
20  import org.apache.jdo.model.java.JavaType;
21  import org.apache.jdo.model.jdo.JDOArray;
22  import org.apache.jdo.model.jdo.JDOField;
23  
24  /***
25   * An instance of this class represents the JDO relationship metadata 
26   * of a array relationship field. This dynamic implementation only
27   * stores property values explicitly set by setter method. 
28   *
29   * @author Michael Bouschen
30   * @since 1.1
31   * @version 2.0
32   */
33  public class JDOArrayImplDynamic extends JDORelationshipImpl 
34      implements JDOArray {
35      
36      /*** Property embeddedElement. */
37      protected Boolean embeddedElement;
38  
39      /***
40       * Determines whether the values of the elements should be stored 
41       * if possible as part of the instance instead of as their own instances 
42       * in the datastore.
43       * @return <code>true</code> if the elements should be stored as part of 
44       * the instance; <code>false</code> otherwise
45       */
46      public boolean isEmbeddedElement() {
47          if (embeddedElement != null) {
48              // return embeddedElement, if explicitly set by the setter
49              return embeddedElement.booleanValue();
50          }
51          
52          // not set => calculate
53          JavaType elementType = getElementType();
54          return (elementType != null) ? 
55              TypeSupport.isEmbeddedElementType(elementType) : false;
56      }
57      
58      /***
59       * Set whether the values of the elements should be stored 
60       * if possible as part of the instance instead of as their own instances 
61       * in the datastore.
62       * @param embeddedElement flag indicating whether the elements should be 
63       * stored as part of the instance
64       */
65      public void setEmbeddedElement(boolean embeddedElement) {
66          this.embeddedElement = (embeddedElement ? Boolean.TRUE : Boolean.FALSE);
67      }
68  
69      /*** 
70       * Get the type representation of the array component type. 
71       * @return the array component type
72       */
73      public JavaType getElementType() {
74          JDOField jdoField = getDeclaringField();
75          JavaType fieldType = jdoField.getType();
76          return (fieldType != null) ? fieldType.getArrayComponentType() : null;
77      }
78  
79      /***
80       * Determines whether this JDORelationship represents an array
81       * relationship or not. A return of <code>true</code> means this
82       * JDORelationship is a JDOArray instance.
83       * @return <code>true</code> if this JDORelationship represents an 
84       * array relationship; <code>false</code> otherwise.
85       */
86      public boolean isJDOArray() {
87          return true;
88      }
89  
90      //========= Internal helper methods ==========
91  
92      /*** 
93       * Get the type representation of the relationship. This will be 
94       * the JavaType for references, the element type for collections
95       * and arrays, and the value type for maps.
96       * @return the relationship type
97       */
98      public JavaType getRelatedJavaType() {
99          return getElementType();
100     }
101 
102 }