blob: 8fe3d42a2e275a9ec909974a1e852bc90e743cdc [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 abstract class implements a weak cache for a table whose data
41 * is obtained from a {@link List}.
42 *
43 * <p><b>NOTE: This class is not synchronized, subclasses must implement
44 * the appropriate synchronization whwn needed.</b></p>
45 **/
46public abstract class SnmpListTableCache extends SnmpTableCache {
47
48
49 /**
50 * The index of the entry corresponding to the given <var>item</var>.
51 * <br>This method is called by {@link #updateCachedDatas(Object,List)}.
52 * The given <var>item</var> is expected to be always associated with
53 * the same index.
54 * @param context The context passed to
55 * {@link #updateCachedDatas(Object,List)}.
56 * @param rawDatas Raw table datas passed to
57 * {@link #updateCachedDatas(Object,List)}.
58 * @param rank Rank of the given <var>item</var> in the
59 * <var>rawDatas</var> list iterator.
60 * @param item The raw data object for which an index must be determined.
61 **/
62 protected abstract SnmpOid getIndex(Object context, List rawDatas,
63 int rank, Object item);
64
65 /**
66 * The data for the entry corresponding to the given <var>item</var>.
67 * <br>This method is called by {@link #updateCachedDatas(Object,List)}.
68 * @param context The context passed to
69 * {@link #updateCachedDatas(Object,List)}.
70 * @param rawDatas Raw table datas passed to
71 * {@link #updateCachedDatas(Object,List)}.
72 * @param rank Rank of the given <var>item</var> in the
73 * <var>rawDatas</var> list iterator.
74 * @param item The raw data object from which the entry data must be
75 * extracted.
76 * @return By default <var>item</var> is returned.
77 **/
78 protected Object getData(Object context, List rawDatas,
79 int rank, Object item) {
80 return item;
81 }
82
83 /**
84 * Recompute cached data.
85 * @param context A context object, valid during the duration of
86 * of the call to this method, and that will be passed to
87 * {@link #getIndex} and {@link #getData}. <br>
88 * This method is intended to be called by
89 * {@link #updateCachedDatas(Object)}. It is assumed that
90 * the context is be allocated by before this method is called,
91 * and released just after this method has returned.<br>
92 * This class does not use the context object: it is a simple
93 * hook for subclassed.
94 * @param rawDatas The table datas from which the cached data will be
95 * computed.
96 * @return the computed cached data.
97 **/
98 protected SnmpCachedData updateCachedDatas(Object context, List rawDatas) {
99 final int size = ((rawDatas == null)?0:rawDatas.size());
100 if (size == 0) return null;
101
102 final long time = System.currentTimeMillis();
103 final Iterator it = rawDatas.iterator();
104 final TreeMap<SnmpOid, Object> map =
105 new TreeMap<SnmpOid, Object>(SnmpCachedData.oidComparator);
106 for (int rank=0; it.hasNext() ; rank++) {
107 final Object item = it.next();
108 final SnmpOid index = getIndex(context, rawDatas, rank, item);
109 final Object data = getData(context, rawDatas, rank, item);
110 if (index == null) continue;
111 map.put(index,data);
112 }
113
114 return new SnmpCachedData(time,map);
115 }
116
117}