01: /*_############################################################################
02: _##
03: _## SNMP4J-Agent - ProxyMap.java
04: _##
05: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
06: _##
07: _## Licensed under the Apache License, Version 2.0 (the "License");
08: _## you may not use this file except in compliance with the License.
09: _## You may obtain a copy of the License at
10: _##
11: _## http://www.apache.org/licenses/LICENSE-2.0
12: _##
13: _## Unless required by applicable law or agreed to in writing, software
14: _## distributed under the License is distributed on an "AS IS" BASIS,
15: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: _## See the License for the specific language governing permissions and
17: _## limitations under the License.
18: _##
19: _##########################################################################*/
20:
21: package org.snmp4j.agent;
22:
23: import java.util.Map;
24: import org.snmp4j.smi.OctetString;
25: import java.util.TreeMap;
26:
27: /**
28: * The <code>ProxyMap</code> maps context engine IDs in conjunction with a
29: * proxy usage type to a ProxyForwarder instance.
30: *
31: * @author Frank Fock
32: * @version 1.0
33: */
34: public class ProxyMap {
35:
36: private Map proxies = new TreeMap();
37:
38: public ProxyMap() {
39: }
40:
41: public ProxyForwarder add(ProxyForwarder proxyForwarder,
42: OctetString contextEngineID, int proxyType) {
43: return (ProxyForwarder) this .proxies.put(new ProxyKey(
44: contextEngineID, proxyType), proxyForwarder);
45: }
46:
47: public ProxyForwarder remove(OctetString contextEngineID,
48: int proxyType) {
49: return (ProxyForwarder) this .proxies.remove(new ProxyKey(
50: contextEngineID, proxyType));
51: }
52:
53: public ProxyForwarder get(OctetString contextEngineID, int proxyType) {
54: ProxyForwarder proxy = (ProxyForwarder) this .proxies
55: .get(new ProxyKey(contextEngineID, proxyType));
56: if (proxy == null) {
57: proxy = (ProxyForwarder) this .proxies.get(new ProxyKey(
58: null, proxyType));
59: }
60: return proxy;
61: }
62:
63: static class ProxyKey implements Comparable {
64:
65: private OctetString contextEngineID;
66: private int proxyType;
67:
68: ProxyKey(OctetString contextEngineID, int proxyType) {
69: this .contextEngineID = contextEngineID;
70: this .proxyType = proxyType;
71: }
72:
73: public int compareTo(Object o) {
74: ProxyKey other = (ProxyKey) o;
75: if (((contextEngineID == null) && (other.contextEngineID != null))
76: || ((other.contextEngineID == null) && (contextEngineID != null))
77: || ((other.contextEngineID == null) && (contextEngineID == null))
78: || (contextEngineID.equals(other.contextEngineID))) {
79: if ((proxyType == ProxyForwarder.PROXY_TYPE_ALL)
80: || (other.proxyType == ProxyForwarder.PROXY_TYPE_ALL)) {
81: return 0;
82: }
83: return (proxyType - other.proxyType);
84: }
85: return contextEngineID.compareTo(other.contextEngineID);
86: }
87:
88: }
89: }
|