001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jmx.adaptor.snmp.agent;
023:
024: import org.opennms.protocols.snmp.SnmpObjectId;
025:
026: /**
027: * Provide SnmpObjectIds that are Comparable to be used
028: * in SortedSets etc.
029: * @author <a href="mailto:hwr@pilhuhn.de">Heiko W. Rupp</a>
030: * @version $Revision: 57210 $
031: */
032: public class ComparableSnmpObjectId extends SnmpObjectId implements
033: Comparable {
034:
035: public ComparableSnmpObjectId(String oid) {
036: super (oid);
037: }
038:
039: public ComparableSnmpObjectId(SnmpObjectId oid) {
040: super (oid);
041: }
042:
043: public ComparableSnmpObjectId(int[] identifiers) {
044: super (identifiers);
045: }
046:
047: /**
048: * Compare to the passed object. Uses compare()
049: * from the underlying snmp-library
050: * @see SnmpObjectId.compare()
051: * @param o Object to compare with (Usually a ComparableSnmpObjectId)
052: * @return -1, if no SnmpObjectId passed in, the result of the underlying compare otherwise.
053: */
054: public int compareTo(Object o) {
055:
056: if (o == null)
057: return -1;
058:
059: if (!(o instanceof SnmpObjectId))
060: return -1;
061:
062: return this .compare((SnmpObjectId) o);
063: }
064:
065: /**
066: * This object is a leaf if the last part of the oid parts is a 0 (Zero)
067: * @return true if the oid ends in 0
068: */
069: public boolean isLeaf() {
070: int[] ids = getIdentifiers();
071: if (ids.length == 0) { // no data present (should not happen)
072: return false;
073: }
074: if (ids[ids.length - 1] == 0) {
075: return true;
076: }
077: return false;
078: }
079:
080: /**
081: * Removes the last oid-component.
082: * Example .1.2.3.4.0 as input yields .1.2.3.4 as output
083: * @return an oid with the last part removed.
084: */
085: public ComparableSnmpObjectId removeLastPart() {
086: int[] ids = getIdentifiers();
087:
088: int[] outs = new int[ids.length - 1];
089: int len = ids.length - 1;
090: for (int i = 0; i < len; i++) {
091: outs[i] = ids[i];
092: }
093: ComparableSnmpObjectId out = new ComparableSnmpObjectId(outs);
094: return out;
095: }
096:
097: /**
098: * Build an oid where the second last component
099: * (after removing a .0 of a leaf) is increased by 1.
100: * The last component is removed, to the result actually forms
101: * the root of a subtree, that is adjacent to the subtree this
102: * object is in.
103: * Example .1.2.3.4.0 -> .1.2.4
104: * Example .1.2.3.4.5 -> .1.2.4
105: * @return
106: */
107: public ComparableSnmpObjectId getNextArc() {
108: ComparableSnmpObjectId cid = this ;
109: if (isLeaf()) {
110: cid = removeLastPart();
111: }
112: cid = cid.removeLastPart();
113:
114: int[] ids = cid.getIdentifiers();
115: int[] ods = new int[ids.length];
116: System.arraycopy(ids, 0, ods, 0, ids.length);
117:
118: int len = ods.length - 1;
119: ods[len]++;
120:
121: ComparableSnmpObjectId ret = new ComparableSnmpObjectId(ods);
122: return ret;
123: }
124:
125: }
|