Bump odlparent/yangtools to 7.0.5/5.0.5
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / osgi / Dict.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter.osgi;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.Iterators;
12 import com.google.common.collect.Maps;
13 import java.util.Dictionary;
14 import java.util.Enumeration;
15 import java.util.Map;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.osgi.framework.ServiceReference;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 @NonNullByDefault
23 final class Dict extends Dictionary<String, Object> {
24     private static final Logger LOG = LoggerFactory.getLogger(Dict.class);
25     private static final Dict EMPTY = new Dict(ImmutableMap.of());
26
27     private final Map<String, Object> map;
28
29     private Dict(final Map<String, Object> map) {
30         this.map = ImmutableMap.copyOf(map);
31     }
32
33     static Dict fromReference(final ServiceReference<?> ref) {
34         final String[] keys = ref.getPropertyKeys();
35         if (keys.length == 0) {
36             return EMPTY;
37         }
38
39         final Map<String, Object> props = Maps.newHashMapWithExpectedSize(keys.length);
40         for (String key : keys) {
41             // Ignore properties with our prefix: we are not exporting those
42             if (!key.startsWith(ServiceProperties.PREFIX)) {
43                 final Object value = ref.getProperty(key);
44                 if (value != null) {
45                     props.put(key, value);
46                 }
47             }
48         }
49
50         // Second phase: apply any our properties
51         for (String key : keys) {
52             if (key.startsWith(ServiceProperties.OVERRIDE_PREFIX)) {
53                 final Object value = ref.getProperty(key);
54                 if (value != null) {
55                     final String newKey = key.substring(ServiceProperties.OVERRIDE_PREFIX.length());
56                     if (!newKey.isEmpty()) {
57                         LOG.debug("Overriding property {}", newKey);
58                         props.put(newKey, value);
59                     }
60                 }
61             }
62         }
63
64         return new Dict(props);
65     }
66
67     @Override
68     public int size() {
69         return map.size();
70     }
71
72     @Override
73     public boolean isEmpty() {
74         return map.isEmpty();
75     }
76
77     @Override
78     public Enumeration<String> keys() {
79         return Iterators.asEnumeration(map.keySet().iterator());
80     }
81
82     @Override
83     public Enumeration<Object> elements() {
84         return Iterators.asEnumeration(map.values().iterator());
85     }
86
87     @Override
88     public Object get(final @Nullable Object key) {
89         return map.get(key);
90     }
91
92     @Override
93     public Object put(final String key, final Object value) {
94         return map.put(key, value);
95     }
96
97     @Override
98     public Object remove(final @Nullable Object key) {
99         return map.remove(key);
100     }
101
102     @Override
103     public int hashCode() {
104         return map.hashCode();
105     }
106
107     @Override
108     public boolean equals(final @Nullable Object obj) {
109         return this == obj || obj instanceof Dict && map.equals(((Dict) obj).map);
110     }
111
112     @Override
113     public String toString() {
114         return map.toString();
115     }
116 }