View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.web.jcr.rest.client.domain;
25  
26  import java.util.Map;
27  import java.util.Set;
28  import javax.jcr.version.OnParentVersionAction;
29  import net.jcip.annotations.Immutable;
30  
31  /**
32   * An immutable representation of a JCR PropertyDefinition.
33   */
34  @Immutable
35  public class ChildNodeDefinition extends ItemDefinition implements javax.jcr.nodetype.NodeDefinition {
36  
37      private final Id id;
38      private final String defaultPrimaryTypeName;
39  
40      public ChildNodeDefinition( String declaringNodeTypeName,
41                                  String name,
42                                  Set<String> requiredTypes,
43                                  boolean isAutoCreated,
44                                  boolean isMandatory,
45                                  boolean isProtected,
46                                  boolean allowsSameNameSiblings,
47                                  int onParentVersion,
48                                  String defaultPrimaryTypeName,
49                                  Map<String, NodeType> nodeTypes ) {
50          super(declaringNodeTypeName, isAutoCreated, isMandatory, isProtected, onParentVersion, nodeTypes);
51          this.id = new Id(name, allowsSameNameSiblings, requiredTypes);
52          this.defaultPrimaryTypeName = defaultPrimaryTypeName;
53      }
54  
55      /**
56       * @return id
57       */
58      protected Id id() {
59          return id;
60      }
61  
62      /**
63       * {@inheritDoc}
64       * 
65       * @see javax.jcr.nodetype.ItemDefinition#getName()
66       */
67      @Override
68      public String getName() {
69          return id.name;
70      }
71  
72      /**
73       * {@inheritDoc}
74       * 
75       * @see javax.jcr.nodetype.NodeDefinition#allowsSameNameSiblings()
76       */
77      @Override
78      public boolean allowsSameNameSiblings() {
79          return id.isMultiple;
80      }
81  
82      /**
83       * {@inheritDoc}
84       * 
85       * @see javax.jcr.nodetype.NodeDefinition#getRequiredPrimaryTypeNames()
86       */
87      @Override
88      public String[] getRequiredPrimaryTypeNames() {
89          return toArray(id.requiredTypes);
90      }
91  
92      /**
93       * {@inheritDoc}
94       * 
95       * @see javax.jcr.nodetype.NodeDefinition#getDefaultPrimaryType()
96       */
97      @Override
98      public NodeType getDefaultPrimaryType() {
99          return nodeType(getDefaultPrimaryTypeName());
100     }
101 
102     /**
103      * {@inheritDoc}
104      * 
105      * @see javax.jcr.nodetype.NodeDefinition#getDefaultPrimaryTypeName()
106      */
107     @Override
108     public String getDefaultPrimaryTypeName() {
109         return defaultPrimaryTypeName;
110     }
111 
112     /**
113      * {@inheritDoc}
114      * 
115      * @see javax.jcr.nodetype.NodeDefinition#getRequiredPrimaryTypes()
116      */
117     @Override
118     public javax.jcr.nodetype.NodeType[] getRequiredPrimaryTypes() {
119         return nodeTypes(id.requiredTypes, nodeTypes());
120     }
121 
122     /**
123      * {@inheritDoc}
124      * 
125      * @see java.lang.Object#hashCode()
126      */
127     @Override
128     public int hashCode() {
129         return id.hashCode();
130     }
131 
132     /**
133      * {@inheritDoc}
134      * 
135      * @see java.lang.Object#equals(java.lang.Object)
136      */
137     @Override
138     public boolean equals( Object obj ) {
139         if (obj == this) return true;
140         if (obj instanceof ChildNodeDefinition) {
141             ChildNodeDefinition that = (ChildNodeDefinition)obj;
142             return this.id.equals(that.id);
143         }
144         return false;
145     }
146 
147     /**
148      * {@inheritDoc}
149      * 
150      * @see java.lang.Object#toString()
151      */
152     @Override
153     public String toString() {
154         StringBuilder sb = new StringBuilder();
155         sb.append(" + ");
156         sb.append(id.name);
157         if (getRequiredPrimaryTypeNames().length != 0) {
158             sb.append(" (");
159             boolean first = true;
160             for (String typeName : getRequiredPrimaryTypeNames()) {
161                 if (typeName == null) continue;
162                 if (first) first = false;
163                 else sb.append(',');
164                 sb.append(typeName);
165             }
166             sb.append(')');
167         }
168         if (getDefaultPrimaryTypeName() != null) {
169             sb.append(" = ").append(getDefaultPrimaryTypeName());
170         }
171         if (isAutoCreated()) sb.append(" autocreated");
172         if (isMandatory()) sb.append(" mandatory");
173         if (allowsSameNameSiblings()) sb.append(" sns");
174         if (isProtected()) sb.append(" protected");
175         sb.append(' ').append(OnParentVersionAction.nameFromValue(getOnParentVersion()));
176         return sb.toString();
177     }
178 
179     protected static class Id {
180         protected final String name;
181         protected final boolean isMultiple;
182         protected final Set<String> requiredTypes;
183 
184         protected Id( String name,
185                       boolean isMultiple,
186                       Set<String> requiredTypes ) {
187             this.name = name;
188             this.isMultiple = isMultiple;
189             this.requiredTypes = requiredTypes;
190             assert this.name != null;
191             assert this.requiredTypes != null;
192         }
193 
194         /**
195          * {@inheritDoc}
196          * 
197          * @see java.lang.Object#hashCode()
198          */
199         @Override
200         public int hashCode() {
201             return name.hashCode();
202         }
203 
204         /**
205          * {@inheritDoc}
206          * 
207          * @see java.lang.Object#equals(java.lang.Object)
208          */
209         @Override
210         public boolean equals( Object obj ) {
211             if (obj == this) return true;
212             if (obj instanceof Id) {
213                 Id that = (Id)obj;
214                 if (this.isMultiple != that.isMultiple) return false;
215                 if (!this.requiredTypes.equals(that.requiredTypes)) return false;
216                 if (!this.name.equals(that.name)) return false;
217                 return true;
218             }
219             return false;
220         }
221 
222         /**
223          * {@inheritDoc}
224          * 
225          * @see java.lang.Object#toString()
226          */
227         @Override
228         public String toString() {
229             StringBuilder sb = new StringBuilder(name);
230             sb.append('(');
231             boolean first = true;
232             for (String requiredType : requiredTypes) {
233                 if (first) first = false;
234                 else sb.append(',');
235                 sb.append(requiredType);
236             }
237             sb.append(')');
238             sb.append(isMultiple ? '*' : '1');
239             return sb.toString();
240         }
241     }
242 
243 }