/
ImmutableLogicalFilter.java
executable file
·144 lines (123 loc) · 5.22 KB
/
ImmutableLogicalFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.thingsearch;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
/**
* An immutable implementation of {@link LogicalSearchFilter}.
*/
@Immutable
final class ImmutableLogicalFilter implements LogicalSearchFilter {
private final Type type;
private final Collection<SearchFilter> searchFilters;
private ImmutableLogicalFilter(final Type theFilterType, final Collection<SearchFilter> theFilters) {
type = theFilterType;
searchFilters = Collections.unmodifiableList(new ArrayList<>(theFilters));
}
/**
* Returns a new instance of {@code ImmutableLogicalFilter}
*
* @param filter the filter to be negated.
* @return the new logical {@code NOT} filter.
* @throws NullPointerException if {@code filter} is {@code null}.
*/
public static ImmutableLogicalFilter not(final SearchFilter filter) {
checkNotNull(filter, "filter to be negated");
return new ImmutableLogicalFilter(Type.NOT, Collections.singleton(filter));
}
/**
* Returns a new {@code ImmutableLogicalFilter} which concatenates the given filters with the boolean operator
* {@code AND}.
*
* @param filter1 the first filter to be concatenated with at least {@code filter2} and optionally additionally with
* {@code furtherFilters}.
* @param filter2 the second filter to be concatenated with {@code filter1} and {@code furtherFilters}.
* @param furtherFilters optional additional filters to be concatenated.
* @return the new logical {@code AND} search filter.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ImmutableLogicalFilter and(final SearchFilter filter1, final SearchFilter filter2,
final SearchFilter... furtherFilters) {
checkNotNull(filter1, "first filter to be concatenated with AND");
checkNotNull(filter2, "second filter to be concatenated with AND");
checkNotNull(furtherFilters, "further searchFilters to be concatenated with AND");
return new ImmutableLogicalFilter(Type.AND, paramsToList(filter1, filter2, furtherFilters));
}
/**
* Returns a new {@code ImmutableLogicalFilter} which concatenates the given filters with the boolean operator
* {@code OR}.
*
* @param filter1 the first filter to be concatenated with at least {@code filter2} and optionally additionally with
* {@code furtherFilters}.
* @param filter2 the second filter to be concatenated with {@code filter1} and {@code furtherFilters}.
* @param furtherFilters optional additional filters to be concatenated.
* @return the new logical {@code OR} search filter.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ImmutableLogicalFilter or(final SearchFilter filter1, final SearchFilter filter2,
final SearchFilter... furtherFilters) {
checkNotNull(filter1, "first filter to be concatenated with OR");
checkNotNull(filter2, "second filter to be concatenated with OR");
checkNotNull(furtherFilters, "further searchFilters to be concatenated with OR");
return new ImmutableLogicalFilter(Type.OR, paramsToList(filter1, filter2, furtherFilters));
}
@SafeVarargs
private static <T> List<T> paramsToList(final T param1, final T param2, final T... furtherParams) {
final List<T> result = new ArrayList<>(2 + furtherParams.length);
result.add(param1);
result.add(param2);
Collections.addAll(result, furtherParams);
return result;
}
@Override
public Type getType() {
return type;
}
@Override
public Collection<SearchFilter> getFilters() {
return searchFilters;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ImmutableLogicalFilter that = (ImmutableLogicalFilter) o;
return type == that.type && Objects.equals(searchFilters, that.searchFilters);
}
@Override
public int hashCode() {
return Objects.hash(type, searchFilters);
}
@Override
public String toString() {
return createFilterString();
}
private String createFilterString() {
final String delimiter = ",";
final String prefix = type.getName() + "(";
final String suffix = ")";
return searchFilters.stream()
.map(SearchFilter::toString)
.collect(Collectors.joining(delimiter, prefix, suffix));
}
}