2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter.osgi;
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;
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;
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());
27 private final Map<String, Object> map;
29 private Dict(final Map<String, Object> map) {
30 this.map = ImmutableMap.copyOf(map);
33 static Dict fromReference(final ServiceReference<?> ref) {
34 final String[] keys = ref.getPropertyKeys();
35 if (keys.length == 0) {
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);
45 props.put(key, value);
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);
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);
64 return new Dict(props);
73 public boolean isEmpty() {
78 public Enumeration<String> keys() {
79 return Iterators.asEnumeration(map.keySet().iterator());
83 public Enumeration<Object> elements() {
84 return Iterators.asEnumeration(map.values().iterator());
88 public Object get(final @Nullable Object key) {
93 public Object put(final String key, final Object value) {
94 return map.put(key, value);
98 public Object remove(final @Nullable Object key) {
99 return map.remove(key);
103 public int hashCode() {
104 return map.hashCode();
108 public boolean equals(final @Nullable Object obj) {
109 return this == obj || obj instanceof Dict && map.equals(((Dict) obj).map);
113 public String toString() {
114 return map.toString();