RuntimeMessage.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: RuntimeMessage.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.text.MessageFormat;
  38. import java.util.Locale;
  39. import java.util.Map;
  40. import javax.xml.bind.annotation.XmlTransient;
  41. import org.jomc.model.Message;
  42. import org.jomc.model.ModelObjectException;
  43. import static org.jomc.ri.model.RuntimeModelObjects.createMap;

  44. // SECTION-START[Documentation]
  45. // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
  46. /**
  47.  * Runtime {@code Message}.
  48.  *
  49.  * <dl>
  50.  *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeMessage</dd>
  51.  *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeMessage</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 RuntimeMessage extends Message implements RuntimeModelObject
  70. {
  71.     // SECTION-START[RuntimeMessage]

  72.     /**
  73.      * Java messages by locale cache.
  74.      */
  75.     @XmlTransient
  76.     private final Map<Locale, MessageFormat> javaMessagesByLocaleCache = createMap();

  77.     /**
  78.      * Creates a new {@code RuntimeMessage} instance by deeply copying a given {@code Message} instance.
  79.      *
  80.      * @param message The instance to copy.
  81.      *
  82.      * @throws NullPointerException if {@code message} is {@code null}.
  83.      */
  84.     public RuntimeMessage( final Message message )
  85.     {
  86.         super( message );

  87.         if ( this.getArguments() != null )
  88.         {
  89.             this.setArguments( RuntimeModelObjects.getInstance().copyOf( this.getArguments() ) );
  90.         }
  91.         if ( this.getAuthors() != null )
  92.         {
  93.             this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
  94.         }
  95.         if ( this.getDocumentation() != null )
  96.         {
  97.             this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
  98.         }
  99.         if ( this.getTemplate() != null )
  100.         {
  101.             this.setTemplate( RuntimeModelObjects.getInstance().copyOf( this.getTemplate() ) );
  102.         }
  103.     }

  104.     /**
  105.      * Gets a Java {@code MessageFormat} instance for a given locale.
  106.      * <p>
  107.      * This method queries an internal cache for a result object to return for the given argument values. If no
  108.      * cached result object is available, this method queries the super-class for a result object to return and caches
  109.      * the outcome of that query for use on successive calls.
  110.      * </p>
  111.      * <p>
  112.      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
  113.      * state of the instance, should the state of the instance change.
  114.      * </p>
  115.      *
  116.      * @param locale The locale to get a Java {@code MessageFormat} instance for.
  117.      *
  118.      * @return A Java {@code MessageFormat} instance for {@code locale}.
  119.      *
  120.      * @throws NullPointerException if {@code locale} is {@code null}.
  121.      * @throws ModelObjectException if compiling the template of the message for {@code locale} to a
  122.      * {@code MessageFormat} fails.
  123.      *
  124.      * @see #getTemplate()
  125.      * @see #clear()
  126.      */
  127.     @Override
  128.     public MessageFormat getJavaMessage( final Locale locale ) throws ModelObjectException
  129.     {
  130.         if ( locale == null )
  131.         {
  132.             throw new NullPointerException( "locale" );
  133.         }

  134.         synchronized ( this.javaMessagesByLocaleCache )
  135.         {
  136.             MessageFormat javaMessage = this.javaMessagesByLocaleCache.get( locale );

  137.             if ( javaMessage == null && !this.javaMessagesByLocaleCache.containsKey( locale ) )
  138.             {
  139.                 javaMessage = super.getJavaMessage( locale );
  140.                 this.javaMessagesByLocaleCache.put( locale, javaMessage );
  141.             }

  142.             return javaMessage;
  143.         }
  144.     }

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

  151.     public void clear()
  152.     {
  153.         synchronized ( this.javaMessagesByLocaleCache )
  154.         {
  155.             this.javaMessagesByLocaleCache.clear();
  156.         }

  157.         this.gcOrClear( false, true );
  158.     }

  159.     private void gcOrClear( final boolean gc, final boolean clear )
  160.     {
  161.         if ( this.getArguments() instanceof RuntimeModelObject )
  162.         {
  163.             if ( gc )
  164.             {
  165.                 ( (RuntimeModelObject) this.getArguments() ).gc();
  166.             }
  167.             if ( clear )
  168.             {
  169.                 ( (RuntimeModelObject) this.getArguments() ).clear();
  170.             }
  171.         }
  172.         if ( this.getAuthors() instanceof RuntimeModelObject )
  173.         {
  174.             if ( gc )
  175.             {
  176.                 ( (RuntimeModelObject) this.getAuthors() ).gc();
  177.             }
  178.             if ( clear )
  179.             {
  180.                 ( (RuntimeModelObject) this.getAuthors() ).clear();
  181.             }
  182.         }
  183.         if ( this.getDocumentation() instanceof RuntimeModelObject )
  184.         {
  185.             if ( gc )
  186.             {
  187.                 ( (RuntimeModelObject) this.getDocumentation() ).gc();
  188.             }
  189.             if ( clear )
  190.             {
  191.                 ( (RuntimeModelObject) this.getDocumentation() ).clear();
  192.             }
  193.         }
  194.         if ( this.getTemplate() instanceof RuntimeModelObject )
  195.         {
  196.             if ( gc )
  197.             {
  198.                 ( (RuntimeModelObject) this.getTemplate() ).gc();
  199.             }
  200.             if ( clear )
  201.             {
  202.                 ( (RuntimeModelObject) this.getTemplate() ).clear();
  203.             }
  204.         }
  205.     }

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

  225. }