View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.beanutils2.converters;
20  
21  import java.awt.Point;
22  import java.util.regex.Pattern;
23  
24  /**
25   * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from {@link Point}.
26   *
27   * @since 2.0.0
28   */
29  public class PointConverter extends AbstractConverter<Point> {
30  
31      /** Pattern used to split the {@link Point#x} and {@link Point#y} coordinate. */
32      private static final Pattern POINT_SPLIT = Pattern.compile("\\s*,\\s*");
33  
34      /**
35       * Construct a <strong>{@link Point}</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
36       */
37      public PointConverter() {
38      }
39  
40      /**
41       * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return the specified default value if a conversion error occurs.
42       *
43       * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
44       */
45      public PointConverter(final Point defaultValue) {
46          super(defaultValue);
47      }
48  
49      /**
50       * Converts the specified input object into an output object of the specified type.
51       *
52       * @param type  Data type to which this value should be converted.
53       * @param value The {@link String} property value to convert.
54       * @return A {@link Point} represented by the x and y coordinate of the input.
55       * @throws NullPointerException     If the value is null.
56       * @throws IllegalArgumentException If the configuration value is an invalid representation of a {@link Point}.
57       * @throws NumberFormatException    If a one of coordinates cannot be parsed to an {@link Integer}.
58       */
59      @Override
60      protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
61          if (Point.class.isAssignableFrom(type)) {
62              final String stringValue = toString(value);
63  
64              if (stringValue.isEmpty()) {
65                  throw new IllegalArgumentException("A point cannot be empty.");
66              }
67  
68              final int lastCharIndex = stringValue.length() - 1;
69  
70              if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') {
71                  throw new IllegalArgumentException("Point coordinates must be enclosed in brackets.");
72              }
73  
74              final String coordinates = stringValue.substring(1, lastCharIndex);
75              final String[] xy = POINT_SPLIT.split(coordinates);
76  
77              if (xy.length != 2) {
78                  throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " + "expecting the following format: (40, 200)");
79              }
80  
81              final int x = Integer.parseInt(xy[0]);
82              final int y = Integer.parseInt(xy[1]);
83              return type.cast(new Point(x, y));
84          }
85  
86          throw conversionException(type, value);
87      }
88  
89      /**
90       * Gets the default type this {@code Converter} handles.
91       *
92       * @return The default type this {@code Converter} handles.
93       * @since 2.0.0
94       */
95      @Override
96      protected Class<Point> getDefaultType() {
97          return Point.class;
98      }
99  }