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.opendaylight.mdsal.binding.api.BindingService;
19 import org.osgi.framework.ServiceReference;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 final class Dict extends Dictionary<String, Object> {
25 private static final Logger LOG = LoggerFactory.getLogger(Dict.class);
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, final BindingService service) {
34 final String[] keys = ref.getPropertyKeys();
35 final Map<String, Object> props = Maps.newHashMapWithExpectedSize(keys.length + 1);
36 for (String key : keys) {
37 // Ignore properties with our prefix: we are not exporting those
38 if (!key.startsWith(ServiceProperties.PREFIX)) {
39 final Object value = ref.getProperty(key);
41 props.put(key, value);
46 // Second phase: apply any our properties
47 for (String key : keys) {
48 if (key.startsWith(ServiceProperties.OVERRIDE_PREFIX)) {
49 final Object value = ref.getProperty(key);
51 final String newKey = key.substring(ServiceProperties.OVERRIDE_PREFIX.length());
52 if (!newKey.isEmpty()) {
53 LOG.debug("Overriding property {}", newKey);
54 props.put(newKey, value);
60 props.put(AbstractAdaptedService.DELEGATE, service);
61 return new Dict(props);
70 public boolean isEmpty() {
75 public Enumeration<String> keys() {
76 return Iterators.asEnumeration(map.keySet().iterator());
80 public Enumeration<Object> elements() {
81 return Iterators.asEnumeration(map.values().iterator());
85 public Object get(final @Nullable Object key) {
90 public Object put(final String key, final Object value) {
91 return map.put(key, value);
95 public Object remove(final @Nullable Object key) {
96 return map.remove(key);
100 public int hashCode() {
101 return map.hashCode();
105 public boolean equals(final @Nullable Object obj) {
106 return this == obj || obj instanceof Dict && map.equals(((Dict) obj).map);
110 public String toString() {
111 return map.toString();