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: RuntimeInstances.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.Instance;
041    import org.jomc.model.Instances;
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 Instances}.
048     *
049     * <dl>
050     *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeInstances</dd>
051     *   <dt><b>Name:</b></dt><dd>JOMC RI RuntimeInstances</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 RuntimeInstances extends Instances implements RuntimeModelObject
070    {
071        // SECTION-START[RuntimeInstances]
072    
073        /** Cache map. */
074        @XmlTransient
075        private transient final Map<String, Instance> instancesByIdentifierCache = createMap();
076    
077        /**
078         * Creates a new {@code RuntimeInstances} instance by deeply copying a given {@code Instances} instance.
079         *
080         * @param instances The instance to copy.
081         *
082         * @throws NullPointerException if {@code instances} is {@code null}.
083         */
084        public RuntimeInstances( final Instances instances )
085        {
086            super( instances );
087    
088            if ( this.getAuthors() != null )
089            {
090                this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
091            }
092            if ( this.getDocumentation() != null )
093            {
094                this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
095            }
096    
097            this.copyInstances();
098        }
099    
100        /**
101         * Gets an instance for a given identifier from the list of instances.
102         * <p>This method queries an internal cache for a result object to return for the given argument values. If no
103         * cached result object is available, this method queries the super-class for a result object to return and caches
104         * the outcome of that query for use on successive calls.</p>
105         * <p><b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
106         * state of the instance, should the state of the instance change.</p>
107         *
108         * @param identifier The identifier of the instance to return.
109         *
110         * @return The first matching instance or {@code null}, if no such instance is found.
111         *
112         * @throws NullPointerException if {@code identifier} is {@code null}.
113         *
114         * @see #getInstance()
115         * @see Instance#getIdentifier()
116         * @see #clear()
117         */
118        @Override
119        public Instance getInstance( final String identifier )
120        {
121            if ( identifier == null )
122            {
123                throw new NullPointerException( "identifier" );
124            }
125    
126            synchronized ( this.instancesByIdentifierCache )
127            {
128                Instance i = this.instancesByIdentifierCache.get( identifier );
129    
130                if ( i == null && !this.instancesByIdentifierCache.containsKey( identifier ) )
131                {
132                    i = super.getInstance( identifier );
133                    this.instancesByIdentifierCache.put( identifier, i );
134                }
135    
136                return i;
137            }
138        }
139    
140        private void copyInstances()
141        {
142            for ( int i = 0, s0 = this.getInstance().size(); i < s0; i++ )
143            {
144                final Instance inst = this.getInstance().get( i );
145                this.getInstance().set( i, RuntimeModelObjects.getInstance().copyOf( inst ) );
146            }
147        }
148    
149        // SECTION-END
150        // SECTION-START[RuntimeModelObject]
151        public void gc()
152        {
153            this.gcOrClear( true, false );
154        }
155    
156        public void clear()
157        {
158            synchronized ( this.instancesByIdentifierCache )
159            {
160                this.instancesByIdentifierCache.clear();
161            }
162    
163            this.gcOrClear( false, true );
164        }
165    
166        private void gcOrClear( final boolean gc, final boolean clear )
167        {
168            if ( this.getAuthors() instanceof RuntimeModelObject )
169            {
170                if ( gc )
171                {
172                    ( (RuntimeModelObject) this.getAuthors() ).gc();
173                }
174                if ( clear )
175                {
176                    ( (RuntimeModelObject) this.getAuthors() ).clear();
177                }
178            }
179            if ( this.getDocumentation() instanceof RuntimeModelObject )
180            {
181                if ( gc )
182                {
183                    ( (RuntimeModelObject) this.getDocumentation() ).gc();
184                }
185                if ( clear )
186                {
187                    ( (RuntimeModelObject) this.getDocumentation() ).clear();
188                }
189            }
190    
191            this.gcOrClearInstances( gc, clear );
192        }
193    
194        private void gcOrClearInstances( final boolean handle, final boolean prepare )
195        {
196            for ( int i = 0, s0 = this.getInstance().size(); i < s0; i++ )
197            {
198                final Instance inst = this.getInstance().get( i );
199                if ( inst instanceof RuntimeModelObject )
200                {
201                    if ( handle )
202                    {
203                        ( (RuntimeModelObject) inst ).gc();
204                    }
205                    if ( prepare )
206                    {
207                        ( (RuntimeModelObject) inst ).clear();
208                    }
209                }
210            }
211        }
212        // SECTION-END
213        // SECTION-START[Constructors]
214        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
215        /** Creates a new {@code RuntimeInstances} instance. */
216        @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" )
217        public RuntimeInstances()
218        {
219            // SECTION-START[Default Constructor]
220            super();
221            // SECTION-END
222        }
223        // </editor-fold>
224        // SECTION-END
225        // SECTION-START[Dependencies]
226        // SECTION-END
227        // SECTION-START[Properties]
228        // SECTION-END
229        // SECTION-START[Messages]
230        // SECTION-END
231    }