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