View Javadoc
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  
38  import java.text.MessageFormat;
39  import java.util.Locale;
40  import java.util.Map;
41  import javax.xml.bind.annotation.XmlTransient;
42  import org.jomc.model.Message;
43  import org.jomc.model.ModelObjectException;
44  import static org.jomc.ri.model.RuntimeModelObjects.createMap;
45  
46  // SECTION-START[Documentation]
47  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
48  /**
49   * Runtime {@code Message}.
50   *
51   * <dl>
52   *   <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeMessage</dd>
53   *   <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeMessage</dd>
54   *   <dt><b>Specifications:</b></dt>
55   *     <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd>
56   *   <dt><b>Abstract:</b></dt><dd>No</dd>
57   *   <dt><b>Final:</b></dt><dd>No</dd>
58   *   <dt><b>Stateless:</b></dt><dd>No</dd>
59   * </dl>
60   *
61   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2
62   * @version 1.2
63   */
64  // </editor-fold>
65  // SECTION-END
66  // SECTION-START[Annotations]
67  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
68  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
69  // </editor-fold>
70  // SECTION-END
71  public class RuntimeMessage extends Message implements RuntimeModelObject
72  {
73      // SECTION-START[RuntimeMessage]
74  
75      /**
76       * Java messages by locale cache.
77       */
78      @XmlTransient
79      private final Map<Locale, MessageFormat> javaMessagesByLocaleCache = createMap();
80  
81      /**
82       * Creates a new {@code RuntimeMessage} instance by deeply copying a given {@code Message} instance.
83       *
84       * @param message The instance to copy.
85       *
86       * @throws NullPointerException if {@code message} is {@code null}.
87       */
88      public RuntimeMessage( final Message message )
89      {
90          super( message );
91  
92          if ( this.getArguments() != null )
93          {
94              this.setArguments( RuntimeModelObjects.getInstance().copyOf( this.getArguments() ) );
95          }
96          if ( this.getAuthors() != null )
97          {
98              this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
99          }
100         if ( this.getDocumentation() != null )
101         {
102             this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
103         }
104         if ( this.getTemplate() != null )
105         {
106             this.setTemplate( RuntimeModelObjects.getInstance().copyOf( this.getTemplate() ) );
107         }
108     }
109 
110     /**
111      * Gets a Java {@code MessageFormat} instance for a given locale.
112      * <p>
113      * This method queries an internal cache for a result object to return for the given argument values. If no
114      * cached result object is available, this method queries the super-class for a result object to return and caches
115      * the outcome of that query for use on successive calls.
116      * </p>
117      * <p>
118      * <b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the
119      * state of the instance, should the state of the instance change.
120      * </p>
121      *
122      * @param locale The locale to get a Java {@code MessageFormat} instance for.
123      *
124      * @return A Java {@code MessageFormat} instance for {@code locale}.
125      *
126      * @throws NullPointerException if {@code locale} is {@code null}.
127      * @throws ModelObjectException if compiling the template of the message for {@code locale} to a
128      * {@code MessageFormat} fails.
129      *
130      * @see #getTemplate()
131      * @see #clear()
132      */
133     @Override
134     public MessageFormat getJavaMessage( final Locale locale ) throws ModelObjectException
135     {
136         if ( locale == null )
137         {
138             throw new NullPointerException( "locale" );
139         }
140 
141         synchronized ( this.javaMessagesByLocaleCache )
142         {
143             MessageFormat javaMessage = this.javaMessagesByLocaleCache.get( locale );
144 
145             if ( javaMessage == null && !this.javaMessagesByLocaleCache.containsKey( locale ) )
146             {
147                 javaMessage = super.getJavaMessage( locale );
148                 this.javaMessagesByLocaleCache.put( locale, javaMessage );
149             }
150 
151             return javaMessage;
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.javaMessagesByLocaleCache )
165         {
166             this.javaMessagesByLocaleCache.clear();
167         }
168 
169         this.gcOrClear( false, true );
170     }
171 
172     private void gcOrClear( final boolean gc, final boolean clear )
173     {
174         if ( this.getArguments() instanceof RuntimeModelObject )
175         {
176             if ( gc )
177             {
178                 ( (RuntimeModelObject) this.getArguments() ).gc();
179             }
180             if ( clear )
181             {
182                 ( (RuntimeModelObject) this.getArguments() ).clear();
183             }
184         }
185         if ( this.getAuthors() instanceof RuntimeModelObject )
186         {
187             if ( gc )
188             {
189                 ( (RuntimeModelObject) this.getAuthors() ).gc();
190             }
191             if ( clear )
192             {
193                 ( (RuntimeModelObject) this.getAuthors() ).clear();
194             }
195         }
196         if ( this.getDocumentation() instanceof RuntimeModelObject )
197         {
198             if ( gc )
199             {
200                 ( (RuntimeModelObject) this.getDocumentation() ).gc();
201             }
202             if ( clear )
203             {
204                 ( (RuntimeModelObject) this.getDocumentation() ).clear();
205             }
206         }
207         if ( this.getTemplate() instanceof RuntimeModelObject )
208         {
209             if ( gc )
210             {
211                 ( (RuntimeModelObject) this.getTemplate() ).gc();
212             }
213             if ( clear )
214             {
215                 ( (RuntimeModelObject) this.getTemplate() ).clear();
216             }
217         }
218     }
219 
220     // SECTION-END
221     // SECTION-START[Constructors]
222     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
223     /** Creates a new {@code RuntimeMessage} 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 RuntimeMessage()
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 }