KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > agent > ComparableSnmpObjectId


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.jmx.adaptor.snmp.agent;
23
24 import org.opennms.protocols.snmp.SnmpObjectId;
25
26 /**
27  * Provide SnmpObjectIds that are Comparable to be used
28  * in SortedSets etc.
29  * @author <a HREF="mailto:hwr@pilhuhn.de">Heiko W. Rupp</a>
30  * @version $Revision: 46150 $
31  */

32 public class ComparableSnmpObjectId extends SnmpObjectId implements Comparable JavaDoc
33 {
34
35     public ComparableSnmpObjectId(String JavaDoc oid)
36     {
37         super(oid);
38     }
39     
40     public ComparableSnmpObjectId(SnmpObjectId oid)
41     {
42         super(oid);
43     }
44     
45     public ComparableSnmpObjectId(int[] identifiers)
46     {
47         super(identifiers);
48     }
49
50     /**
51      * Compare to the passed object. Uses compare()
52      * from the underlying snmp-library
53      * @see SnmpObjectId.compare()
54      * @param o Object to compare with (Usually a ComparableSnmpObjectId)
55      * @return -1, if no SnmpObjectId passed in, the result of the underlying compare otherwise.
56      */

57     public int compareTo(Object JavaDoc o)
58     {
59         
60         if (o==null)
61             return -1;
62         
63         if (!(o instanceof SnmpObjectId))
64             return -1;
65         
66         return this.compare((SnmpObjectId)o);
67     }
68     
69     /**
70      * This object is a leaf if the last part of the oid parts is a 0 (Zero)
71      * @return true if the oid ends in 0
72      */

73     public boolean isLeaf()
74     {
75         int[] ids = getIdentifiers();
76         if (ids.length==0) { // no data present (should not happen)
77
return false;
78         }
79         if (ids[ids.length-1]==0)
80         {
81             return true;
82         }
83         return false;
84     }
85
86     /**
87      * Removes the last oid-component.
88      * Example .1.2.3.4.0 as input yields .1.2.3.4 as output
89      * @return an oid with the last part removed.
90      */

91     public ComparableSnmpObjectId removeLastPart()
92     {
93         int[] ids = getIdentifiers();
94         
95         int[] outs = new int[ids.length-1];
96         int len = ids.length-1;
97         for (int i = 0; i<len ; i++)
98         {
99             outs[i]=ids[i];
100         }
101         ComparableSnmpObjectId out = new ComparableSnmpObjectId(outs);
102         return out;
103     }
104     
105     /**
106      * Build an oid where the second last component
107      * (after removing a .0 of a leaf) is increased by 1.
108      * The last component is removed, to the result actually forms
109      * the root of a subtree, that is adjacent to the subtree this
110      * object is in.
111      * Example .1.2.3.4.0 -> .1.2.4
112      * Example .1.2.3.4.5 -> .1.2.4
113      * @return
114      */

115     public ComparableSnmpObjectId getNextArc()
116     {
117         ComparableSnmpObjectId cid = this;
118         if (isLeaf())
119         {
120             cid = removeLastPart();
121         }
122         cid = cid.removeLastPart();
123         
124         int[] ids = cid.getIdentifiers();
125         int[] ods = new int[ids.length];
126         System.arraycopy(ids,
127                         0,
128                         ods,
129                         0,
130                         ids.length);
131         
132         int len = ods.length-1;
133         ods[len]++;
134         
135         ComparableSnmpObjectId ret = new ComparableSnmpObjectId(ods);
136         return ret;
137     }
138     
139 }
140
Popular Tags