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: RuntimeArguments.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.Argument;
41  import org.jomc.model.Arguments;
42  import static org.jomc.ri.model.RuntimeModelObjects.createMap;
43  
44  // SECTION-START[Documentation]
45  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
46  /**
47   * Runtime {@code Arguments}.
48   *
49   * <dl>
50   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeArguments</dd>
51   *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeArguments</dd>
52   *   <dt><b>Specifications:</b></dt>
53   *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
54   *   <dt><b>Abstract:</b></dt><dd>No</dd>
55   *   <dt><b>Final:</b></dt><dd>No</dd>
56   *   <dt><b>Stateless:</b></dt><dd>No</dd>
57   * </dl>
58   *
59   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
60   * @version 1.2
61   */
62  // </editor-fold>
63  // SECTION-END
64  // SECTION-START[Annotations]
65  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
66  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
67  // </editor-fold>
68  // SECTION-END
69  public class RuntimeArguments extends Arguments implements RuntimeModelObject
70  {
71      // SECTION-START[RuntimeArguments]
72  
73      /**
74       * Cache map.
75       */
76      @XmlTransient
77      private transient final Map<String, Argument> argumentsByNameCache = createMap();
78  
79      /**
80       * Cache map.
81       */
82      @XmlTransient
83      private transient final Map<Number, Argument> argumentsByIndexCache = createMap();
84  
85      /**
86       * Creates a new {@code RuntimeArguments} instance by deeply copying a given {@code Arguments} instance.
87       *
88       * @param arguments The instance to copy.
89       *
90       * @throws NullPointerException if {@code arguments} is {@code null}.
91       */
92      public RuntimeArguments( final Arguments arguments )
93      {
94          super( arguments );
95  
96          if ( this.getAuthors() != null )
97          {
98              this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
99          }
100         if ( this.getDocumentation() != null )
101         {
102             this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
103         }
104 
105         this.copyArguments();
106     }
107 
108     /**
109      * Gets an argument for a given name from the list of arguments.
110      * <p>
111      * This method queries an internal cache for a result object to return for the given argument values. If no
112      * cached result object is available, this method queries the super-class for a result object to return and caches
113      * the outcome of that query for use on successive calls.
114      * </p>
115      * <p>
116      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
117      * state of the instance, should the state of the instance change.
118      * </p>
119      *
120      * @param name The name of the argument to return.
121      *
122      * @return The first matching argument or {@code null}, if no such argument is found.
123      *
124      * @throws NullPointerException if {@code name} is {@code null}.
125      *
126      * @see #getArgument()
127      * @see Argument#getName()
128      * @see #clear()
129      */
130     @Override
131     public Argument getArgument( final String name )
132     {
133         if ( name == null )
134         {
135             throw new NullPointerException( "name" );
136         }
137 
138         synchronized ( this.argumentsByNameCache )
139         {
140             Argument a = this.argumentsByNameCache.get( name );
141 
142             if ( a == null && !this.argumentsByNameCache.containsKey( name ) )
143             {
144                 a = super.getArgument( name );
145                 this.argumentsByNameCache.put( name, a );
146             }
147 
148             return a;
149         }
150     }
151 
152     /**
153      * Gets an argument for a given index from the list of arguments.
154      * <p>
155      * This method queries an internal cache for a result object to return for the given argument values. If no
156      * cached result object is available, this method queries the super-class for a result object to return and caches
157      * the outcome of that query for use on successive calls.
158      * </p>
159      * <p>
160      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
161      * state of the instance, should the state of the instance change.
162      * </p>
163      *
164      * @param index The index of the argument to return.
165      *
166      * @return The first matching argument or {@code null}, if no such argument is found.
167      *
168      * @throws IndexOutOfBoundsException if {@code index} is negative, greater or equal to the size of the list of
169      * arguments.
170      *
171      * @see #getArgument()
172      * @see Argument#getIndex()
173      * @see #clear()
174      */
175     @Override
176     public Argument getArgument( final int index )
177     {
178         if ( index < 0 || index >= this.getArgument().size() )
179         {
180             throw new IndexOutOfBoundsException( Integer.toString( index ) );
181         }
182 
183         synchronized ( this.argumentsByIndexCache )
184         {
185             Argument a = this.argumentsByIndexCache.get( index );
186 
187             if ( a == null && !this.argumentsByIndexCache.containsKey( index ) )
188             {
189                 a = super.getArgument( index );
190                 this.argumentsByIndexCache.put( index, a );
191             }
192 
193             return a;
194         }
195     }
196 
197     private void copyArguments()
198     {
199         for ( int i = 0, s0 = this.getArgument().size(); i < s0; i++ )
200         {
201             final Argument a = this.getArgument().get( i );
202             this.getArgument().set( i, RuntimeModelObjects.getInstance().copyOf( a ) );
203         }
204     }
205 
206     // SECTION-END
207     // SECTION-START[RuntimeModelObject]
208     public void gc()
209     {
210         this.gcOrClear( true, false );
211     }
212 
213     public void clear()
214     {
215         synchronized ( this.argumentsByIndexCache )
216         {
217             this.argumentsByIndexCache.clear();
218         }
219         synchronized ( this.argumentsByNameCache )
220         {
221             this.argumentsByNameCache.clear();
222         }
223 
224         this.gcOrClear( false, true );
225     }
226 
227     private void gcOrClear( final boolean gc, final boolean clear )
228     {
229         if ( this.getAuthors() instanceof RuntimeModelObject )
230         {
231             if ( gc )
232             {
233                 ( (RuntimeModelObject) this.getAuthors() ).gc();
234             }
235             if ( clear )
236             {
237                 ( (RuntimeModelObject) this.getAuthors() ).clear();
238             }
239         }
240         if ( this.getDocumentation() instanceof RuntimeModelObject )
241         {
242             if ( gc )
243             {
244                 ( (RuntimeModelObject) this.getDocumentation() ).gc();
245             }
246             if ( clear )
247             {
248                 ( (RuntimeModelObject) this.getDocumentation() ).clear();
249             }
250         }
251 
252         this.gcOrClearArguments( gc, clear );
253     }
254 
255     private void gcOrClearArguments( final boolean gc, final boolean clear )
256     {
257         for ( int i = 0, s0 = this.getArgument().size(); i < s0; i++ )
258         {
259             final Argument a = this.getArgument().get( i );
260             if ( a instanceof RuntimeModelObject )
261             {
262                 if ( gc )
263                 {
264                     ( (RuntimeModelObject) a ).gc();
265                 }
266                 if ( clear )
267                 {
268                     ( (RuntimeModelObject) a ).clear();
269                 }
270             }
271         }
272     }
273 
274     // SECTION-END
275     // SECTION-START[Constructors]
276     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
277     /** Creates a new {@code RuntimeArguments} instance. */
278     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
279     public RuntimeArguments()
280     {
281         // SECTION-START[Default Constructor]
282         super();
283         // SECTION-END
284     }
285     // </editor-fold>
286     // SECTION-END
287     // SECTION-START[Dependencies]
288     // SECTION-END
289     // SECTION-START[Properties]
290     // SECTION-END
291     // SECTION-START[Messages]
292     // SECTION-END
293 
294 }