View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.sensor;
2   
3   import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Weapon;
4   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
5   import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType.Category;
6   import cz.cuni.amis.utils.HashCode;
7   import cz.cuni.amis.utils.NullCheck;
8   
9   /**
10   * Class that contains a weapon preference. I.e., stating:
11   * <ol>
12   * <li>weapon to be used</li>
13   * <li>primary/secondary mode</li>
14   * </ol>
15   * 
16   * @author Jimmy
17   */
18  public class WeaponPref {
19  
20  	private ItemType weapon;
21  	private boolean primary;
22  	private int hashCode;
23  
24  	/**
25  	 * Use 'weapon' and the concrete mode.
26  	 * @param weapon
27  	 * @param primary true == use primary firing mode, false == use secondary firing mode
28  	 */
29  	public WeaponPref(ItemType weapon, boolean primary) {
30  		this.weapon = weapon;
31  		this.primary = primary;
32  		NullCheck.check(this.weapon, "weapon");
33  		if (weapon.getCategory() != Category.WEAPON) {
34  			throw new IllegalArgumentException("passed 'weapon' is not of ItemType.Category.WEAPON but " + weapon.getCategory().toString());
35  		}
36  		hashCode = new HashCode().add(weapon).add(primary).getHash();
37  	}
38  	
39  	/**
40  	 * Use 'weapon' with primary-firing-mode.
41  	 * @param weapon
42  	 * @param primary true == use primary firing mode, false == use secondary firing mode
43  	 */
44  	public WeaponPref(ItemType weapon) {
45  		this.weapon = weapon;
46  		this.primary = true;
47  		NullCheck.check(this.weapon, "weapon");
48  		if (weapon.getCategory() != Category.WEAPON) {
49  			throw new IllegalArgumentException("passed 'weapon' is not of ItemType.Category.WEAPON but " + weapon.getCategory().toString());
50  		}
51  		hashCode = new HashCode().add(weapon).add(primary).getHash();
52  	}
53  	
54  	@Override
55  	public int hashCode() {
56  		return hashCode;
57  	}
58  	
59  	@Override
60  	public boolean equals(Object obj) {
61  		if (obj == this) return true;
62  		if (obj == null) return false;
63  		if (!(obj instanceof WeaponPref)) return false;
64  		WeaponPref pref = (WeaponPref)obj;
65  		return pref.weapon == weapon && pref.primary == primary;
66  	}
67  	
68  	/**
69  	 * Use 'weapon' and the concrete mode.
70  	 * @param weapon
71  	 * @param primary true == use primary firing mode, false == use secondary firing mode
72  	 */
73  	public WeaponPref(Weapon weapon, boolean primary) {
74  		this.weapon = weapon.getType();
75  		this.primary = primary;
76  		NullCheck.check(this.weapon, "weapon");
77  	}
78  
79  	/**
80  	 * Which weapon to choose.
81  	 * @return
82  	 */
83  	public ItemType getWeapon() {
84  		return weapon;
85  	}
86  
87  	/**
88  	 * Whether to use primary firing mode?
89  	 * @return
90  	 */
91  	public boolean isPrimary() {
92  		return primary;
93  	}
94  	
95  	/**
96  	 * Whether to use secondary firing mode?
97  	 * @return
98  	 */
99  	public boolean isSecondary() {
100 		return !primary;
101 	}
102 	
103 	@Override
104 	public String toString() {
105 		return "WeaponPref[type=" + weapon.getName() + ", " + (primary ? "primary mode" : "secondary mode") + "]";
106 	}
107 	
108 }