1 package net.sf.snmpadaptor4j.object;
2
3 import java.io.Serializable;
4 import java.util.Arrays;
5 import java.util.StringTokenizer;
6
7
8
9
10
11 public final class SnmpOid
12 implements Comparable<SnmpOid>, Serializable {
13
14
15
16
17 private static final long serialVersionUID = -1099844003876586949L;
18
19
20
21
22 public static SnmpOid SYSNAME_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 });
23
24
25
26
27 public static SnmpOid SYSDESCR_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 1, 0 });
28
29
30
31
32 public static SnmpOid SYSLOCATION_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 });
33
34
35
36
37 public static SnmpOid SYSCONTACT_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 4, 0 });
38
39
40
41
42 public static SnmpOid SYSUPTIME_OID = SnmpOid.newInstance(new int[] { 1, 3, 6, 1, 2, 1, 1, 3, 0 });
43
44
45
46
47 private final int[] oid;
48
49
50
51
52
53
54
55
56 public static SnmpOid newInstance (final String oid, final int nodeNum, final int index) {
57 final StringTokenizer tokens = new StringTokenizer(oid.trim(), ".");
58 int[] array = new int[tokens.countTokens() + 2];
59 int i = 0;
60 while (tokens.hasMoreTokens()) {
61 array[i] = Integer.parseInt(tokens.nextToken());
62 i++;
63 }
64 array[i] = nodeNum;
65 array[i + 1] = index;
66 return new SnmpOid(array);
67 }
68
69
70
71
72
73
74
75
76 public static SnmpOid newInstance (final SnmpOid oid, final int nodeNum, final int index) {
77 final int[] array = new int[oid.oid.length + 2];
78 for (int i = 0; i < oid.oid.length; i++) {
79 array[i] = oid.oid[i];
80 }
81 array[oid.oid.length] = nodeNum;
82 array[oid.oid.length + 1] = index;
83 return new SnmpOid(array);
84 }
85
86
87
88
89
90
91 public static SnmpOid newInstance (final String oid) {
92 final StringTokenizer tokens = new StringTokenizer(oid.trim(), ".");
93 int[] array = new int[tokens.countTokens()];
94 int i = 0;
95 while (tokens.hasMoreTokens()) {
96 array[i] = Integer.parseInt(tokens.nextToken());
97 i++;
98 }
99 return new SnmpOid(array);
100 }
101
102
103
104
105
106
107
108 public static SnmpOid newInstance (final int[] oid, final int index) {
109 int[] newOid = new int[oid.length + 1];
110 for (int i = 0; i < oid.length; i++) {
111 newOid[i] = oid[i];
112 }
113 newOid[oid.length] = index;
114 return new SnmpOid(newOid);
115 }
116
117
118
119
120
121
122 public static SnmpOid newInstance (final int[] oid) {
123 return new SnmpOid(oid);
124 }
125
126
127
128
129
130 private SnmpOid (final int[] oid) {
131 super();
132 this.oid = oid;
133 }
134
135
136
137
138
139 public int[] getOid () {
140 return this.oid;
141 }
142
143
144
145
146
147 public int getIndex () {
148 return this.oid[this.oid.length - 1];
149 }
150
151
152
153
154
155 @Override
156 public int hashCode () {
157 return Arrays.hashCode(this.oid);
158 }
159
160
161
162
163
164 @Override
165 public boolean equals (final Object obj) {
166 boolean result = false;
167 if (obj == this) {
168 result = true;
169 }
170 else if ((obj != null) && (getClass().equals(obj.getClass()))) {
171 result = Arrays.equals(this.oid, ((SnmpOid) obj).oid);
172 }
173 return result;
174 }
175
176
177
178
179
180
181 public boolean equalsWithoutIndex (final SnmpOid other) {
182 boolean result = false;
183 if ((other != null) && (other.oid.length == (this.oid.length - 1))) {
184 result = true;
185 int i = 0;
186 while ((i < other.oid.length) && result) {
187 result = (other.oid[i] == this.oid[i]);
188 i++;
189 }
190 }
191 return result;
192 }
193
194
195
196
197
198
199 public boolean isRootOf (final SnmpOid other) {
200 boolean result = false;
201 if ((other != null) && (other.oid.length >= this.oid.length)) {
202 result = true;
203 int i = 0;
204 while ((i < this.oid.length) && result) {
205 result = (other.oid[i] == this.oid[i]);
206 i++;
207 }
208 }
209 return result;
210 }
211
212
213
214
215
216 public int compareTo (final SnmpOid obj) {
217 int result = 0;
218 int i = 0;
219 boolean compare = true;
220 while ((i < this.oid.length) && compare) {
221 if (i < obj.oid.length) {
222 if (this.oid[i] != obj.oid[i]) {
223 result = this.oid[i] - obj.oid[i];
224 compare = false;
225 }
226 }
227 else {
228 result = 1;
229 compare = false;
230 }
231 i++;
232 }
233 if (compare && (i < obj.oid.length)) {
234 result = -1;
235 }
236 return result;
237 }
238
239
240
241
242
243 @Override
244 public String toString () {
245 String result = "";
246 for (final int num : this.oid) {
247 result = result + "." + num;
248 }
249 return result;
250 }
251
252 }