001// Copyright 2004, 2005 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.tapestry.bean;
016
017import org.apache.tapestry.IBinding;
018
019/**
020 *  A helper bean to assist with providing defaults for unspecified
021 *  parameters.    It is initalized
022 *  with an {@link IBinding} and a default value.  It's value property
023 *  is either the value of the binding, but if the binding is null,
024 *  or the binding returns null, the default value is returned.
025 *
026 *  @author Howard Lewis Ship
027 *  @since 1.0.5
028 * 
029 **/
030
031public class Default
032{
033    private IBinding binding;
034    private Object defaultValue;
035
036    public void resetForPool()
037    {
038        binding = null;
039        defaultValue = null;
040    }
041
042    public void setBinding(IBinding value)
043    {
044        binding = value;
045    }
046
047    public IBinding getBinding()
048    {
049        return binding;
050    }
051
052    public void setDefaultValue(Object value)
053    {
054        defaultValue = value;
055    }
056
057    public Object getDefaultValue()
058    {
059        return defaultValue;
060    }
061
062    /**
063     *  Returns the value of the binding.  However, if the binding is null, or the binding
064     *  returns null, then the defaultValue is returned instead.
065     *
066     **/
067
068    public Object getValue()
069    {
070        if (binding == null)
071            return defaultValue;
072
073        Object value = binding.getObject();
074
075        if (value == null)
076            return defaultValue;
077
078        return value;
079    }
080    
081    /** @since 3.0 **/
082    
083    public void discardFromPool()
084    {
085    }
086
087}