View Javadoc
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    *      https://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  
19  import java.util.Objects;
20  
21  import org.apache.commons.beanutils2.Converter;
22  
23  /**
24   * <p>
25   * Provides a facade for {@link Converter} implementations preventing access to any public API in the implementation, other than that specified by
26   * {@link Converter}.
27   * <p>
28   * This implementation can be used to prevent registered {@link Converter} implementations that provide configuration options from being retrieved and modified.
29   * </p>
30   *
31   * @param <T> The converter type.
32   * @since 1.8.0
33   */
34  public final class ConverterFacade<T> implements Converter<T> {
35  
36      private final Converter<T> converter;
37  
38      /**
39       * Constructs a converter which delegates to the specified {@link Converter} implementation.
40       *
41       * @param converter The converter to delegate to
42       */
43      public ConverterFacade(final Converter<T> converter) {
44          this.converter = Objects.requireNonNull(converter, "converter");
45      }
46  
47      /**
48       * Convert the input object into an output object of the specified type by delegating to the underlying {@link Converter} implementation.
49       *
50       * @param type  Data type to which this value should be converted
51       * @param value The input value to be converted
52       * @return The converted value.
53       */
54      @Override
55      public <R> R convert(final Class<R> type, final Object value) {
56          return converter.convert(type, value);
57      }
58  
59      /**
60       * Provide a String representation of this facade implementation sand the underlying {@link Converter} it delegates to.
61       *
62       * @return A String representation of this facade implementation sand the underlying {@link Converter} it delegates to
63       */
64      @Override
65      public String toString() {
66          return "ConverterFacade[" + converter.toString() + "]";
67      }
68  
69  }