|
| 1 | +/* |
| 2 | + * Copyright © 2018 Knative Authors ([email protected]) |
| 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 dev.knative.eventing.kafka.broker.core.filter; |
| 17 | + |
| 18 | +import dev.knative.eventing.kafka.broker.contract.DataPlaneContract; |
| 19 | +import dev.knative.eventing.kafka.broker.core.filter.subscriptionsapi.*; |
| 20 | +import io.cloudevents.CloudEvent; |
| 21 | +import java.util.List; |
| 22 | +import java.util.function.Predicate; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +/** |
| 26 | + * This interface provides an abstraction for filtering {@link CloudEvent} instances. |
| 27 | + */ |
| 28 | +@FunctionalInterface |
| 29 | +public interface Filter extends Predicate<CloudEvent> { |
| 30 | + |
| 31 | + /** |
| 32 | + * @return noop implementation that always returns true |
| 33 | + */ |
| 34 | + static Filter noop() { |
| 35 | + return ce -> true; |
| 36 | + } |
| 37 | + |
| 38 | + static Filter fromContract(DataPlaneContract.DialectedFilter filter) { |
| 39 | + return switch (filter.getFilterCase()) { |
| 40 | + case EXACT -> new ExactFilter(filter.getExact().getAttributesMap()); |
| 41 | + case PREFIX -> new PrefixFilter(filter.getPrefix().getAttributesMap()); |
| 42 | + case SUFFIX -> new SuffixFilter(filter.getSuffix().getAttributesMap()); |
| 43 | + case NOT -> new NotFilter(fromContract(filter.getNot().getFilter())); |
| 44 | + case ANY -> new AnyFilter(filter.getAny().getFiltersList().stream() |
| 45 | + .map(Filter::fromContract) |
| 46 | + .collect(Collectors.toList())); |
| 47 | + case ALL -> new AllFilter(filter.getAll().getFiltersList().stream() |
| 48 | + .map(Filter::fromContract) |
| 49 | + .collect(Collectors.toList())); |
| 50 | + case CESQL -> new CeSqlFilter(filter.getCesql().getExpression()); |
| 51 | + default -> Filter.noop(); |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + static Filter fromContract(List<DataPlaneContract.DialectedFilter> filters) { |
| 56 | + return new AllFilter(filters.stream().map(Filter::fromContract).collect(Collectors.toList())); |
| 57 | + } |
| 58 | +} |
0 commit comments