View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor;
2   
3   import java.lang.reflect.Field;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
9   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemTypeTranslator;
10  import cz.cuni.amis.utils.ClassUtils;
11  
12  /**
13   * A parent of all item descriptors.
14   * 
15   * Contains attributes common for all descriptors plus some common functions.
16   * 
17   * @author Ondrej, knight
18   */
19  public class ItemDescriptor {
20  	
21  	
22  	public static final ItemDescriptor NONE = new ItemDescriptor();
23  
24      @ItemDescriptorField
25      private int amount;
26      @ItemDescriptorField
27      private String inventoryType;
28      @ItemDescriptorField
29      private ItemType pickupType;
30      @ItemDescriptorField
31      private ItemType.Category itemCategory;
32  
33      public int getAmount() {
34          return amount;
35      }
36  
37      public String getInventoryType() {
38          return inventoryType;
39      }
40  
41      public ItemType getPickupType() {
42          return pickupType;
43      }
44  
45      public ItemType.Category getItemCategory() {
46          return itemCategory;
47      }
48  
49      /**
50       * Converts a fully qualified field name (e.g. private boolean cz.cuni.amis.MyClass.myBoolean)
51       * to a field name (e.g. myBoolean).
52       *
53       * @param string - complete identifier of a field in a class
54       * @return name of a field
55       */
56      protected String fieldToName(String string) {
57          String result = string.substring(string.lastIndexOf(".") + 1);
58          return firstCharToUpperCase(result);
59      }
60  
61      protected String firstCharToUpperCase(String input) {
62          return input.substring(0, 1).toUpperCase() + input.substring(1);
63      }
64  
65      /**
66       * This method does the mapping from a map of attributes contained it ITCMsg and the attributes of the descriptor.
67       *
68       * NOTE: names of the attributes must be equal to the keys of the HashMap contained in ITCMsg.
69       *
70       * @param configMsg
71       */
72      protected void doReflexion(Object configMsg, Class<? extends ItemDescriptor> clazz) {
73          Field[] configMsgFields = configMsg.getClass().getDeclaredFields();
74          Map<String, Field> configMsgFieldsMap = new HashMap<String, Field>();
75          for (Field field : configMsgFields) {
76              configMsgFieldsMap.put(field.getName(), field);
77          }
78          List<Field> descFields = ClassUtils.getAllFields(clazz, false);
79          for (Field descField : descFields) {
80              if (!descField.isAnnotationPresent(ItemDescriptorField.class)) {
81                  continue;
82              }
83              synchronized(descField) {
84  	            String key = fieldToName(descField.toString());
85  	            Field configField = configMsgFieldsMap.get(key);
86  	            synchronized(configField) {
87  		            try {
88  		            	descField.setAccessible(true);
89  		                if (configField == null) {
90  		                	descField.set(this, null);
91  		                } else {
92  		                    configField.setAccessible(true);
93  		                    descField.set(this, configField.get(configMsg));
94  		                    configField.setAccessible(false);
95  		                }
96  		            } catch (IllegalArgumentException e) {
97  		                e.printStackTrace();
98  		            } catch (IllegalAccessException e) {
99  		                e.printStackTrace();
100 		            } finally {
101 		                descField.setAccessible(false);
102 		            }
103 	            }
104             }
105         }
106     }
107 
108     @Override
109     public String toString() {
110         return new String("ITEM DESCRIPTOR for PickupType: " + pickupType + ", Inv.Type: " + inventoryType
111                 + ", Amount: " + amount + ", category: " + itemCategory.toString());
112     }
113 }