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: RuntimeTexts.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.util.Map; |
39 | import javax.xml.bind.annotation.XmlTransient; |
40 | import org.jomc.model.Text; |
41 | import org.jomc.model.Texts; |
42 | import static org.jomc.ri.model.RuntimeModelObjects.createMap; |
43 | |
44 | // SECTION-START[Documentation] |
45 | // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> |
46 | /** |
47 | * Runtime {@code Texts}. |
48 | * |
49 | * <dl> |
50 | * <dt><b>Identifier:</b></dt><dd>org.jomc.ri.model.RuntimeTexts</dd> |
51 | * <dt><b>Name:</b></dt><dd>JOMC RI RuntimeTexts</dd> |
52 | * <dt><b>Specifications:</b></dt> |
53 | * <dd>org.jomc.ri.model.RuntimeModelObject @ 1.2</dd> |
54 | * <dt><b>Abstract:</b></dt><dd>No</dd> |
55 | * <dt><b>Final:</b></dt><dd>No</dd> |
56 | * <dt><b>Stateless:</b></dt><dd>No</dd> |
57 | * </dl> |
58 | * |
59 | * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.2 |
60 | * @version 1.2 |
61 | */ |
62 | // </editor-fold> |
63 | // SECTION-END |
64 | // SECTION-START[Annotations] |
65 | // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> |
66 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" ) |
67 | // </editor-fold> |
68 | // SECTION-END |
69 | public class RuntimeTexts extends Texts implements RuntimeModelObject |
70 | { |
71 | // SECTION-START[RuntimeTexts] |
72 | |
73 | /** Cache map. */ |
74 | @XmlTransient |
75 | private transient final Map<String, Text> textsByLanguageCache = createMap(); |
76 | |
77 | /** |
78 | * Creates a new {@code RuntimeTexts} instance by deeply copying a given {@code Texts} instance. |
79 | * |
80 | * @param texts The instance to copy. |
81 | * |
82 | * @throws NullPointerException if {@code texts} is {@code null}. |
83 | */ |
84 | public RuntimeTexts( final Texts texts ) |
85 | { |
86 | super( texts ); |
87 | this.copyTexts(); |
88 | } |
89 | |
90 | /** |
91 | * Gets a text for a given language from the list of texts. |
92 | * <p>This method queries an internal cache for a result object to return for the given argument values. If no |
93 | * cached result object is available, this method queries the super-class for a result object to return and caches |
94 | * the outcome of that query for use on successive calls.</p> |
95 | * <p><b>Note:</b><br/>Method {@code clear()} must be used to synchronize the state of the internal cache with the |
96 | * state of the instance, should the state of the instance change.</p> |
97 | * |
98 | * @param language The language of the text to return. |
99 | * |
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.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" ) |
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 | } |