| 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: RuntimeDependencies.java 4712 2013-01-02 13:56:12Z 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.Dependencies; | 
| 41 | import org.jomc.model.Dependency; | 
| 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 Dependencies}. | 
| 48 | * | 
| 49 | * <dl> | 
| 50 | *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeDependencies</dd> | 
| 51 | *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeDependencies</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.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" ) | 
| 67 | // </editor-fold> | 
| 68 | // SECTION-END | 
| 69 | public class RuntimeDependencies extends Dependencies implements RuntimeModelObject | 
| 70 | { | 
| 71 | // SECTION-START[RuntimeDependencies] | 
| 72 |  | 
| 73 | /** Cache map. */ | 
| 74 | @XmlTransient | 
| 75 | private transient final Map<String, Dependency> dependenciesByNameCache = createMap(); | 
| 76 |  | 
| 77 | /** | 
| 78 | * Creates a new {@code RuntimeDependencies} instance by deeply copying a given {@code Dependencies} instance. | 
| 79 | * | 
| 80 | * @param dependencies The instance to copy. | 
| 81 | * | 
| 82 | * @throws NullPointerException if {@code dependencies} is {@code null}. | 
| 83 | */ | 
| 84 | public RuntimeDependencies( final Dependencies dependencies ) | 
| 85 | { | 
| 86 | super( dependencies ); | 
| 87 |  | 
| 88 | if ( this.getAuthors() != null ) | 
| 89 | { | 
| 90 | this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) ); | 
| 91 | } | 
| 92 | if ( this.getDocumentation() != null ) | 
| 93 | { | 
| 94 | this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) ); | 
| 95 | } | 
| 96 |  | 
| 97 | this.copyDependencies(); | 
| 98 | } | 
| 99 |  | 
| 100 | /** | 
| 101 | * Gets a dependency for a given name from the list of dependencies. | 
| 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 name The name of the dependency to return. | 
| 109 | * | 
| 110 | * @return The first matching dependency or {@code null}, if no such dependency is found. | 
| 111 | * | 
| 112 | * @throws NullPointerException if {@code name} is {@code null}. | 
| 113 | * | 
| 114 | * @see #getDependency() | 
| 115 | * @see Dependency#getName() | 
| 116 | * @see #clear() | 
| 117 | */ | 
| 118 | @Override | 
| 119 | public Dependency getDependency( final String name ) | 
| 120 | { | 
| 121 | if ( name == null ) | 
| 122 | { | 
| 123 | throw new NullPointerException( "identifier" ); | 
| 124 | } | 
| 125 |  | 
| 126 | synchronized ( this.dependenciesByNameCache ) | 
| 127 | { | 
| 128 | Dependency d = this.dependenciesByNameCache.get( name ); | 
| 129 |  | 
| 130 | if ( d == null && !this.dependenciesByNameCache.containsKey( name ) ) | 
| 131 | { | 
| 132 | d = super.getDependency( name ); | 
| 133 | this.dependenciesByNameCache.put( name, d ); | 
| 134 | } | 
| 135 |  | 
| 136 | return d; | 
| 137 | } | 
| 138 | } | 
| 139 |  | 
| 140 | private void copyDependencies() | 
| 141 | { | 
| 142 | for ( int i = 0, s0 = this.getDependency().size(); i < s0; i++ ) | 
| 143 | { | 
| 144 | final Dependency d = this.getDependency().get( i ); | 
| 145 | this.getDependency().set( i, RuntimeModelObjects.getInstance().copyOf( d ) ); | 
| 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.dependenciesByNameCache ) | 
| 159 | { | 
| 160 | this.dependenciesByNameCache.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.gcOrClearDependencies( gc, clear ); | 
| 192 | } | 
| 193 |  | 
| 194 | private void gcOrClearDependencies( final boolean gc, final boolean clear ) | 
| 195 | { | 
| 196 |  | 
| 197 | for ( int i = 0, s0 = this.getDependency().size(); i < s0; i++ ) | 
| 198 | { | 
| 199 | final Dependency d = this.getDependency().get( i ); | 
| 200 | if ( d instanceof RuntimeModelObject ) | 
| 201 | { | 
| 202 | if ( gc ) | 
| 203 | { | 
| 204 | ( (RuntimeModelObject) d ).gc(); | 
| 205 | } | 
| 206 | if ( clear ) | 
| 207 | { | 
| 208 | ( (RuntimeModelObject) d ).clear(); | 
| 209 | } | 
| 210 | } | 
| 211 | } | 
| 212 | } | 
| 213 |  | 
| 214 | // SECTION-END | 
| 215 | // SECTION-START[Constructors] | 
| 216 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> | 
| 217 | /** Creates a new {@code RuntimeDependencies} instance. */ | 
| 218 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" ) | 
| 219 | public RuntimeDependencies() | 
| 220 | { | 
| 221 | // SECTION-START[Default Constructor] | 
| 222 | super(); | 
| 223 | // SECTION-END | 
| 224 | } | 
| 225 | // </editor-fold> | 
| 226 | // SECTION-END | 
| 227 | // SECTION-START[Dependencies] | 
| 228 | // SECTION-END | 
| 229 | // SECTION-START[Properties] | 
| 230 | // SECTION-END | 
| 231 | // SECTION-START[Messages] | 
| 232 | // SECTION-END | 
| 233 | } |