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