001// Copyright 2006, 2007 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.util;
016
017import java.util.concurrent.atomic.AtomicReference;
018
019/**
020 * An object that holds some type of other object. This is useful for communicating information from an inner class
021 * (used as a closure) to the containing method. This is similar to {@link AtomicReference}, except that it is simpler
022 * but <strong>not</strong> thread safe.
023 *
024 * @param <T> the type being holded.
025 */
026public class Holder<T>
027{
028    private T held;
029
030    public void put(T object)
031    {
032        held = object;
033    }
034
035    public T get()
036    {
037        return held;
038    }
039
040    public boolean hasValue()
041    {
042        return held != null;
043    }
044
045    public static <T> Holder<T> create()
046    {
047        return new Holder<T>();
048    }
049
050    public static <T> Holder<T> create(T initial)
051    {
052        Holder<T> result = create();
053        result.put(initial);
054
055        return result;
056    }
057}