blob: 6e7c243bf4fefc76a50886c011dcfbcb37dd3a24 (
plain) (
blame)
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
|
/*******************************************************************************
* Copyright (c) 2016 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.common.utility.internal.transformer;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.predicate.Predicate;
import org.eclipse.jpt.common.utility.transformer.Transformer;
/**
* Tranformer that "filters" its input.
* If the input is a member of the set defined by the transformer's filter,
* the transformer simply returns the input;
* otherwise, the transformer will return the default output.
*
* @param <I> input: the type of the object passed to and
* returned by the transformer
*/
public class FilteringTransformer<I>
implements Transformer<I, I>
{
private final Predicate<? super I> filter;
private final I defaultOutput;
public FilteringTransformer(Predicate<? super I> filter, I defaultOutput) {
super();
if (filter == null) {
throw new NullPointerException();
}
this.filter = filter;
this.defaultOutput = defaultOutput;
}
public I transform(I input) {
return this.filter.evaluate(input) ? input : this.defaultOutput;
}
public Predicate<? super I> getFilter() {
return this.filter;
}
public I getDefaultOutput() {
return this.defaultOutput;
}
@Override
public String toString() {
return ObjectTools.toString(this, this.filter);
}
}
|