KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > FutureObject


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 /**
21  * * A multiprocessor-safe wrapper to defer evaluation of an object until it
22  * * is needed.
23  */

24 public abstract class FutureObject {
25     private static final boolean _volatileIsEffectiveMemoryBarrier = MemoryBarrier.USE_VOLATILE;
26
27     private volatile boolean _evaluated = false;
28
29     private Object JavaDoc _value;
30
31     /**
32      * * Sub-classes should override this method to get the object's value
33      * * when it is first needed.
34      */

35     public abstract Object JavaDoc evaluate();
36
37     public final Object JavaDoc getValue() {
38         if (_volatileIsEffectiveMemoryBarrier) {
39             if (!_evaluated) {
40                 synchronized (this) {
41                     if (!_evaluated) {
42                         _value = evaluate();
43                         _evaluated = true;
44                     }
45                 }
46             }
47         } else {
48             synchronized (this) {
49                 if (!_evaluated) {
50                     _value = evaluate();
51                     _evaluated = true;
52                 }
53             }
54         }
55         return _value;
56     }
57 }
58
Popular Tags