001// SECTION-START[License Header] 002// <editor-fold defaultstate="collapsed" desc=" Generated License "> 003/* 004 * Java Object Management and Configuration 005 * Copyright (C) Christian Schulte, 2011-313 006 * All rights reserved. 007 * 008 * Redistribution and use in source and binary forms, with or without 009 * modification, are permitted provided that the following conditions 010 * are met: 011 * 012 * o Redistributions of source code must retain the above copyright 013 * notice, this list of conditions and the following disclaimer. 014 * 015 * o Redistributions in binary form must reproduce the above copyright 016 * notice, this list of conditions and the following disclaimer in 017 * the documentation and/or other materials provided with the 018 * distribution. 019 * 020 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 021 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 022 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 023 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 024 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 025 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 026 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 027 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 029 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * $JOMC: RuntimeMessage.java 4712 2013-01-02 13:56:12Z schulte $ 032 * 033 */ 034// </editor-fold> 035// SECTION-END 036package org.jomc.ri.model; 037 038import java.text.MessageFormat; 039import java.util.Locale; 040import java.util.Map; 041import javax.xml.bind.annotation.XmlTransient; 042import org.jomc.model.Message; 043import org.jomc.model.ModelObjectException; 044import static org.jomc.ri.model.RuntimeModelObjects.createMap; 045 046// SECTION-START[Documentation] 047// <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> 048/** 049 * Runtime {@code Message}. 050 * 051 * <dl> 052 * <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeMessage</dd> 053 * <dt><b>Name:</b></dt><dd>JOMC ⁑ RI ⁑ RuntimeMessage</dd> 054 * <dt><b>Specifications:</b></dt> 055 * <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd> 056 * <dt><b>Abstract:</b></dt><dd>No</dd> 057 * <dt><b>Final:</b></dt><dd>No</dd> 058 * <dt><b>Stateless:</b></dt><dd>No</dd> 059 * </dl> 060 * 061 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.2 062 * @version 1.2 063 */ 064// </editor-fold> 065// SECTION-END 066// SECTION-START[Annotations] 067// <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> 068@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" ) 069// </editor-fold> 070// SECTION-END 071public class RuntimeMessage extends Message implements RuntimeModelObject 072{ 073 // SECTION-START[RuntimeMessage] 074 075 /** Java messages by locale cache.*/ 076 @XmlTransient 077 private final Map<Locale, MessageFormat> javaMessagesByLocaleCache = createMap(); 078 079 /** 080 * Creates a new {@code RuntimeMessage} instance by deeply copying a given {@code Message} instance. 081 * 082 * @param message The instance to copy. 083 * 084 * @throws NullPointerException if {@code message} is {@code null}. 085 */ 086 public RuntimeMessage( final Message message ) 087 { 088 super( message ); 089 090 if ( this.getArguments() != null ) 091 { 092 this.setArguments( RuntimeModelObjects.getInstance().copyOf( this.getArguments() ) ); 093 } 094 if ( this.getAuthors() != null ) 095 { 096 this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) ); 097 } 098 if ( this.getDocumentation() != null ) 099 { 100 this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) ); 101 } 102 if ( this.getTemplate() != null ) 103 { 104 this.setTemplate( RuntimeModelObjects.getInstance().copyOf( this.getTemplate() ) ); 105 } 106 } 107 108 /** 109 * Gets a Java {@code MessageFormat} instance for a given locale. 110 * <p>This method queries an internal cache for a result object to return for the given argument values. If no 111 * cached result object is available, this method queries the super-class for a result object to return and caches 112 * the outcome of that query for use on successive calls.</p> 113 * <p><b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the 114 * state of the instance, should the state of the instance change.</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 135 synchronized ( this.javaMessagesByLocaleCache ) 136 { 137 MessageFormat javaMessage = this.javaMessagesByLocaleCache.get( locale ); 138 139 if ( javaMessage == null && !this.javaMessagesByLocaleCache.containsKey( locale ) ) 140 { 141 javaMessage = super.getJavaMessage( locale ); 142 this.javaMessagesByLocaleCache.put( locale, javaMessage ); 143 } 144 145 return javaMessage; 146 } 147 } 148 149 // SECTION-END 150 // SECTION-START[RuntimeModelObject] 151 public void gc() 152 { 153 this.gcOrClear( true, false ); 154 } 155 156 public void clear() 157 { 158 synchronized ( this.javaMessagesByLocaleCache ) 159 { 160 this.javaMessagesByLocaleCache.clear(); 161 } 162 163 this.gcOrClear( false, true ); 164 } 165 166 private void gcOrClear( final boolean gc, final boolean clear ) 167 { 168 if ( this.getArguments() instanceof RuntimeModelObject ) 169 { 170 if ( gc ) 171 { 172 ( (RuntimeModelObject) this.getArguments() ).gc(); 173 } 174 if ( clear ) 175 { 176 ( (RuntimeModelObject) this.getArguments() ).clear(); 177 } 178 } 179 if ( this.getAuthors() instanceof RuntimeModelObject ) 180 { 181 if ( gc ) 182 { 183 ( (RuntimeModelObject) this.getAuthors() ).gc(); 184 } 185 if ( clear ) 186 { 187 ( (RuntimeModelObject) this.getAuthors() ).clear(); 188 } 189 } 190 if ( this.getDocumentation() instanceof RuntimeModelObject ) 191 { 192 if ( gc ) 193 { 194 ( (RuntimeModelObject) this.getDocumentation() ).gc(); 195 } 196 if ( clear ) 197 { 198 ( (RuntimeModelObject) this.getDocumentation() ).clear(); 199 } 200 } 201 if ( this.getTemplate() instanceof RuntimeModelObject ) 202 { 203 if ( gc ) 204 { 205 ( (RuntimeModelObject) this.getTemplate() ).gc(); 206 } 207 if ( clear ) 208 { 209 ( (RuntimeModelObject) this.getTemplate() ).clear(); 210 } 211 } 212 } 213 214 // SECTION-END 215 // SECTION-START[Constructors] 216 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> 217 /** Creates a new {@code RuntimeMessage} instance. */ 218 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" ) 219 public RuntimeMessage() 220 { 221 // SECTION-START[Default Constructor] 222 super(); 223 // SECTION-END 224 } 225 // </editor-fold> 226 // SECTION-END 227 // SECTION-START[Dependencies] 228 // SECTION-END 229 // SECTION-START[Properties] 230 // SECTION-END 231 // SECTION-START[Messages] 232 // SECTION-END 233}