1 package nl.tudelft.goal.ut2004.translators; 2 3 import java.util.Arrays; 4 import java.util.Set; 5 6 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType; 7 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category; 8 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Group; 9 import eis.eis2java.exception.TranslationException; 10 import eis.eis2java.translation.Java2Parameter; 11 import eis.eis2java.translation.Parameter2Java; 12 import eis.eis2java.translation.Translator; 13 import eis.iilang.Identifier; 14 import eis.iilang.Parameter; 15 16 public class ItemTypeTranslator implements Java2Parameter<ItemType>, Parameter2Java<ItemType> { 17 18 @Override 19 public Parameter[] translate(ItemType o) throws TranslationException { 20 return new Parameter[] { new Identifier(o.getGroup().name().toLowerCase()) }; 21 } 22 23 @Override 24 public Class<? extends ItemType> translatesFrom() { 25 return ItemType.class; 26 } 27 28 @Override 29 public ItemType translate(Parameter parameter) throws TranslationException { 30 String itemTypeString = Translator.getInstance().translate2Java(parameter, String.class); 31 32 try { 33 Group itemGroup = Group.valueOf(itemTypeString.toUpperCase()); 34 Set<ItemType> items = ItemType.GROUPS.get(itemGroup ); 35 for(ItemType item : items){ 36 if(item.getCategory() == Category.WEAPON){ 37 return item; 38 } 39 } 40 41 String message = String.format("%s was not a ItemType in the weapon category.", itemTypeString); 42 throw new TranslationException(message); 43 } catch (IllegalArgumentException e) { 44 String message = String.format("%s was not a ItemType. Expected on off %s.", itemTypeString, Arrays.toString(Group.values())); 45 throw new TranslationException(message, e); 46 } 47 } 48 49 @Override 50 public Class<ItemType> translatesTo() { 51 return ItemType.class; 52 } 53 54 }