blob: 4a292b51c0f34ba560ae473ab141b56a9cdecd2a [file] [log] [blame]
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.optical.device;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.HashMap;
21import java.util.Map;
22import java.util.Set;
23
24import org.onosproject.net.Annotations;
25
26import com.google.common.annotations.Beta;
27import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.Sets;
29
30/**
31 * Filtered {@link Annotations} view.
32 */
33@Beta
34public class FilteredAnnotation implements Annotations {
35
36 private final Annotations delegate;
37 private final Set<String> filtered;
38
39 /**
40 * Creates filtered {@link Annotations} view based on {@code delegate}.
41 *
42 * @param delegate input {@link Annotations}
43 * @param keys to filter-out
44 */
45 public FilteredAnnotation(Annotations delegate, Set<String> keys) {
46 this.delegate = checkNotNull(delegate);
47 this.filtered = ImmutableSet.copyOf(keys);
48 }
49
50 @Override
51 public String value(String key) {
52 if (filtered.contains(key)) {
53 return null;
54 }
55 return delegate.value(key);
56 }
57
58 @Override
59 public Set<String> keys() {
60 return Sets.difference(delegate.keys(), filtered);
61 }
62
63 @Override
64 public String toString() {
65 Map<String, String> mapView = new HashMap<>();
66 keys().forEach(key -> mapView.put(key, delegate.value(key)));
67 return mapView.toString();
68 }
69}