Correct ActionProviderService method definition
[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.opendaylight.mdsal.binding.api.BindingService;
19 import org.osgi.framework.ServiceReference;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 @NonNullByDefault
24 final class Dict extends Dictionary<String, Object> {
25     private static final Logger LOG = LoggerFactory.getLogger(Dict.class);
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, 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);
40                 if (value != null) {
41                     props.put(key, value);
42                 }
43             }
44         }
45
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);
50                 if (value != null) {
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);
55                     }
56                 }
57             }
58         }
59
60         props.put(AbstractAdaptedService.DELEGATE, service);
61         return new Dict(props);
62     }
63
64     @Override
65     public int size() {
66         return map.size();
67     }
68
69     @Override
70     public boolean isEmpty() {
71         return map.isEmpty();
72     }
73
74     @Override
75     public Enumeration<String> keys() {
76         return Iterators.asEnumeration(map.keySet().iterator());
77     }
78
79     @Override
80     public Enumeration<Object> elements() {
81         return Iterators.asEnumeration(map.values().iterator());
82     }
83
84     @Override
85     public Object get(final @Nullable Object key) {
86         return map.get(key);
87     }
88
89     @Override
90     public Object put(final String key, final Object value) {
91         return map.put(key, value);
92     }
93
94     @Override
95     public Object remove(final @Nullable Object key) {
96         return map.remove(key);
97     }
98
99     @Override
100     public int hashCode() {
101         return map.hashCode();
102     }
103
104     @Override
105     public boolean equals(final @Nullable Object obj) {
106         return this == obj || obj instanceof Dict && map.equals(((Dict) obj).map);
107     }
108
109     @Override
110     public String toString() {
111         return map.toString();
112     }
113 }