1 /*
2 Copyright (C) 2008 Grid Systems, S.A.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 package com.gridsystems.nextgrid.api.env;
19
20 import java.io.Serializable;
21
22 import nextgrid.api.env.ProcessEnvironment;
23
24 /**
25 * <p>Utility class for environment attributes that do not implement
26 * {@link Serializable}.</p>
27 *
28 * <p>Subclasses can implement logic for creating and configuring the
29 * internal value instance.</p>
30 *
31 * @param <T> Value type
32 * @author Rodrigo Ruiz
33 */
34 public abstract class AbstractTransientAttribute<T> implements Serializable {
35
36 /**
37 * Held value.
38 */
39 private transient T value;
40
41 /**
42 * Creates a new instance.
43 */
44 public AbstractTransientAttribute() {
45 }
46
47 /**
48 * Creates a new instance.
49 *
50 * @param value Initial value
51 */
52 public AbstractTransientAttribute(T value) {
53 this.value = value;
54 }
55
56 /**
57 * Gets the held value.
58 *
59 * @param env For instance creation/initialisation
60 * @return The held value
61 */
62 public synchronized T getValue(ProcessEnvironment env) {
63 if (value == null) {
64 value = createValue(env);
65 }
66 return value;
67 }
68
69 /**
70 * Explicitly sets the value.
71 *
72 * @param value New value
73 */
74 public synchronized void setValue(T value) {
75 this.value = value;
76 }
77
78 /**
79 * Creates a new value instance, and optionally configures it.
80 *
81 * @param env For value configuration
82 * @return Value
83 */
84 protected abstract T createValue(ProcessEnvironment env);
85
86 }