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