RuntimeInstances.java

  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. import java.util.Map;
  38. import javax.xml.bind.annotation.XmlTransient;
  39. import org.jomc.model.Instance;
  40. import org.jomc.model.Instances;
  41. import static org.jomc.ri.model.RuntimeModelObjects.createMap;

  42. // SECTION-START[Documentation]
  43. // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
  44. /**
  45.  * Runtime {@code Instances}.
  46.  *
  47.  * <dl>
  48.  *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeInstances</dd>
  49.  *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeInstances</dd>
  50.  *   <dt><b>Specifications:</b></dt>
  51.  *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
  52.  *   <dt><b>Abstract:</b></dt><dd>No</dd>
  53.  *   <dt><b>Final:</b></dt><dd>No</dd>
  54.  *   <dt><b>Stateless:</b></dt><dd>No</dd>
  55.  * </dl>
  56.  *
  57.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
  58.  * @version 1.2
  59.  */
  60. // </editor-fold>
  61. // SECTION-END
  62. // SECTION-START[Annotations]
  63. // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
  64. @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
  65. // </editor-fold>
  66. // SECTION-END
  67. public class RuntimeInstances extends Instances implements RuntimeModelObject
  68. {
  69.     // SECTION-START[RuntimeInstances]

  70.     /**
  71.      * Cache map.
  72.      */
  73.     @XmlTransient
  74.     private transient final Map<String, Instance> instancesByIdentifierCache = createMap();

  75.     /**
  76.      * Creates a new {@code RuntimeInstances} instance by deeply copying a given {@code Instances} instance.
  77.      *
  78.      * @param instances The instance to copy.
  79.      *
  80.      * @throws NullPointerException if {@code instances} is {@code null}.
  81.      */
  82.     public RuntimeInstances( final Instances instances )
  83.     {
  84.         super( instances );

  85.         if ( this.getAuthors() != null )
  86.         {
  87.             this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
  88.         }
  89.         if ( this.getDocumentation() != null )
  90.         {
  91.             this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
  92.         }

  93.         this.copyInstances();
  94.     }

  95.     /**
  96.      * Gets an instance for a given identifier from the list of instances.
  97.      * <p>
  98.      * This method queries an internal cache for a result object to return for the given argument values. If no
  99.      * cached result object is available, this method queries the super-class for a result object to return and caches
  100.      * the outcome of that query for use on successive calls.
  101.      * </p>
  102.      * <p>
  103.      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
  104.      * state of the instance, should the state of the instance change.
  105.      * </p>
  106.      *
  107.      * @param identifier The identifier of the instance to return.
  108.      *
  109.      * @return The first matching instance or {@code null}, if no such instance is found.
  110.      *
  111.      * @throws NullPointerException if {@code identifier} is {@code null}.
  112.      *
  113.      * @see #getInstance()
  114.      * @see Instance#getIdentifier()
  115.      * @see #clear()
  116.      */
  117.     @Override
  118.     public Instance getInstance( final String identifier )
  119.     {
  120.         if ( identifier == null )
  121.         {
  122.             throw new NullPointerException( "identifier" );
  123.         }

  124.         synchronized ( this.instancesByIdentifierCache )
  125.         {
  126.             Instance i = this.instancesByIdentifierCache.get( identifier );

  127.             if ( i == null && !this.instancesByIdentifierCache.containsKey( identifier ) )
  128.             {
  129.                 i = super.getInstance( identifier );
  130.                 this.instancesByIdentifierCache.put( identifier, i );
  131.             }

  132.             return i;
  133.         }
  134.     }

  135.     private void copyInstances()
  136.     {
  137.         for ( int i = 0, s0 = this.getInstance().size(); i < s0; i++ )
  138.         {
  139.             final Instance inst = this.getInstance().get( i );
  140.             this.getInstance().set( i, RuntimeModelObjects.getInstance().copyOf( inst ) );
  141.         }
  142.     }

  143.     // SECTION-END
  144.     // SECTION-START[RuntimeModelObject]
  145.     public void gc()
  146.     {
  147.         this.gcOrClear( true, false );
  148.     }

  149.     public void clear()
  150.     {
  151.         synchronized ( this.instancesByIdentifierCache )
  152.         {
  153.             this.instancesByIdentifierCache.clear();
  154.         }

  155.         this.gcOrClear( false, true );
  156.     }

  157.     private void gcOrClear( final boolean gc, final boolean clear )
  158.     {
  159.         if ( this.getAuthors() instanceof RuntimeModelObject )
  160.         {
  161.             if ( gc )
  162.             {
  163.                 ( (RuntimeModelObject) this.getAuthors() ).gc();
  164.             }
  165.             if ( clear )
  166.             {
  167.                 ( (RuntimeModelObject) this.getAuthors() ).clear();
  168.             }
  169.         }
  170.         if ( this.getDocumentation() instanceof RuntimeModelObject )
  171.         {
  172.             if ( gc )
  173.             {
  174.                 ( (RuntimeModelObject) this.getDocumentation() ).gc();
  175.             }
  176.             if ( clear )
  177.             {
  178.                 ( (RuntimeModelObject) this.getDocumentation() ).clear();
  179.             }
  180.         }

  181.         this.gcOrClearInstances( gc, clear );
  182.     }

  183.     private void gcOrClearInstances( final boolean handle, final boolean prepare )
  184.     {
  185.         for ( int i = 0, s0 = this.getInstance().size(); i < s0; i++ )
  186.         {
  187.             final Instance inst = this.getInstance().get( i );
  188.             if ( inst instanceof RuntimeModelObject )
  189.             {
  190.                 if ( handle )
  191.                 {
  192.                     ( (RuntimeModelObject) inst ).gc();
  193.                 }
  194.                 if ( prepare )
  195.                 {
  196.                     ( (RuntimeModelObject) inst ).clear();
  197.                 }
  198.             }
  199.         }
  200.     }

  201.     // SECTION-END
  202.     // SECTION-START[Constructors]
  203.     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
  204.     /** Creates a new {@code RuntimeInstances} instance. */
  205.     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
  206.     public RuntimeInstances()
  207.     {
  208.         // SECTION-START[Default Constructor]
  209.         super();
  210.         // SECTION-END
  211.     }
  212.     // </editor-fold>
  213.     // SECTION-END
  214.     // SECTION-START[Dependencies]
  215.     // SECTION-END
  216.     // SECTION-START[Properties]
  217.     // SECTION-END
  218.     // SECTION-START[Messages]
  219.     // SECTION-END

  220. }