View Javadoc

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