create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / motor / MotorId.java
1 package net.sf.openrocket.motor;
2
3 /**
4  * An immutable identifier for a motor instance in a MotorInstanceConfiguration.
5  * The motor is identified by the ID of its mounting component and a 
6  * positive motor count number.
7  * 
8  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
9  */
10 public final class MotorId {
11
12         private final String componentId;
13         private final int number;
14         
15         
16         /**
17          * Sole constructor.
18          * 
19          * @param componentId   the component ID, must not be null
20          * @param number                a positive motor doun5 number
21          */
22         public MotorId(String componentId, int number) {
23                 super();
24                 
25                 if (componentId == null) {
26                         throw new IllegalArgumentException("Component ID was null");
27                 }
28                 if (number <= 0) {
29                         throw new IllegalArgumentException("Number must be positive, n=" + number);
30                 }
31                 
32                 // Use intern so comparison can be done using == instead of equals()
33                 this.componentId = componentId.intern();
34                 this.number = number;
35         }
36         
37         
38         public String getComponentId() {
39                 return componentId;
40         }
41         
42         public int getNumber() {
43                 return number;
44         }
45         
46         
47         @Override
48         public boolean equals(Object o) {
49                 if (this == o)
50                         return true;
51                 
52                 if (!(o instanceof MotorId))
53                         return false;
54                 
55                 MotorId other = (MotorId)o;
56                 // Comparison with == ok since string is intern()'ed
57                 return this.componentId == other.componentId && this.number == other.number;
58         }
59         
60         
61         @Override
62         public int hashCode() {
63                 return componentId.hashCode() + (number << 12);
64         }
65         
66         // TODO: toString()
67 }