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: RuntimeTexts.java 4381 2012-03-04 19:29:29Z schulte2005 $ 032 * 033 */ 034 // </editor-fold> 035 // SECTION-END 036 package org.jomc.ri.model; 037 038 import java.util.Map; 039 import javax.xml.bind.annotation.XmlTransient; 040 import org.jomc.model.Text; 041 import org.jomc.model.Texts; 042 import static org.jomc.ri.model.RuntimeModelObjects.createMap; 043 044 // SECTION-START[Documentation] 045 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> 046 /** 047 * Runtime {@code Texts}. 048 * 049 * <dl> 050 * <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeTexts</dd> 051 * <dt><b>Name:</b></dt><dd>JOMC RI RuntimeTexts</dd> 052 * <dt><b>Specifications:</b></dt> 053 * <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd> 054 * <dt><b>Abstract:</b></dt><dd>No</dd> 055 * <dt><b>Final:</b></dt><dd>No</dd> 056 * <dt><b>Stateless:</b></dt><dd>No</dd> 057 * </dl> 058 * 059 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.2 060 * @version 1.2 061 */ 062 // </editor-fold> 063 // SECTION-END 064 // SECTION-START[Annotations] 065 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> 066 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" ) 067 // </editor-fold> 068 // SECTION-END 069 public class RuntimeTexts extends Texts implements RuntimeModelObject 070 { 071 // SECTION-START[RuntimeTexts] 072 073 /** Cache map. */ 074 @XmlTransient 075 private transient final Map<String, Text> textsByLanguageCache = createMap(); 076 077 /** 078 * Creates a new {@code RuntimeTexts} instance by deeply copying a given {@code Texts} instance. 079 * 080 * @param texts The instance to copy. 081 * 082 * @throws NullPointerException if {@code texts} is {@code null}. 083 */ 084 public RuntimeTexts( final Texts texts ) 085 { 086 super( texts ); 087 this.copyTexts(); 088 } 089 090 /** 091 * Gets a text for a given language from the list of texts. 092 * <p>This method queries an internal cache for a result object to return for the given argument values. If no 093 * cached result object is available, this method queries the super-class for a result object to return and caches 094 * the outcome of that query for use on successive calls.</p> 095 * <p><b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the 096 * state of the instance, should the state of the instance change.</p> 097 * 098 * @param language The language of the text to return. 099 * 100 * @return The first matching text or the default text, if no such text is found. 101 * 102 * @throws NullPointerException if {@code language} is {@code null}. 103 * 104 * @see #getText() 105 * @see #getDefaultLanguage() 106 * @see Text#getLanguage() 107 * @see #clear() 108 */ 109 @Override 110 public Text getText( final String language ) 111 { 112 if ( language == null ) 113 { 114 throw new NullPointerException( "language" ); 115 } 116 117 synchronized ( this.textsByLanguageCache ) 118 { 119 Text t = this.textsByLanguageCache.get( language ); 120 121 if ( t == null && !this.textsByLanguageCache.containsKey( language ) ) 122 { 123 t = super.getText( language ); 124 this.textsByLanguageCache.put( language, t ); 125 } 126 127 return t; 128 } 129 } 130 131 private void copyTexts() 132 { 133 for ( int i = 0, s0 = this.getText().size(); i < s0; i++ ) 134 { 135 final Text t = this.getText().get( i ); 136 this.getText().set( i, RuntimeModelObjects.getInstance().copyOf( t ) ); 137 } 138 } 139 140 // SECTION-END 141 // SECTION-START[RuntimeModelObject] 142 public void gc() 143 { 144 this.gcOrClear( true, false ); 145 } 146 147 public void clear() 148 { 149 synchronized ( this.textsByLanguageCache ) 150 { 151 this.textsByLanguageCache.clear(); 152 } 153 154 this.gcOrClear( false, true ); 155 } 156 157 private void gcOrClear( final boolean gc, final boolean clear ) 158 { 159 this.gcOrClearTexts( gc, clear ); 160 } 161 162 private void gcOrClearTexts( final boolean gc, final boolean clear ) 163 { 164 for ( int i = 0, s0 = this.getText().size(); i < s0; i++ ) 165 { 166 final Text t = this.getText().get( i ); 167 if ( t instanceof RuntimeModelObject ) 168 { 169 if ( gc ) 170 { 171 ( (RuntimeModelObject) t ).gc(); 172 } 173 if ( clear ) 174 { 175 ( (RuntimeModelObject) t ).clear(); 176 } 177 } 178 } 179 } 180 // SECTION-END 181 // SECTION-START[Constructors] 182 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> 183 /** Creates a new {@code RuntimeTexts} instance. */ 184 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" ) 185 public RuntimeTexts() 186 { 187 // SECTION-START[Default Constructor] 188 super(); 189 // SECTION-END 190 } 191 // </editor-fold> 192 // SECTION-END 193 // SECTION-START[Dependencies] 194 // SECTION-END 195 // SECTION-START[Properties] 196 // SECTION-END 197 // SECTION-START[Messages] 198 // SECTION-END 199 }