View Javadoc
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: RuntimeImplementations.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.Implementation;
41  import org.jomc.model.ImplementationReference;
42  import org.jomc.model.Implementations;
43  import org.jomc.model.ModelObjectException;
44  import static org.jomc.ri.model.RuntimeModelObjects.createMap;
45  
46  // SECTION-START[Documentation]
47  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
48  /**
49   * Runtime {@code Implementations}.
50   *
51   * <dl>
52   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeImplementations</dd>
53   *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeImplementations</dd>
54   *   <dt><b>Specifications:</b></dt>
55   *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
56   *   <dt><b>Abstract:</b></dt><dd>No</dd>
57   *   <dt><b>Final:</b></dt><dd>No</dd>
58   *   <dt><b>Stateless:</b></dt><dd>No</dd>
59   * </dl>
60   *
61   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
62   * @version 1.2
63   */
64  // </editor-fold>
65  // SECTION-END
66  // SECTION-START[Annotations]
67  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
68  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
69  // </editor-fold>
70  // SECTION-END
71  public class RuntimeImplementations extends Implementations implements RuntimeModelObject
72  {
73      // SECTION-START[RuntimeImplementations]
74  
75      /**
76       * Cache map.
77       */
78      @XmlTransient
79      private transient final Map<String, Implementation> implementationsByIdentifierCache = createMap();
80  
81      /**
82       * Cache map.
83       */
84      @XmlTransient
85      private transient final Map<String, Implementation> implementationsByClassCache = createMap();
86  
87      /**
88       * Cache map.
89       */
90      @XmlTransient
91      private transient final Map<String, Implementation> implementationsByNameCache = createMap();
92  
93      /**
94       * Cache map.
95       */
96      @XmlTransient
97      private transient final Map<String, ImplementationReference> referencesByIdentifierCache = createMap();
98  
99      /**
100      * Creates a new {@code RuntimeImplementations} instance by deeply copying a given {@code Implementations} instance.
101      *
102      * @param implementations The instance to copy.
103      *
104      * @throws NullPointerException if {@code implementations} is {@code null}.
105      */
106     public RuntimeImplementations( final Implementations implementations )
107     {
108         super( implementations );
109 
110         if ( this.getAuthors() != null )
111         {
112             this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
113         }
114         if ( this.getDocumentation() != null )
115         {
116             this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
117         }
118 
119         this.copyImplementations();
120         this.copyImplementationReferences();
121     }
122 
123     /**
124      * Gets an implementation for a given identifier from the list of implementations.
125      * <p>
126      * This method queries an internal cache for a result object to return for the given argument values. If no
127      * cached result object is available, this method queries the super-class for a result object to return and caches
128      * the outcome of that query for use on successive calls.
129      * </p>
130      * <p>
131      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
132      * state of the instance, should the state of the instance change.
133      * </p>
134      *
135      * @param implementation The identifier of the implementation to return.
136      *
137      * @return The first matching implementation or {@code null}, if no such implementation is found.
138      *
139      * @throws NullPointerException if {@code implementation} is {@code null}.
140      *
141      * @see #getImplementation()
142      * @see Implementation#getIdentifier()
143      * @see #clear()
144      */
145     @Override
146     public Implementation getImplementation( final String implementation )
147     {
148         if ( implementation == null )
149         {
150             throw new NullPointerException( "implementation" );
151         }
152 
153         synchronized ( this.implementationsByIdentifierCache )
154         {
155             Implementation i = this.implementationsByIdentifierCache.get( implementation );
156 
157             if ( i == null && !this.implementationsByIdentifierCache.containsKey( implementation ) )
158             {
159                 i = super.getImplementation( implementation );
160                 this.implementationsByIdentifierCache.put( implementation, i );
161             }
162 
163             return i;
164         }
165     }
166 
167     /**
168      * Gets an implementation for a given class from the list of implementations.
169      * <p>
170      * This method queries an internal cache for a result object to return for the given argument values. If no
171      * cached result object is available, this method queries the super-class for a result object to return and caches
172      * the outcome of that query for use on successive calls.
173      * </p>
174      * <p>
175      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
176      * state of the instance, should the state of the instance change.
177      * </p>
178      *
179      * @param implementation The class of the implementation to return.
180      *
181      * @return The first matching implementation or {@code null}, if no such implementation is found.
182      *
183      * @throws NullPointerException if {@code implementation} is {@code null}.
184      * @throws ModelObjectException if parsing the name of a referenced type fails.
185      *
186      * @see #getImplementation()
187      * @see Implementation#isClassDeclaration()
188      * @see Implementation#getClazz()
189      * @see #clear()
190      */
191     @Override
192     public Implementation getImplementation( final Class<?> implementation ) throws ModelObjectException
193     {
194         if ( implementation == null )
195         {
196             throw new NullPointerException( "implementation" );
197         }
198 
199         synchronized ( this.implementationsByClassCache )
200         {
201             Implementation i = this.implementationsByClassCache.get( implementation.getName() );
202 
203             if ( i == null && !this.implementationsByClassCache.containsKey( implementation.getName() ) )
204             {
205                 i = super.getImplementation( implementation );
206                 this.implementationsByClassCache.put( implementation.getName(), i );
207             }
208 
209             return i;
210         }
211     }
212 
213     /**
214      * Gets an implementation for a given name from the list of implementations.
215      * <p>
216      * This method queries an internal cache for a result object to return for the given argument values. If no
217      * cached result object is available, this method queries the super-class for a result object to return and caches
218      * the outcome of that query for use on successive calls.
219      * </p>
220      * <p>
221      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
222      * state of the instance, should the state of the instance change.
223      * </p>
224      *
225      * @param name The name of the implementation to return.
226      *
227      * @return The first matching implementation or {@code null}, if no such implementation is found.
228      *
229      * @throws NullPointerException if {@code name} is {@code null}.
230      *
231      * @see #getImplementation()
232      * @see Implementation#getName()
233      * @see #clear()
234      */
235     @Override
236     public Implementation getImplementationByName( final String name )
237     {
238         if ( name == null )
239         {
240             throw new NullPointerException( "name" );
241         }
242 
243         synchronized ( this.implementationsByNameCache )
244         {
245             Implementation i = this.implementationsByNameCache.get( name );
246 
247             if ( i == null && !this.implementationsByNameCache.containsKey( name ) )
248             {
249                 i = super.getImplementationByName( name );
250                 this.implementationsByNameCache.put( name, i );
251             }
252 
253             return i;
254         }
255     }
256 
257     /**
258      * Gets an implementation reference for a given identifier from the list of references.
259      * <p>
260      * This method queries an internal cache for a result object to return for the given argument values. If no
261      * cached result object is available, this method queries the super-class for a result object to return and caches
262      * the outcome of that query for use on successive calls.
263      * </p>
264      * <p>
265      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
266      * state of the instance, should the state of the instance change.
267      * </p>
268      *
269      * @param implementation The identifier of the reference to return.
270      *
271      * @return The first matching implementation reference or {@code null}, if no such reference is found.
272      *
273      * @throws NullPointerException if {@code implementation} is {@code null}.
274      *
275      * @see #getReference()
276      * @see ImplementationReference#getIdentifier()
277      * @see #clear()
278      */
279     @Override
280     public ImplementationReference getReference( final String implementation )
281     {
282         if ( implementation == null )
283         {
284             throw new NullPointerException( "implementation" );
285         }
286 
287         synchronized ( this.referencesByIdentifierCache )
288         {
289             ImplementationReference r = this.referencesByIdentifierCache.get( implementation );
290 
291             if ( r == null && !this.referencesByIdentifierCache.containsKey( implementation ) )
292             {
293                 r = super.getReference( implementation );
294                 this.referencesByIdentifierCache.put( implementation, r );
295             }
296 
297             return r;
298         }
299     }
300 
301     private void copyImplementations()
302     {
303         for ( int i = 0, s0 = this.getImplementation().size(); i < s0; i++ )
304         {
305             final Implementation impl = this.getImplementation().get( i );
306             this.getImplementation().set( i, RuntimeModelObjects.getInstance().copyOf( impl ) );
307         }
308     }
309 
310     private void copyImplementationReferences()
311     {
312         for ( int i = 0, s0 = this.getReference().size(); i < s0; i++ )
313         {
314             final ImplementationReference r = this.getReference().get( i );
315             this.getReference().set( i, RuntimeModelObjects.getInstance().copyOf( r ) );
316         }
317     }
318 
319     // SECTION-END
320     // SECTION-START[RuntimeModelObject]
321     public void gc()
322     {
323         this.gcOrClear( true, false );
324     }
325 
326     public void clear()
327     {
328         synchronized ( this.implementationsByClassCache )
329         {
330             this.implementationsByClassCache.clear();
331         }
332         synchronized ( this.implementationsByIdentifierCache )
333         {
334             this.implementationsByIdentifierCache.clear();
335         }
336         synchronized ( this.implementationsByNameCache )
337         {
338             this.implementationsByNameCache.clear();
339         }
340         synchronized ( this.referencesByIdentifierCache )
341         {
342             this.referencesByIdentifierCache.clear();
343         }
344 
345         this.gcOrClear( false, true );
346     }
347 
348     private void gcOrClear( final boolean gc, final boolean clear )
349     {
350         if ( this.getAuthors() instanceof RuntimeModelObject )
351         {
352             if ( gc )
353             {
354                 ( (RuntimeModelObject) this.getAuthors() ).gc();
355             }
356             if ( clear )
357             {
358                 ( (RuntimeModelObject) this.getAuthors() ).clear();
359             }
360         }
361         if ( this.getDocumentation() instanceof RuntimeModelObject )
362         {
363             if ( gc )
364             {
365                 ( (RuntimeModelObject) this.getDocumentation() ).gc();
366             }
367             if ( clear )
368             {
369                 ( (RuntimeModelObject) this.getDocumentation() ).clear();
370             }
371         }
372 
373         this.gcOrClearImplementationReferences( gc, clear );
374         this.gcOrClearImplementations( gc, clear );
375     }
376 
377     private void gcOrClearImplementations( final boolean gc, final boolean clear )
378     {
379         for ( int i = 0, s0 = this.getImplementation().size(); i < s0; i++ )
380         {
381             final Implementation impl = this.getImplementation().get( i );
382             if ( impl instanceof RuntimeModelObject )
383             {
384                 if ( gc )
385                 {
386                     ( (RuntimeModelObject) impl ).gc();
387                 }
388                 if ( clear )
389                 {
390                     ( (RuntimeModelObject) impl ).clear();
391                 }
392             }
393         }
394     }
395 
396     private void gcOrClearImplementationReferences( final boolean gc, final boolean clear )
397     {
398         for ( int i = 0, s0 = this.getReference().size(); i < s0; i++ )
399         {
400             final ImplementationReference r = this.getReference().get( i );
401             if ( r instanceof RuntimeModelObject )
402             {
403                 if ( gc )
404                 {
405                     ( (RuntimeModelObject) r ).gc();
406                 }
407                 if ( clear )
408                 {
409                     ( (RuntimeModelObject) r ).clear();
410                 }
411             }
412         }
413     }
414 
415     // SECTION-END
416     // SECTION-START[Constructors]
417     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
418     /** Creates a new {@code RuntimeImplementations} instance. */
419     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
420     public RuntimeImplementations()
421     {
422         // SECTION-START[Default Constructor]
423         super();
424         // SECTION-END
425     }
426     // </editor-fold>
427     // SECTION-END
428     // SECTION-START[Dependencies]
429     // SECTION-END
430     // SECTION-START[Properties]
431     // SECTION-END
432     // SECTION-START[Messages]
433     // SECTION-END
434 
435 }