Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SnmpTrap |
|
| 1.7777777777777777;1,778 |
1 | package net.sf.snmpadaptor4j.object; | |
2 | ||
3 | import java.io.Serializable; | |
4 | import java.util.Collections; | |
5 | import java.util.HashMap; | |
6 | import java.util.Map; | |
7 | import org.apache.log4j.Logger; | |
8 | ||
9 | /** | |
10 | * Object representing a specific or generic SNMP trap. | |
11 | * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a> | |
12 | */ | |
13 | public abstract class SnmpTrap | |
14 | implements Serializable { | |
15 | ||
16 | /** | |
17 | * Serial number. | |
18 | */ | |
19 | private static final long serialVersionUID = -2950288105650647983L; | |
20 | ||
21 | /** | |
22 | * Elapsed time in <b>100th of second</b> since the application launch. | |
23 | */ | |
24 | private final long timeStamp; | |
25 | ||
26 | /** | |
27 | * OID indexing the source of the trap. | |
28 | */ | |
29 | private final SnmpOid source; | |
30 | ||
31 | /** | |
32 | * Map of "interesting" information. | |
33 | */ | |
34 | private final Map<SnmpOid, SnmpTrapData> dataMap; | |
35 | ||
36 | /** | |
37 | * Creates and returns an object representing a specific SNMP trap. | |
38 | * @param timeStamp Elapsed time in <b>100th of second</b> since the application launch. | |
39 | * @param source OID indexing the source of the trap (must not be <code>NULL</code>). | |
40 | * @param type Trap type. | |
41 | * @param dataMap Map of "interesting" information. | |
42 | * @return Object representing a specific SNMP trap. | |
43 | */ | |
44 | public static SpecificSnmpTrap newInstance (final long timeStamp, final SnmpOid source, final int type, final Map<SnmpOid, SnmpTrapData> dataMap) { | |
45 | 36 | return new SpecificSnmpTrap(timeStamp, source, type, dataMap); |
46 | } | |
47 | ||
48 | /** | |
49 | * Creates and returns an object representing a generic SNMP trap. | |
50 | * @param timeStamp Elapsed time in <b>100th of second</b> since the application launch. | |
51 | * @param source OID indexing the source of the trap (must not be <code>NULL</code>). | |
52 | * @param type Trap type (must not be <code>NULL</code>). | |
53 | * @param dataMap Map of "interesting" information. | |
54 | * @return Object representing a generic SNMP trap. | |
55 | */ | |
56 | public static GenericSnmpTrap newInstance (final long timeStamp, final SnmpOid source, final GenericSnmpTrapType type, final Map<SnmpOid, SnmpTrapData> dataMap) { | |
57 | 71 | return new GenericSnmpTrap(timeStamp, source, type, dataMap); |
58 | } | |
59 | ||
60 | /** | |
61 | * Hidden constructor (abstract class). | |
62 | * @param timeStamp Elapsed time in <b>100th of second</b> since the application launch. | |
63 | * @param source OID indexing the source of the trap. | |
64 | * @param dataMap Map of "interesting" information. | |
65 | */ | |
66 | protected SnmpTrap (final long timeStamp, final SnmpOid source, final Map<SnmpOid, SnmpTrapData> dataMap) { | |
67 | 109 | super(); |
68 | 109 | this.timeStamp = timeStamp; |
69 | 109 | this.source = source; |
70 | 109 | this.dataMap = Collections.unmodifiableMap(dataMap != null ? dataMap : new HashMap<SnmpOid, SnmpTrapData>()); |
71 | 109 | } |
72 | ||
73 | /** | |
74 | * Returns the elapsed time in <b>100th of second</b> since the application launch. | |
75 | * @return Elapsed time in <b>100th of second</b> since the application launch. | |
76 | */ | |
77 | public final long getTimeStamp () { | |
78 | 29 | return this.timeStamp; |
79 | } | |
80 | ||
81 | /** | |
82 | * Returns the OID indexing the source of the trap. | |
83 | * @return OID indexing the source of the trap. | |
84 | */ | |
85 | public final SnmpOid getSource () { | |
86 | 60 | return this.source; |
87 | } | |
88 | ||
89 | /** | |
90 | * Returns the map of "interesting" information. | |
91 | * @return Map of "interesting" information (never <code>NULL</code>). | |
92 | */ | |
93 | public final Map<SnmpOid, SnmpTrapData> getDataMap () { | |
94 | 23 | return this.dataMap; |
95 | } | |
96 | ||
97 | /** | |
98 | * Traces the content of SNMP trap to logs. | |
99 | * @param logger Logger (must not be <code>NULL</code>). | |
100 | */ | |
101 | public abstract void traceTo (Logger logger); | |
102 | ||
103 | /* | |
104 | * {@inheritDoc} | |
105 | * @see java.lang.Object#hashCode() | |
106 | */ | |
107 | @Override | |
108 | public int hashCode () { | |
109 | final int prime = 31; | |
110 | 60 | int result = 1; |
111 | 60 | result = prime * result + this.dataMap.hashCode(); |
112 | 60 | result = prime * result + ((this.source == null) ? 0 : this.source.hashCode()); |
113 | 60 | result = prime * result + (int) (this.timeStamp ^ (this.timeStamp >>> 32)); |
114 | 60 | return result; |
115 | } | |
116 | ||
117 | /* | |
118 | * {@inheritDoc} | |
119 | * @see java.lang.Object#equals(java.lang.Object) | |
120 | */ | |
121 | @Override | |
122 | public boolean equals (final Object obj) { | |
123 | 44 | boolean result = false; |
124 | 44 | if ((obj != null) && getClass().equals(obj.getClass())) { |
125 | 38 | final SnmpTrap other = (SnmpTrap) obj; |
126 | 38 | result = (this.timeStamp == other.timeStamp); |
127 | 38 | if (result) { |
128 | 36 | result = (this.source != null ? this.source.equals(other.source) : (other.source == null)); |
129 | } | |
130 | 38 | if (result) { |
131 | 30 | result = this.dataMap.equals(other.dataMap); |
132 | } | |
133 | } | |
134 | 44 | return result; |
135 | } | |
136 | ||
137 | } |