001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Java Object Management and Configuration
005     *   Copyright (C) Christian Schulte, 2011-313
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     *
031     *   $JOMC: RuntimeArguments.java 4381 2012-03-04 19:29:29Z schulte2005 $
032     *
033     */
034    // </editor-fold>
035    // SECTION-END
036    package org.jomc.ri.model;
037    
038    import java.util.Map;
039    import javax.xml.bind.annotation.XmlTransient;
040    import org.jomc.model.Argument;
041    import org.jomc.model.Arguments;
042    import static org.jomc.ri.model.RuntimeModelObjects.createMap;
043    
044    // SECTION-START[Documentation]
045    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
046    /**
047     * Runtime {@code Arguments}.
048     *
049     * <dl>
050     *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeArguments</dd>
051     *   <dt><b>Name:</b></dt><dd>JOMC RI RuntimeArguments</dd>
052     *   <dt><b>Specifications:</b></dt>
053     *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
054     *   <dt><b>Abstract:</b></dt><dd>No</dd>
055     *   <dt><b>Final:</b></dt><dd>No</dd>
056     *   <dt><b>Stateless:</b></dt><dd>No</dd>
057     * </dl>
058     *
059     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.2
060     * @version 1.2
061     */
062    // </editor-fold>
063    // SECTION-END
064    // SECTION-START[Annotations]
065    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
066    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
067    // </editor-fold>
068    // SECTION-END
069    public class RuntimeArguments extends Arguments implements RuntimeModelObject
070    {
071        // SECTION-START[RuntimeArguments]
072    
073        /** Cache map. */
074        @XmlTransient
075        private transient final Map<String, Argument> argumentsByNameCache = createMap();
076    
077        /** Cache map. */
078        @XmlTransient
079        private transient final Map<Number, Argument> argumentsByIndexCache = createMap();
080    
081        /**
082         * Creates a new {@code RuntimeArguments} instance by deeply copying a given {@code Arguments} instance.
083         *
084         * @param arguments The instance to copy.
085         *
086         * @throws NullPointerException if {@code arguments} is {@code null}.
087         */
088        public RuntimeArguments( final Arguments arguments )
089        {
090            super( arguments );
091    
092            if ( this.getAuthors() != null )
093            {
094                this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
095            }
096            if ( this.getDocumentation() != null )
097            {
098                this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
099            }
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.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
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    }