001// Copyright 2007, 2008 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.services;
016
017import org.apache.tapestry5.ValueEncoder;
018import org.apache.tapestry5.services.ValueEncoderFactory;
019
020/**
021 * An implementation of {@link ValueEncoderFactory} that returns a pre-wired instance of {@link ValueEncoder}. This is
022 * odd for a factory, because it doesn't actually create the returned instance, just stores it until the encoder is
023 * needed.
024 *
025 * @param <V> the type of the value.
026 */
027public class GenericValueEncoderFactory<V> implements ValueEncoderFactory<V>
028{
029    private final ValueEncoder<V> encoder;
030
031    public GenericValueEncoderFactory(ValueEncoder<V> encoder)
032    {
033        this.encoder = encoder;
034    }
035
036    public ValueEncoder<V> create(Class<V> type)
037    {
038        return encoder;
039    }
040
041    public static <V> GenericValueEncoderFactory<V> create(ValueEncoder<V> encoder)
042    {
043        return new GenericValueEncoderFactory<V>(encoder);
044    }
045}