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.juneau.utils;
018
019import org.apache.juneau.*;
020import org.apache.juneau.commons.conversion.*;
021
022/**
023 * Generic object converter implementation.
024 *
025 * <p>
026 * A simple implementation of the {@link Converter} interface that delegates to the default
027 * {@link BeanContext} session for type conversion.
028 *
029 * <p>
030 * This converter provides a convenient way to convert objects between different types using
031 * the same conversion logic that's used throughout the Juneau framework.
032 *
033 * <h5 class='section'>Example:</h5>
034 * <p class='bjava'>
035 *    <jc>// Convert a string to an integer</jc>
036 *    Integer <jv>result</jv> = GenericConverter.INSTANCE.convertTo(Integer.<jk>class</jk>, <js>"123"</js>);
037 *    <jc>// result = 123</jc>
038 *
039 *    <jc>// Convert a map to a bean</jc>
040 *    MyBean <jv>bean</jv> = GenericConverter.INSTANCE.convertTo(MyBean.<jk>class</jk>, <jv>map</jv>);
041 * </p>
042 *
043 * <h5 class='section'>Thread Safety:</h5>
044 * <p>
045 * This class is thread-safe. The singleton instance can be safely shared across multiple threads.
046 *
047 * <h5 class='section'>See Also:</h5><ul>
048 *    <li class='jm'>{@link Converter}
049 *    <li class='jm'>{@link BeanContext#DEFAULT_SESSION}
050 *    <li class='jm'>{@link BeanSession#convertToType(Object, Class)}
051 * </ul>
052 */
053public class GenericConverter implements Converter {
054
055   /**
056    * Singleton instance of the generic converter.
057    *
058    * <p>
059    * This instance can be safely shared across multiple threads and reused for all conversion operations.
060    */
061   public static final GenericConverter INSTANCE = new GenericConverter();
062
063   /**
064    * Converts the specified object to the specified type.
065    *
066    * <p>
067    * This method delegates to the default {@link BeanContext} session for the actual conversion logic.
068    * It supports all the same conversion types that are supported by the framework's bean conversion system.
069    *
070    * <p>
071    * Supported conversions include:
072    * <ul>
073    *    <li>Primitive types and their wrapper classes
074    *    <li>String to Number conversions
075    *    <li>Map to Bean conversions
076    *    <li>Collection to Array conversions
077    *    <li>Enum conversions
078    *    <li>Object swap conversions
079    *    <li>And many more...
080    * </ul>
081    *
082    * @param <T> The target type to convert to.
083    * @param type The target class type.
084    * @param o The object to convert.
085    * @return The converted object, or <jk>null</jk> if the input object is <jk>null</jk>.
086    * @throws InvalidDataConversionException If the object cannot be converted to the specified type.
087    */
088   @Override
089   public <T> T convertTo(Class<T> type, Object o) {
090      return BeanContext.DEFAULT_SESSION.convertToType(o, type);
091   }
092}