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.wicket.util.convert;
018
019import java.util.Date;
020import java.util.Locale;
021
022import org.apache.wicket.util.io.IClusterable;
023
024
025/**
026 * Converts input to output and vice versa. Converters are needed in web applications because we
027 * have to switch between Java objects on the server and Strings in the browser output and input.
028 * <p>
029 * Output conversion, which is handled by {@link #convertToString(Object, Locale)}, is typically
030 * used by components when they render, so that a date can be displayed as '12/12/2007'. Input
031 * conversion, handled by {@link #convertToObject(String, Locale)}, is typically used by form
032 * components to interpret incoming values Such values are strings as they are send as request
033 * parameters from browsers. An incoming value could be the string '12/12/2007' which could be
034 * translated to a corresponding {@link Date} object.
035 * 
036 * Notice that incoming value, when used by a FormComponent, will never be null because before
037 * validation form components perform the required (see FormComponent.isRequired()) check which
038 * errors out on null values. In the case the FormComponent is not required and the user enters a
039 * null value converters will not be invoked because no type conversion is necessary.
040 * </p>
041 * 
042 * See org.apache.wicket.Component#getConverter(Class)
043 * See org.apache.wicket.Application#getConverterLocator()
044 *
045 * @author Eelco Hillenius
046 * @author Jonathan Locke
047 * 
048 * @param <C>
049 *            The object to convert from and to String
050 */
051public interface IConverter<C> extends IClusterable
052{
053        /**
054         * Converts the given {@link String} value
055         * 
056         * @param value
057         *            The string value to convert
058         * @param locale
059         *            The locale used to convert the value
060         * @return The converted value
061         * @throws ConversionException
062         *             if value could not be converted
063         */
064        C convertToObject(String value, Locale locale) throws ConversionException;
065
066        /**
067         * Converts the given value to a string.
068         * 
069         * @param value
070         *            The value to convert
071         * @param locale
072         *            The locale used to convert the value
073         * 
074         * @return The converted string value
075         */
076        String convertToString(C value, Locale locale);
077}