Mercurial Hosting > nabble
comparison 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 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:7ecd1a4ef557 |
|---|---|
| 1 package nabble.model; | |
| 2 | |
| 3 import org.slf4j.Logger; | |
| 4 import org.slf4j.LoggerFactory; | |
| 5 | |
| 6 import java.util.Map; | |
| 7 | |
| 8 | |
| 9 abstract class AbstractType<T extends AbstractType> { | |
| 10 | |
| 11 private static final Logger logger = LoggerFactory.getLogger(AbstractType.class); | |
| 12 | |
| 13 private final char code; | |
| 14 private final String name; | |
| 15 | |
| 16 @SuppressWarnings("unchecked") | |
| 17 AbstractType( Map<Character,T> map, char code, String name ) { | |
| 18 this.code = code; | |
| 19 this.name = name; | |
| 20 if( map.put(code,(T)this) != null ) | |
| 21 throw new RuntimeException("duplicate code: "+code); | |
| 22 } | |
| 23 | |
| 24 public final char getCode() { | |
| 25 return code; | |
| 26 } | |
| 27 | |
| 28 public final String getName() { | |
| 29 return name; | |
| 30 } | |
| 31 | |
| 32 public String toString() { | |
| 33 return getClass().getSimpleName()+"-"+getName(); | |
| 34 } | |
| 35 | |
| 36 static <T> T getType( Map<Character,T> map, char code ) { | |
| 37 T t = map.get(code); | |
| 38 if( t==null ) { | |
| 39 //throw new RuntimeException("code="+code); | |
| 40 logger.warn("Invalid code="+code); | |
| 41 } | |
| 42 return t; | |
| 43 } | |
| 44 } |
