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 <cs@schulte.it>, 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: RuntimeInstances.java 5061 2015-05-31 13:20:40Z 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.Instance; 41 import org.jomc.model.Instances; 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 Instances}. 48 * 49 * <dl> 50 * <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeInstances</dd> 51 * <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeInstances</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.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" ) 67 // </editor-fold> 68 // SECTION-END 69 public class RuntimeInstances extends Instances implements RuntimeModelObject 70 { 71 // SECTION-START[RuntimeInstances] 72 73 /** 74 * Cache map. 75 */ 76 @XmlTransient 77 private transient final Map<String, Instance> instancesByIdentifierCache = createMap(); 78 79 /** 80 * Creates a new {@code RuntimeInstances} instance by deeply copying a given {@code Instances} instance. 81 * 82 * @param instances The instance to copy. 83 * 84 * @throws NullPointerException if {@code instances} is {@code null}. 85 */ 86 public RuntimeInstances( final Instances instances ) 87 { 88 super( instances ); 89 90 if ( this.getAuthors() != null ) 91 { 92 this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) ); 93 } 94 if ( this.getDocumentation() != null ) 95 { 96 this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) ); 97 } 98 99 this.copyInstances(); 100 } 101 102 /** 103 * Gets an instance for a given identifier from the list of instances. 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 identifier The identifier of the instance to return. 115 * 116 * @return The first matching instance or {@code null}, if no such instance is found. 117 * 118 * @throws NullPointerException if {@code identifier} is {@code null}. 119 * 120 * @see #getInstance() 121 * @see Instance#getIdentifier() 122 * @see #clear() 123 */ 124 @Override 125 public Instance getInstance( final String identifier ) 126 { 127 if ( identifier == null ) 128 { 129 throw new NullPointerException( "identifier" ); 130 } 131 132 synchronized ( this.instancesByIdentifierCache ) 133 { 134 Instance i = this.instancesByIdentifierCache.get( identifier ); 135 136 if ( i == null && !this.instancesByIdentifierCache.containsKey( identifier ) ) 137 { 138 i = super.getInstance( identifier ); 139 this.instancesByIdentifierCache.put( identifier, i ); 140 } 141 142 return i; 143 } 144 } 145 146 private void copyInstances() 147 { 148 for ( int i = 0, s0 = this.getInstance().size(); i < s0; i++ ) 149 { 150 final Instance inst = this.getInstance().get( i ); 151 this.getInstance().set( i, RuntimeModelObjects.getInstance().copyOf( inst ) ); 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.instancesByIdentifierCache ) 165 { 166 this.instancesByIdentifierCache.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.gcOrClearInstances( gc, clear ); 198 } 199 200 private void gcOrClearInstances( final boolean handle, final boolean prepare ) 201 { 202 for ( int i = 0, s0 = this.getInstance().size(); i < s0; i++ ) 203 { 204 final Instance inst = this.getInstance().get( i ); 205 if ( inst instanceof RuntimeModelObject ) 206 { 207 if ( handle ) 208 { 209 ( (RuntimeModelObject) inst ).gc(); 210 } 211 if ( prepare ) 212 { 213 ( (RuntimeModelObject) inst ).clear(); 214 } 215 } 216 } 217 } 218 219 // SECTION-END 220 // SECTION-START[Constructors] 221 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> 222 /** Creates a new {@code RuntimeInstances} instance. */ 223 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" ) 224 public RuntimeInstances() 225 { 226 // SECTION-START[Default Constructor] 227 super(); 228 // SECTION-END 229 } 230 // </editor-fold> 231 // SECTION-END 232 // SECTION-START[Dependencies] 233 // SECTION-END 234 // SECTION-START[Properties] 235 // SECTION-END 236 // SECTION-START[Messages] 237 // SECTION-END 238 239 }