View Javadoc

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