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.camel.spi;
018
019import java.util.List;
020
021import org.apache.camel.LoggingLevel;
022import org.apache.camel.StaticService;
023import org.apache.camel.TypeConverter;
024import org.apache.camel.TypeConverterExists;
025import org.apache.camel.TypeConverters;
026
027/**
028 * Registry for type converters.
029 * <p/>
030 * The utilization {@link Statistics} is by default disabled, as it has a slight performance impact under very high
031 * concurrent load. The statistics can be enabled using {@link Statistics#setStatisticsEnabled(boolean)} method.
032 */
033public interface TypeConverterRegistry extends StaticService {
034
035    /**
036     * Utilization statistics of the this registry.
037     */
038    interface Statistics {
039
040        /**
041         * Number of noop attempts (no type conversion was needed)
042         */
043        long getNoopCounter();
044
045        /**
046         * Number of type conversion attempts
047         */
048        long getAttemptCounter();
049
050        /**
051         * Number of successful conversions
052         */
053        long getHitCounter();
054
055        /**
056         * Number of attempts which cannot be converted as no suitable type converter exists
057         */
058        long getMissCounter();
059
060        /**
061         * Number of failed attempts during type conversion
062         */
063        long getFailedCounter();
064
065        /**
066         * Reset the counters
067         */
068        void reset();
069
070        /**
071         * Whether statistics is enabled.
072         */
073        boolean isStatisticsEnabled();
074
075        /**
076         * Sets whether statistics is enabled.
077         *
078         * @param statisticsEnabled <tt>true</tt> to enable
079         */
080        void setStatisticsEnabled(boolean statisticsEnabled);
081    }
082
083    /**
084     * Registers a new type converter.
085     * <p/>
086     * This method may throw {@link org.apache.camel.TypeConverterExistsException} if configured to fail if an existing
087     * type converter already exists
088     *
089     * @param toType        the type to convert to
090     * @param fromType      the type to convert from
091     * @param typeConverter the type converter to use
092     */
093    void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter);
094
095    /**
096     * Removes the type converter
097     *
098     * @param toType        the type to convert to
099     * @param fromType      the type to convert from
100     * @return <tt>true</tt> if removed, <tt>false</tt> if the type converter didn't exist
101     */
102    boolean removeTypeConverter(Class<?> toType, Class<?> fromType);
103
104    /**
105     * Registers all the type converters from the class, each converter must be implemented as a method and annotated with {@link org.apache.camel.Converter}.
106     *
107     * @param typeConverters class which implements the type converters
108     */
109    void addTypeConverters(TypeConverters typeConverters);
110
111    /**
112     * Registers a new fallback type converter
113     *
114     * @param typeConverter the type converter to use
115     * @param canPromote  whether or not the fallback type converter can be promoted to a first class type converter
116     */
117    void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote);
118
119    /**
120     * Performs a lookup for a given type converter.
121     *
122     * @param toType        the type to convert to
123     * @param fromType      the type to convert from
124     * @return the type converter or <tt>null</tt> if not found.
125     */
126    TypeConverter lookup(Class<?> toType, Class<?> fromType);
127
128    /**
129     * Gets a read-only list of the type converter from / to classes
130     *
131     * @return a list containing fromType/toType class names
132     */
133    List<Class<?>[]> listAllTypeConvertersFromTo();
134
135    /**
136     * Sets the injector to be used for creating new instances during type conversions.
137     *
138     * @param injector the injector
139     */
140    void setInjector(Injector injector);
141
142    /**
143     * Gets the injector
144     *
145     * @return the injector
146     */
147    Injector getInjector();
148
149    /**
150     * Gets the utilization statistics of this type converter registry
151     *
152     * @return the utilization statistics
153     */
154    Statistics getStatistics();
155
156    /**
157     * Number of type converters in the registry.
158     *
159     * @return number of type converters in the registry.
160     */
161    int size();
162
163    /**
164     * The logging level to use when logging that a type converter already exists when attempting to add a duplicate type converter.
165     * <p/>
166     * The default logging level is <tt>WARN</tt>
167     */
168    LoggingLevel getTypeConverterExistsLoggingLevel();
169
170    /**
171     * The logging level to use when logging that a type converter already exists when attempting to add a duplicate type converter.
172     * <p/>
173     * The default logging level is <tt>WARN</tt>
174     */
175    void setTypeConverterExistsLoggingLevel(LoggingLevel typeConverterExistsLoggingLevel);
176
177    /**
178     * What should happen when attempting to add a duplicate type converter.
179     * <p/>
180     * The default behavior is to override the existing.
181     */
182    TypeConverterExists getTypeConverterExists();
183
184    /**
185     * What should happen when attempting to add a duplicate type converter.
186     * <p/>
187     * The default behavior is to override the existing.
188     */
189    void setTypeConverterExists(TypeConverterExists typeConverterExists);
190
191}