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

org.solovyev.common.text.EnumMapper Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
/*
 * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
 * For more information, please, contact [email protected]
 * or visit http://se.solovyev.org
 */

package org.solovyev.common.text;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * User: serso
 * Date: 12/25/11
 * Time: 1:17 PM
 */
public class EnumMapper implements Mapper {

	@NotNull
	private final Class enumClass;

	public EnumMapper(@NotNull Class enumClass) {
		this.enumClass = enumClass;
	}

	public static  Mapper newInstance(@NotNull Class enumClass) {
		return new EnumMapper(enumClass);
	}

	@Override
	public String formatValue(@Nullable T value) throws IllegalArgumentException {
		return value == null ? null : value.name();
	}

	@Override
	public T parseValue(@Nullable String value) throws IllegalArgumentException {
		return value == null ? null : (T)Enum.valueOf(enumClass, value);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy