001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.beanutils2.converters;
018
019import java.util.Objects;
020
021import org.apache.commons.beanutils2.Converter;
022
023/**
024 * <p>
025 * Provides a facade for {@link Converter} implementations preventing access to any public API in the implementation, other than that specified by
026 * {@link Converter}.
027 * <p>
028 * This implementation can be used to prevent registered {@link Converter} implementations that provide configuration options from being retrieved and modified.
029 * </p>
030 *
031 * @param <T> The converter type.
032 * @since 1.8.0
033 */
034public final class ConverterFacade<T> implements Converter<T> {
035
036    private final Converter<T> converter;
037
038    /**
039     * Constructs a converter which delegates to the specified {@link Converter} implementation.
040     *
041     * @param converter The converter to delegate to
042     */
043    public ConverterFacade(final Converter<T> converter) {
044        this.converter = Objects.requireNonNull(converter, "converter");
045    }
046
047    /**
048     * Convert the input object into an output object of the specified type by delegating to the underlying {@link Converter} implementation.
049     *
050     * @param type  Data type to which this value should be converted
051     * @param value The input value to be converted
052     * @return The converted value.
053     */
054    @Override
055    public <R> R convert(final Class<R> type, final Object value) {
056        return converter.convert(type, value);
057    }
058
059    /**
060     * Provide a String representation of this facade implementation sand the underlying {@link Converter} it delegates to.
061     *
062     * @return A String representation of this facade implementation sand the underlying {@link Converter} it delegates to
063     */
064    @Override
065    public String toString() {
066        return "ConverterFacade[" + converter.toString() + "]";
067    }
068
069}