Filter.java
01 /*
02  * Java GPX Library (jpx-3.1.0).
03  * Copyright (c) 2016-2023 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.jpx;
21 
22 import java.util.List;
23 import java.util.function.Function;
24 import java.util.function.Predicate;
25 
26 /**
27  * Filter interface which contains the {@code filter}, {@code map},
28  * {@code flatMap} and {@code listMap} methods for transforming values from
29  * type {@code T}.
30  *
31  @param <T> the value type for the transformed objects
32  @param <R> the result type of the filtered object
33  *
34  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
35  @version 1.1
36  @since 1.1
37  */
38 public interface Filter<T, R> {
39 
40     /**
41      * Return a filter consisting of the elements of this filter that matches
42      * the given predicate.
43      *
44      @param predicate a non-interfering, stateless predicate to apply to each
45      *        element to determine if it should be included
46      @return a new filter
47      */
48     Filter<T, R> filter(final Predicate<? super T> predicate);
49 
50     /**
51      * Return a filter with the results of applying the given mapper function.
52      *
53      @param mapper a non-interfering, stateless function to apply to each
54      *        element
55      @return a new filter
56      */
57     Filter<T, R> map(final Function<? super T, ? extends T> mapper);
58 
59     /**
60      * Return a filter consisting of the results of replacing each element with
61      * the contents of the mapped elements.
62      *
63      @param mapper a non-interfering, stateless function to apply to each
64      *        element which produces a list of new values
65      @return a new filter
66      */
67     Filter<T, R> flatMap(final Function<? super T, ? extends List<T>> mapper);
68 
69     /**
70      * Return a filter with the results of the applying given mapper function.
71      *
72      @param mapper a non-interfering, stateless function to apply to the
73      *        existing elements
74      @return a new filter
75      */
76     Filter<T, R> listMap(final Function<? super List<T>, ? extends List<T>> mapper);
77 
78     /**
79      * Return a new object of type {@code R} which contains the elements of the
80      * applied filter functions.
81      *
82      @return a new object created from the given filter
83      */
84     R build();
85 
86 }