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