Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.net.optical.device; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.onosproject.net.Annotations; |
| 25 | |
| 26 | import com.google.common.annotations.Beta; |
| 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Sets; |
| 29 | |
| 30 | /** |
| 31 | * Filtered {@link Annotations} view. |
| 32 | */ |
| 33 | @Beta |
| 34 | public 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 | } |