blob: f341c97a9625baff6ae47315ff75eda9e00d70d3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package sun.management.snmp.util;
26
27import com.sun.jmx.snmp.SnmpOid;
28
29import java.io.Serializable;
30
31import java.util.Comparator;
32import java.util.Arrays;
33import java.util.TreeMap;
34import java.util.List;
35import java.util.Iterator;
36
37import java.lang.ref.WeakReference;
38
39/**
40 * This class is used to cache table data.
41 **/
42public class SnmpCachedData implements SnmpTableHandler {
43
44 /**
45 * Compares two SnmpOid.
46 **/
47 public static final Comparator<SnmpOid> oidComparator =
48 new Comparator<SnmpOid>() {
49 public int compare(SnmpOid o1, SnmpOid o2) {
50 return o1.compareTo(o2);
51 }
52 public boolean equals(Object o1, Object o2) {
53 if (o1 == o2) return true;
54 else return o1.equals(o2);
55 }
56 };
57
58 /**
59 * Constructs a new instance of SnmpCachedData. Instances are
60 * immutable.
61 * @param lastUpdated Time stamp as returned by
62 * {@link System#currentTimeMillis System.currentTimeMillis()}
63 * @param indexes The table entry indexes, sorted in ascending order.
64 * @param datas The table datas, sorted according to the
65 * order in <code>indexes</code>: <code>datas[i]</code>
66 * is the data that corresponds to
67 * <code>indexes[i]</code>
68 **/
69 public SnmpCachedData(long lastUpdated, SnmpOid indexes[],
70 Object datas[]) {
71 this.lastUpdated = lastUpdated;
72 this.indexes = indexes;
73 this.datas = datas;
74 }
75
76 /**
77 * Constructs a new instance of SnmpCachedData. Instances are
78 * immutable.
79 * @param lastUpdated Time stamp as returned by
80 * {@link System#currentTimeMillis System.currentTimeMillis()}
81 * @param indexMap The table indexed table data, sorted in ascending
82 * order by {@link #oidComparator}. The keys must be
83 * instances of {@link SnmpOid}.
84 **/
85 public SnmpCachedData(long lastUpdated, TreeMap<SnmpOid, Object> indexMap) {
86 this(lastUpdated, indexMap, true);
87 }
88 /**
89 * Constructs a new instance of SnmpCachedData. Instances are
90 * immutable.
91 * @param lastUpdated Time stamp as returned by
92 * {@link System#currentTimeMillis System.currentTimeMillis()}
93 * @param indexMap The table indexed table data, sorted in ascending
94 * order by {@link #oidComparator}. The keys must be
95 * instances of {@link SnmpOid}.
96 **/
97 public SnmpCachedData(long lastUpdated, TreeMap<SnmpOid, Object> indexMap,
98 boolean b) {
99
100 final int size = indexMap.size();
101 this.lastUpdated = lastUpdated;
102 this.indexes = new SnmpOid[size];
103 this.datas = new Object[size];
104
105 if(b) {
106 indexMap.keySet().toArray(this.indexes);
107 indexMap.values().toArray(this.datas);
108 } else
109 indexMap.values().toArray(this.datas);
110 }
111
112 /**
113 * Time stamp as returned by
114 * {@link System#currentTimeMillis System.currentTimeMillis()}
115 **/
116 public final long lastUpdated;
117
118 /**
119 * The table entry indexes, sorted in ascending order.
120 **/
121 public final SnmpOid indexes[];
122
123 /**
124 * The table datas, sorted according to the
125 * order in <code>indexes</code>: <code>datas[i]</code>
126 * is the data that corresponds to <code>indexes[i]</code>
127 **/
128 public final Object datas[];
129
130 /**
131 * The position of the given <var>index</var>, as returned by
132 * <code>java.util.Arrays.binarySearch()</code>
133 **/
134 public final int find(SnmpOid index) {
135 return Arrays.binarySearch(indexes,index,oidComparator);
136 }
137
138 // SnmpTableHandler.getData()
139 public Object getData(SnmpOid index) {
140 final int pos = find(index);
141 if ((pos < 0)||(pos >= datas.length)) return null;
142 return datas[pos];
143 }
144
145 // SnmpTableHandler.getNext()
146 public SnmpOid getNext(SnmpOid index) {
147 if (index == null) {
148 if (indexes.length>0) return indexes[0];
149 else return null;
150 }
151 final int pos = find(index);
152 if (pos > -1) {
153 if (pos < (indexes.length -1) ) return indexes[pos+1];
154 else return null;
155 }
156 final int insertion = -pos -1;
157 if ((insertion > -1) && (insertion < indexes.length))
158 return indexes[insertion];
159 else return null;
160 }
161
162 // SnmpTableHandler.contains()
163 public boolean contains(SnmpOid index) {
164 final int pos = find(index);
165 return ((pos > -1)&&(pos < indexes.length));
166 }
167
168}