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