    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.commons.beanutils2.converters;
    018
    019import java.net.URL;
    020
    021/**
    022 * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from <strong>java.net.URL</strong> objects.
    023 * <p>
    024 * Can be configured to either return a <em>default value</em> or throw a {@code ConversionException} if a conversion error occurs.
    025 *
    026 * @since 1.3
    027 */
    028public final class URLConverter extends AbstractConverter<URL> {
    029
    030    /**
    031     * Constructs a <strong>java.net.URL</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
    032     */
    033    public URLConverter() {
    034    }
    035
    036    /**
    037     * Constructs a <strong>java.net.URL</strong> <em>Converter</em> that returns a default value if an error occurs.
    038     *
    039     * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
    040     */
    041    public URLConverter(final URL defaultValue) {
    042        super(defaultValue);
    043    }
    044
    045    /**
    046     * <p>
    047     * Converts a java.net.URL or object into a String.
    048     * </p>
    049     *
    050     * @param <T>   Target type of the conversion.
    051     * @param type  Data type to which this value should be converted.
    052     * @param value The input value to be converted.
    053     * @return The converted value.
    054     * @throws Throwable if an error occurs converting to the specified type
    055     * @since 1.8.0
    056     */
    057    @Override
    058    protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
    059        if (URL.class.equals(type)) {
    060            return type.cast(new URL(toString(value)));
    061        }
    062
    063        throw conversionException(type, value);
    064    }
    065
    066    /**
    067     * Gets the default type this {@code Converter} handles.
    068     *
    069     * @return The default type this {@code Converter} handles.
    070     * @since 1.8.0
    071     */
    072    @Override
    073    protected Class<URL> getDefaultType() {
    074        return URL.class;
    075    }
    076
    077}