1 /*
2 * $Id: Definition.java 832840 2009-11-04 18:44:25Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.tiles;
22
23 import static org.apache.tiles.CompareUtil.*;
24
25 import java.util.Map;
26
27 /**
28 * A definition, i.e. a template with (completely or not) filled attributes.
29 * Attributes of a template can be defined with the help of this class.<br>
30 * It can be used as a data transfer object used for registering new
31 * definitions with the Container.
32 *
33 * @since Tiles 2.0
34 * @version $Rev: 832840 $ $Date: 2009-11-05 05:44:25 +1100 (Thu, 05 Nov 2009) $
35 */
36 public class Definition extends BasicAttributeContext {
37 /**
38 * Extends attribute value.
39 */
40 protected String inherit;
41 /**
42 * Definition name.
43 */
44 protected String name = null;
45
46 /**
47 * Constructor.
48 */
49 public Definition() {
50 }
51
52 /**
53 * Copy Constructor.
54 * Create a new definition initialized with parent definition.
55 * Do a shallow copy : attributes are shared between copies, but not the Map
56 * containing attributes.
57 *
58 * @param definition The definition to copy.
59 */
60 public Definition(Definition definition) {
61 super(definition);
62 this.name = definition.name;
63 this.inherit = definition.inherit;
64 }
65
66 /**
67 * Constructor.
68 * @param name The name of the definition.
69 * @param templateAttribute The template attribute of the definition.
70 * @param attributes The attribute map of the definition.
71 *
72 * @since 2.1.2
73 */
74 public Definition(String name, Attribute templateAttribute,
75 Map<String, Attribute> attributes) {
76 super(attributes);
77 this.name = name;
78 this.templateAttribute = templateAttribute;
79 }
80
81 /**
82 * Access method for the name property.
83 *
84 * @return the current value of the name property
85 */
86 public String getName() {
87 return name;
88 }
89
90 /**
91 * Sets the value of the name property.
92 *
93 * @param aName the new value of the name property
94 */
95 public void setName(String aName) {
96 name = aName;
97 }
98
99 /**
100 * Set extends.
101 *
102 * @param name Name of the extended definition.
103 */
104 public void setExtends(String name) {
105 inherit = name;
106 }
107
108 /**
109 * Get extends.
110 *
111 * @return Name of the extended definition.
112 */
113 public String getExtends() {
114 return inherit;
115 }
116
117
118
119 /** {@inheritDoc} */
120 @Override
121 public boolean equals(Object obj) {
122 Definition def = (Definition) obj;
123 return nullSafeEquals(name, def.name)
124 && nullSafeEquals(inherit, def.inherit) && super.equals(def);
125 }
126
127 /** {@inheritDoc} */
128 @Override
129 public int hashCode() {
130 return nullSafeHashCode(name) + nullSafeHashCode(inherit)
131 + super.hashCode();
132 }
133
134 /**
135 * Get extends flag.
136 *
137 * @return <code>true</code> if this definition extends another.
138 */
139 public boolean isExtending() {
140 return inherit != null;
141 }
142
143 /**
144 * Returns a description of the attributes.
145 *
146 * @return A string representation of the content of this definition.
147 */
148 @Override
149 public String toString() {
150 return "{name="
151 + name
152 + ", template="
153 + (templateAttribute != null ? templateAttribute.getValue() : "<null>")
154 + ", role="
155 + (templateAttribute != null ? templateAttribute.getRoles() : "<null>")
156 + ", preparerInstance="
157 + preparer
158 + ", attributes="
159 + attributes
160 + "}";
161 }
162 }