ConverterFacade.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.beanutils2.converters;

  18. import java.util.Objects;

  19. import org.apache.commons.beanutils2.Converter;

  20. /**
  21.  * <p>
  22.  * Provides a facade for {@link Converter} implementations preventing access to any public API in the implementation, other than that specified by
  23.  * {@link Converter}.
  24.  * <p>
  25.  * This implementation can be used to prevent registered {@link Converter} implementations that provide configuration options from being retrieved and modified.
  26.  * </p>
  27.  *
  28.  * @param <T> The converter type.
  29.  * @since 1.8.0
  30.  */
  31. public final class ConverterFacade<T> implements Converter<T> {

  32.     private final Converter<T> converter;

  33.     /**
  34.      * Constructs a converter which delegates to the specified {@link Converter} implementation.
  35.      *
  36.      * @param converter The converter to delegate to
  37.      */
  38.     public ConverterFacade(final Converter<T> converter) {
  39.         this.converter = Objects.requireNonNull(converter, "converter");
  40.     }

  41.     /**
  42.      * Convert the input object into an output object of the specified type by delegating to the underlying {@link Converter} implementation.
  43.      *
  44.      * @param type  Data type to which this value should be converted
  45.      * @param value The input value to be converted
  46.      * @return The converted value.
  47.      */
  48.     @Override
  49.     public <R> R convert(final Class<R> type, final Object value) {
  50.         return converter.convert(type, value);
  51.     }

  52.     /**
  53.      * Provide a String representation of this facade implementation sand the underlying {@link Converter} it delegates to.
  54.      *
  55.      * @return A String representation of this facade implementation sand the underlying {@link Converter} it delegates to
  56.      */
  57.     @Override
  58.     public String toString() {
  59.         return "ConverterFacade[" + converter.toString() + "]";
  60.     }

  61. }