001// Copyright 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.services.impl;
016
017import org.apache.hivemind.Location;
018import org.apache.hivemind.internal.Module;
019import org.apache.hivemind.schema.Translator;
020
021/**
022 * An encapsulation of an invocation of
023 * {@link org.apache.hivemind.schema.Translator#translate(org.apache.hivemind.internal.Module, java.lang.Class, java.lang.String, org.apache.hivemind.Location)},
024 * allowing the actual invocation (and all the object creation, etc., that entails) to be deferred,
025 * or even avoided all together.
026 * 
027 * @author Howard M. Lewis Ship
028 * @since 4.0
029 */
030public class DeferredObjectImpl implements DeferredObject
031{
032    private final Module _module;
033
034    private final Translator _objectTranslator;
035
036    private final String _objectReference;
037
038    private final Location _location;
039
040    private Object _object;
041
042    public DeferredObjectImpl(final Translator objectTranslator, final Module module,
043            final String objectReference, final Location location)
044    {
045        _objectTranslator = objectTranslator;
046        _module = module;
047        _objectReference = objectReference;
048        _location = location;
049    }
050
051    public synchronized Object getObject()
052    {
053        if (_object == null)
054            _object = _objectTranslator.translate(
055                    _module,
056                    Object.class,
057                    _objectReference,
058                    _location);
059
060        return _object;
061    }
062
063    public Location getLocation()
064    {
065        return _location;
066    }
067}