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