001/*
002 * Copyright 2002-2011 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.convert.support;
018
019import java.beans.PropertyEditorSupport;
020
021import org.springframework.core.convert.ConversionService;
022import org.springframework.core.convert.TypeDescriptor;
023import org.springframework.util.Assert;
024
025/**
026 * Adapter that exposes a {@link java.beans.PropertyEditor} for any given
027 * {@link org.springframework.core.convert.ConversionService} and specific target type.
028 *
029 * @author Juergen Hoeller
030 * @since 3.0
031 */
032public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
033
034        private final ConversionService conversionService;
035
036        private final TypeDescriptor targetDescriptor;
037
038        private final boolean canConvertToString;
039
040
041        /**
042         * Create a new ConvertingPropertyEditorAdapter for a given
043         * {@link org.springframework.core.convert.ConversionService}
044         * and the given target type.
045         * @param conversionService the ConversionService to delegate to
046         * @param targetDescriptor the target type to convert to
047         */
048        public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) {
049                Assert.notNull(conversionService, "ConversionService must not be null");
050                Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
051                this.conversionService = conversionService;
052                this.targetDescriptor = targetDescriptor;
053                this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
054        }
055
056
057        @Override
058        public void setAsText(String text) throws IllegalArgumentException {
059                setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor));
060        }
061
062        @Override
063        public String getAsText() {
064                if (this.canConvertToString) {
065                        return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.valueOf(String.class));
066                }
067                else {
068                        return null;
069                }
070        }
071
072}