001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2020 microBean™.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.settings.converter;
018
019import java.beans.PropertyEditor;
020import java.beans.PropertyEditorManager;
021
022import java.io.IOException;
023import java.io.ObjectInputStream;
024
025import java.util.Objects;
026
027import javax.enterprise.inject.Vetoed;
028
029import org.microbean.settings.Converter;
030import org.microbean.settings.Value;
031
032@Vetoed
033public class PropertyEditorConverter<T> implements Converter<T> {
034
035  private static final long serialVersionUID = 1L;
036
037  private final Class<?> conversionClass;
038
039  private transient PropertyEditor editor;
040
041  public PropertyEditorConverter(final Class<?> conversionClass) {
042    this(conversionClass, null);
043  }
044  
045  public PropertyEditorConverter(final Class<?> conversionClass, final PropertyEditor editor) {
046    super();
047    this.conversionClass = Objects.requireNonNull(conversionClass);
048    if (editor == null) {
049      this.editor = PropertyEditorManager.findEditor(conversionClass);
050    } else {
051      this.editor = editor;
052    }
053  }
054
055  @Override
056  public T convert(final Value value) {
057    final T returnValue;
058    if (value == null) {
059      returnValue = null;
060    } else {
061      final String stringValue = value.get();
062      if (this.editor == null) {
063        throw new IllegalArgumentException("No PropertyEditor available to convert " + stringValue);
064      } else {
065        synchronized (this.editor) {
066          editor.setAsText(stringValue);
067          T result = null;
068          try {
069            @SuppressWarnings("unchecked")
070            final T temp = (T)editor.getValue();
071            result = temp;
072          } catch (final ClassCastException classCastException) {
073            throw new IllegalArgumentException(stringValue, classCastException);
074          } finally {
075            returnValue = result;
076          }
077        }
078      }
079    }
080    return returnValue;
081  }
082
083  private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
084    if (in != null) {
085      in.defaultReadObject();
086      this.editor = PropertyEditorManager.findEditor(this.conversionClass);
087    }
088  }
089
090}