All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.mmm.util.value.base.AbstractGenericValueConverter Maven / Gradle / Ivy

/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.value.base;

import java.lang.reflect.Type;

import net.sf.mmm.util.component.base.AbstractLoggableComponent;
import net.sf.mmm.util.value.api.GenericValueConverter;
import net.sf.mmm.util.value.api.ValueNotSetException;
import net.sf.mmm.util.value.api.ValueOutOfRangeException;
import net.sf.mmm.util.value.api.WrongValueTypeException;

/**
 * This is the abstract base implementation of the {@link GenericValueConverter} interface.
 *
 * @param  is the generic type of the values to convert.
 *
 * @author Joerg Hohwiller (hohwille at users.sourceforge.net)
 * @since 1.0.1
 */
public abstract class AbstractGenericValueConverter extends AbstractLoggableComponent
    implements GenericValueConverter {

  /**
   * The constructor.
   */
  public AbstractGenericValueConverter() {

    super();
  }

  @Override
  public  TARGET convertValue(SOURCE value, Object valueSource, Class targetClass)
      throws ValueNotSetException, WrongValueTypeException {

    return convertValue(value, valueSource, targetClass, targetClass);
  }

  @Override
  public final  TARGET convertValue(SOURCE value, Object valueSource, Class type, Type targetType,
      TARGET defaultValue) throws WrongValueTypeException {

    if (value == null) {
      return defaultValue;
    } else {
      return convertValue(value, valueSource, type, type);
    }
  }

  @Override
  public  TARGET convertValue(SOURCE value, Object valueSource, Class targetClass,
      TARGET defaultValue) throws WrongValueTypeException {

    return convertValue(value, valueSource, targetClass, targetClass, defaultValue);
  }

  @Override
  public  TARGET convertValue(SOURCE value, Object valueSource, TARGET minimum,
      TARGET maximum, TARGET defaultValue) throws WrongValueTypeException, ValueOutOfRangeException {

    if (defaultValue != null) {
      ValueOutOfRangeException.checkRange((Object) defaultValue, minimum, maximum, valueSource);
    }
    if (value == null) {
      return defaultValue;
    } else {
      return convertValue(value, valueSource, minimum, maximum);
    }
  }

  @Override
  public  TARGET convertValue(SOURCE value, Object valueSource, TARGET minimum,
      TARGET maximum) throws ValueNotSetException, WrongValueTypeException, ValueOutOfRangeException {

    Class targetClass = minimum.getClass();
    @SuppressWarnings("unchecked")
    TARGET result = (TARGET) convertValue(value, valueSource, targetClass, targetClass);
    ValueOutOfRangeException.checkRange((Object) result, minimum, maximum, valueSource);
    return result;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy