1 // SECTION-START[License Header]
2 // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3 /*
4 * Java Object Management and Configuration
5 * Copyright (C) Christian Schulte <cs@schulte.it>, 2011-313
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * o Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * o Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $JOMC: RuntimeProperties.java 5061 2015-05-31 13:20:40Z schulte $
32 *
33 */
34 // </editor-fold>
35 // SECTION-END
36 package org.jomc.ri.model;
37
38 import java.util.Map;
39 import javax.xml.bind.annotation.XmlTransient;
40 import org.jomc.model.Properties;
41 import org.jomc.model.Property;
42 import org.jomc.model.PropertyReference;
43 import static org.jomc.ri.model.RuntimeModelObjects.createMap;
44
45 // SECTION-START[Documentation]
46 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
47 /**
48 * Runtime {@code Properties}.
49 *
50 * <dl>
51 * <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeProperties</dd>
52 * <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeProperties</dd>
53 * <dt><b>Specifications:</b></dt>
54 * <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
55 * <dt><b>Abstract:</b></dt><dd>No</dd>
56 * <dt><b>Final:</b></dt><dd>No</dd>
57 * <dt><b>Stateless:</b></dt><dd>No</dd>
58 * </dl>
59 *
60 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
61 * @version 1.2
62 */
63 // </editor-fold>
64 // SECTION-END
65 // SECTION-START[Annotations]
66 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
67 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
68 // </editor-fold>
69 // SECTION-END
70 public class RuntimeProperties extends Properties implements RuntimeModelObject
71 {
72 // SECTION-START[RuntimeProperties]
73
74 /**
75 * Cache map.
76 */
77 @XmlTransient
78 private transient final Map<String, Property> propertiesByNameCache = createMap();
79
80 /**
81 * Cache map.
82 */
83 @XmlTransient
84 private transient final Map<String, PropertyReference> referencesByNameCache = createMap();
85
86 /**
87 * Creates a new {@code RuntimeProperties} instance by deeply copying a given {@code Properties} instance.
88 *
89 * @param properties The instance to copy.
90 *
91 * @throws NullPointerException if {@code properties} is {@code null}.
92 */
93 public RuntimeProperties( final Properties properties )
94 {
95 super( properties );
96
97 if ( this.getAuthors() != null )
98 {
99 this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
100 }
101 if ( this.getDocumentation() != null )
102 {
103 this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
104 }
105
106 this.copyProperties();
107 this.copyReferences();
108 }
109
110 /**
111 * Gets a property for a given name from the list of properties.
112 * <p>
113 * This method queries an internal cache for a result object to return for the given argument values. If no
114 * cached result object is available, this method queries the super-class for a result object to return and caches
115 * the outcome of that query for use on successive calls.
116 * </p>
117 * <p>
118 * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
119 * state of the instance, should the state of the instance change.
120 * </p>
121 *
122 * @param name The name of the property to return.
123 *
124 * @return The first matching property or {@code null}, if no such property is found.
125 *
126 * @throws NullPointerException if {@code name} is {@code null}.
127 *
128 * @see #getProperty()
129 * @see Property#getName()
130 * @see #clear()
131 */
132 @Override
133 public Property getProperty( final String name )
134 {
135 if ( name == null )
136 {
137 throw new NullPointerException( "name" );
138 }
139
140 synchronized ( this.propertiesByNameCache )
141 {
142 Property p = this.propertiesByNameCache.get( name );
143
144 if ( p == null && !this.propertiesByNameCache.containsKey( name ) )
145 {
146 p = super.getProperty( name );
147 this.propertiesByNameCache.put( name, p );
148 }
149
150 return p;
151 }
152 }
153
154 /**
155 * Gets a property reference for a given name from the list of references.
156 * <p>
157 * This method queries an internal cache for a result object to return for the given argument values. If no
158 * cached result object is available, this method queries the super-class for a result object to return and caches
159 * the outcome of that query for use on successive calls.
160 * </p>
161 * <p>
162 * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
163 * state of the instance, should the state of the instance change.
164 * </p>
165 *
166 * @param name The name of the property reference to return.
167 *
168 * @return The first matching property reference or {@code null}, if no such reference is found.
169 *
170 * @throws NullPointerException if {@code name} is {@code null}.
171 *
172 * @see #getReference()
173 * @see PropertyReference#getName()
174 * @see #clear()
175 */
176 @Override
177 public PropertyReference getReference( final String name )
178 {
179 if ( name == null )
180 {
181 throw new NullPointerException( "name" );
182 }
183
184 synchronized ( this.referencesByNameCache )
185 {
186 PropertyReference r = this.referencesByNameCache.get( name );
187
188 if ( r == null && !this.referencesByNameCache.containsKey( name ) )
189 {
190 r = super.getReference( name );
191 this.referencesByNameCache.put( name, r );
192 }
193
194 return r;
195 }
196 }
197
198 private void copyProperties()
199 {
200 for ( int i = 0, s0 = this.getProperty().size(); i < s0; i++ )
201 {
202 final Property p = this.getProperty().get( i );
203 this.getProperty().set( i, RuntimeModelObjects.getInstance().copyOf( p ) );
204 }
205 }
206
207 private void copyReferences()
208 {
209 for ( int i = 0, s0 = this.getReference().size(); i < s0; i++ )
210 {
211 final PropertyReference r = this.getReference().get( i );
212 this.getReference().set( i, RuntimeModelObjects.getInstance().copyOf( r ) );
213 }
214 }
215
216 // SECTION-END
217 // SECTION-START[RuntimeModelObject]
218 public void gc()
219 {
220 this.gcOrClear( true, false );
221 }
222
223 public void clear()
224 {
225 synchronized ( this.propertiesByNameCache )
226 {
227 this.propertiesByNameCache.clear();
228 }
229 synchronized ( this.referencesByNameCache )
230 {
231 this.referencesByNameCache.clear();
232 }
233
234 this.gcOrClear( false, true );
235 }
236
237 private void gcOrClear( final boolean gc, final boolean clear )
238 {
239 if ( this.getAuthors() instanceof RuntimeModelObject )
240 {
241 if ( gc )
242 {
243 ( (RuntimeModelObject) this.getAuthors() ).gc();
244 }
245 if ( clear )
246 {
247 ( (RuntimeModelObject) this.getAuthors() ).clear();
248 }
249 }
250 if ( this.getDocumentation() instanceof RuntimeModelObject )
251 {
252 if ( gc )
253 {
254 ( (RuntimeModelObject) this.getDocumentation() ).gc();
255 }
256 if ( clear )
257 {
258 ( (RuntimeModelObject) this.getDocumentation() ).clear();
259 }
260 }
261
262 this.gcOrClearProperties( gc, clear );
263 this.gcOrClearReferences( gc, clear );
264 }
265
266 private void gcOrClearProperties( final boolean gc, final boolean clear )
267 {
268 for ( int i = 0, s0 = this.getProperty().size(); i < s0; i++ )
269 {
270 final Property p = this.getProperty().get( i );
271 if ( p instanceof RuntimeModelObject )
272 {
273 if ( gc )
274 {
275 ( (RuntimeModelObject) p ).gc();
276 }
277 if ( clear )
278 {
279 ( (RuntimeModelObject) p ).clear();
280 }
281 }
282 }
283 }
284
285 private void gcOrClearReferences( final boolean gc, final boolean clear )
286 {
287 for ( int i = 0, s0 = this.getReference().size(); i < s0; i++ )
288 {
289 final PropertyReference r = this.getReference().get( i );
290 if ( r instanceof RuntimeModelObject )
291 {
292 if ( gc )
293 {
294 ( (RuntimeModelObject) r ).gc();
295 }
296 if ( clear )
297 {
298 ( (RuntimeModelObject) r ).clear();
299 }
300 }
301 }
302 }
303
304 // SECTION-END
305 // SECTION-START[Constructors]
306 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
307 /** Creates a new {@code RuntimeProperties} instance. */
308 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
309 public RuntimeProperties()
310 {
311 // SECTION-START[Default Constructor]
312 super();
313 // SECTION-END
314 }
315 // </editor-fold>
316 // SECTION-END
317 // SECTION-START[Dependencies]
318 // SECTION-END
319 // SECTION-START[Properties]
320 // SECTION-END
321 // SECTION-START[Messages]
322 // SECTION-END
323
324 }