blob: 46037a26045147cfd80e723a613cb476fcb0b0f9 [file] [log] [blame]
Carolina Fernandezfa56d142016-11-14 01:13:26 +01001/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onlab.util;
18
19import java.util.ArrayList;
20import java.util.List;
21
22import static com.google.common.base.Preconditions.checkArgument;
23
24/**
25 * Filters content on a given object with String representation.
26 * This is carried out through restrictive (AND) or loose (OR) searches.
27 * If not provided, strategy defaults to ADD.
28 */
29public class StringFilter {
30
31 /**
32 * Defines the filtering strategy.
33 */
34 public enum Strategy {
35 AND,
36 OR
37 }
38
39 private Strategy strategy = Strategy.AND;
40 private List<String> filter = new ArrayList<>();
41
42 /**
43 * Creates a new filter to apply on some data.
44 *
45 * @param filter list with filters to apply
46 */
47 public StringFilter(List<String> filter) {
48 this.filter = filter;
49 }
50
51 /**
52 * Creates a new filter to apply on some data,
53 * given a specific strategy (AND, OR).
54 *
55 * @param filter list with filters to apply
56 * @param strategy type of strategy (AND, OR)
57 */
58 public StringFilter(List<String> filter, Strategy strategy) {
59 this(filter);
60 checkArgument(strategy == Strategy.AND || strategy == Strategy.OR,
61 "Chosen strategy is not allowed (should be one of {AND, OR})");
62 this.strategy = strategy;
63 }
64
65 /**
66 * Filters data according to a set of restrictions and the AND strategy.
67 *
68 * @param data Object with data to filter
69 * @return true if data honours the filter, false otherwise
70 */
71 private boolean filterAnd(Object data) {
72 return filter.isEmpty() ||
73 filter.stream().filter(data.toString()::contains)
74 .count() == filter.size();
75 }
76
77 /**
78 * Filters data according to a set of restrictions and the OR strategy.
79 *
80 * @param data Object with data to filter
81 * @return true if data honours the filter, false otherwise
82 */
83 private boolean filterOr(Object data) {
84 return filter.isEmpty() ||
85 filter.stream().filter(data.toString()::contains)
86 .count() > 0;
87 }
88
89 /**
90 * Filters data according to a set of restrictions and a specific strategy.
91 *
92 * @param data Object with data to filter
93 * @return true if data honours the filters, false otherwise
94 */
95 public boolean filter(Object data) {
96 if (strategy == Strategy.OR) {
97 return filterOr(data);
98 } else {
99 return filterAnd(data);
100 }
101 }
102}