001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.beanutils2.converters;
020
021import java.awt.Dimension;
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025/**
026 * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from {@link Dimension}.
027 *
028 * <p>
029 * Accepts a single {@link Integer} value, or two {@link Integer} values separated by the character {@code x}.
030 * </p>
031 *
032 * <p>
033 * The dimensions must consist of non-negative {@link Integer} values only.
034 * </p>
035 *
036 * @since 2.0.0
037 */
038public class DimensionConverter extends AbstractConverter<Dimension> {
039
040    /** Pattern used to validate and tokenizer the {@link String}. */
041    private static final Pattern DIMENSION_PATTERN = Pattern.compile("(\\d+)(?:x(\\d+))?");
042
043    /**
044     * Construct a <strong>{@link Dimension}</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
045     */
046    public DimensionConverter() {
047    }
048
049    /**
050     * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return the specified default value if a conversion error occurs.
051     *
052     * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
053     */
054    public DimensionConverter(final Dimension defaultValue) {
055        super(defaultValue);
056    }
057
058    /**
059     * Converts the input object into an output object of the specified type.
060     *
061     * @param type  Data type to which this value should be converted.
062     * @param value String property value to convert.
063     * @return A {@link Dimension} which represents the configuration property value.
064     * @throws NullPointerException  If the value is null.
065     * @throws NumberFormatException If the {@link Dimension} width or height is bigger than {@link Integer#MAX_VALUE}.
066     */
067    @Override
068    protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
069        if (Dimension.class.isAssignableFrom(type)) {
070            final String stringValue = toString(value);
071
072            if (stringValue.isEmpty()) {
073                throw new IllegalArgumentException("Dimensions cannot be empty.");
074            }
075
076            final Matcher matcher = DIMENSION_PATTERN.matcher(stringValue);
077
078            if (!matcher.matches()) {
079                throw new IllegalArgumentException("Dimension doesn't match format: {width/height} or {width}x{height}");
080            }
081
082            final String x = matcher.group(1);
083            final String y = matcher.group(2);
084
085            final int xValue = Integer.parseInt(x);
086            final int yValue = y == null || x.equals(y) ? xValue : Integer.parseInt(y);
087
088            return type.cast(new Dimension(xValue, yValue));
089        }
090
091        throw conversionException(type, value);
092    }
093
094    /**
095     * Gets the default type this {@code Converter} handles.
096     *
097     * @return The default type this {@code Converter} handles.
098     * @since 2.0.0
099     */
100    @Override
101    protected Class<Dimension> getDefaultType() {
102        return Dimension.class;
103    }
104}