0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.management.snmp.util;
0N/A
0N/Aimport com.sun.jmx.snmp.SnmpOid;
0N/A
0N/Aimport java.io.Serializable;
0N/A
0N/Aimport java.util.Comparator;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.TreeMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Iterator;
0N/A
0N/Aimport java.lang.ref.WeakReference;
0N/A
0N/A/**
0N/A * This abstract class implements a weak cache for a table whose data
0N/A * is obtained from a {@link List}.
0N/A *
0N/A * <p><b>NOTE: This class is not synchronized, subclasses must implement
0N/A * the appropriate synchronization whwn needed.</b></p>
0N/A **/
0N/Apublic abstract class SnmpListTableCache extends SnmpTableCache {
0N/A
0N/A
0N/A /**
0N/A * The index of the entry corresponding to the given <var>item</var>.
0N/A * <br>This method is called by {@link #updateCachedDatas(Object,List)}.
0N/A * The given <var>item</var> is expected to be always associated with
0N/A * the same index.
0N/A * @param context The context passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rawDatas Raw table datas passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rank Rank of the given <var>item</var> in the
0N/A * <var>rawDatas</var> list iterator.
0N/A * @param item The raw data object for which an index must be determined.
0N/A **/
0N/A protected abstract SnmpOid getIndex(Object context, List rawDatas,
0N/A int rank, Object item);
0N/A
0N/A /**
0N/A * The data for the entry corresponding to the given <var>item</var>.
0N/A * <br>This method is called by {@link #updateCachedDatas(Object,List)}.
0N/A * @param context The context passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rawDatas Raw table datas passed to
0N/A * {@link #updateCachedDatas(Object,List)}.
0N/A * @param rank Rank of the given <var>item</var> in the
0N/A * <var>rawDatas</var> list iterator.
0N/A * @param item The raw data object from which the entry data must be
0N/A * extracted.
0N/A * @return By default <var>item</var> is returned.
0N/A **/
0N/A protected Object getData(Object context, List rawDatas,
0N/A int rank, Object item) {
0N/A return item;
0N/A }
0N/A
0N/A /**
0N/A * Recompute cached data.
0N/A * @param context A context object, valid during the duration of
0N/A * of the call to this method, and that will be passed to
0N/A * {@link #getIndex} and {@link #getData}. <br>
0N/A * This method is intended to be called by
0N/A * {@link #updateCachedDatas(Object)}. It is assumed that
0N/A * the context is be allocated by before this method is called,
0N/A * and released just after this method has returned.<br>
0N/A * This class does not use the context object: it is a simple
0N/A * hook for subclassed.
0N/A * @param rawDatas The table datas from which the cached data will be
0N/A * computed.
0N/A * @return the computed cached data.
0N/A **/
0N/A protected SnmpCachedData updateCachedDatas(Object context, List rawDatas) {
0N/A final int size = ((rawDatas == null)?0:rawDatas.size());
0N/A if (size == 0) return null;
0N/A
0N/A final long time = System.currentTimeMillis();
0N/A final Iterator it = rawDatas.iterator();
0N/A final TreeMap<SnmpOid, Object> map =
0N/A new TreeMap<SnmpOid, Object>(SnmpCachedData.oidComparator);
0N/A for (int rank=0; it.hasNext() ; rank++) {
0N/A final Object item = it.next();
0N/A final SnmpOid index = getIndex(context, rawDatas, rank, item);
0N/A final Object data = getData(context, rawDatas, rank, item);
0N/A if (index == null) continue;
0N/A map.put(index,data);
0N/A }
0N/A
0N/A return new SnmpCachedData(time,map);
0N/A }
0N/A
0N/A}