view src/nabble/model/AbstractType.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source

package nabble.model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


abstract class AbstractType<T extends AbstractType> {

	private static final Logger logger = LoggerFactory.getLogger(AbstractType.class);

	private final char code;
	private final String name;

	@SuppressWarnings("unchecked")
	AbstractType( Map<Character,T> map, char code, String name ) {
		this.code = code;
		this.name = name;
		if( map.put(code,(T)this) != null )
			throw new RuntimeException("duplicate code: "+code);
	}

	public final char getCode() {
		return code;
	}

	public final String getName() {
		return name;
	}

	public String toString() {
		return getClass().getSimpleName()+"-"+getName();
	}

	static <T> T getType( Map<Character,T> map, char code ) {
		T t = map.get(code);
		if( t==null ) {
			//throw new RuntimeException("code="+code);
			logger.warn("Invalid code="+code);
		}
		return t;
	}
}