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.xbean.propertyeditor;
018
019import java.beans.PropertyEditorSupport;
020
021/**
022 * A base class for converters.  This class handles all converter methods, and redirects all conversion requests to
023 * toStringImpl and toObjectImpl.  These methods can assume that the supplied value or text is never null, and that
024 * type checking has been applied to the value.
025 *
026 * @version $Rev: 6680 $
027 */
028public abstract class AbstractConverter extends PropertyEditorSupport implements Converter {
029    private final Class type;
030    private final boolean trim;
031
032    /**
033     * Creates an abstract converter for the specified type.
034     *
035     * @param type type of the property editor
036     */
037    protected AbstractConverter(Class type) {
038        this(type, true);
039    }
040    
041    protected AbstractConverter(Class type, boolean trim) {
042        super();
043        if (type == null) throw new NullPointerException("type is null");
044        this.type = type;
045        this.trim = trim;
046    }
047
048    public final Class getType() {
049        return type;
050    }
051
052    public final String getAsText() {
053        Object value = super.getValue();
054        String text = toString(value);
055        return text;
056    }
057
058    public final void setAsText(String text) {
059        Object value = toObject((trim) ? text.trim() : text);
060        super.setValue(value);
061    }
062
063    public final Object getValue() {
064        Object value = super.getValue();
065        return value;
066    }
067
068    public final void setValue(Object value) {
069        // Don't validate the type. Type validation is not required by spec and some setters (e.g. Spring) expect this.
070        super.setValue(value);
071    }
072
073    public final String toString(Object value) {
074        if (value == null) {
075            return null;
076        }
077        // Don't validate the type. Type validation is not required by spec and some setters (e.g. Spring) expect this.
078        return toStringImpl(value);
079    }
080
081    public final Object toObject(String text) {
082        if (text == null) {
083            return null;
084        }
085
086        Object value = toObjectImpl((trim) ? text.trim() : text);
087        return value;
088    }
089
090    /**
091     * Converts the supplied object to text.  The supplied object will always be an instance of the editor type, and
092     * specifically will never be null or a String (unless this is the String editor).
093     *
094     * @param value an instance of the editor type
095     * @return the text equivalent of the value
096     */
097    protected String toStringImpl(Object value) {
098        String text = value.toString();
099        return text;
100    }
101
102    /**
103     * Converts the supplied text in to an instance of the editor type.  The text will never be null, and trim() will
104     * already have been called.
105     *
106     * @param text the text to convert
107     * @return an instance of the editor type
108     */
109    protected abstract Object toObjectImpl(String text);
110}