OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.cc;
15
 
180 ilm 16
public abstract class IPredicate<E> {
17 ilm 17
 
18
    private static final IPredicate<Object> truePred = new IPredicate<Object>() {
19
        @Override
20
        public boolean evaluateChecked(Object input) {
21
            return true;
22
        }
23
    };
21 ilm 24
    private static final IPredicate<Object> falsePred = new IPredicate<Object>() {
25
        @Override
26
        public boolean evaluateChecked(Object input) {
27
            return false;
28
        }
29
    };
19 ilm 30
    private static final IPredicate<Object> NotNullPred = new IPredicate<Object>() {
31
        @Override
32
        public boolean evaluateChecked(Object input) {
33
            return input != null;
34
        }
35
    };
17 ilm 36
 
37
    @SuppressWarnings("unchecked")
38
    public static final <N> IPredicate<N> truePredicate() {
39
        return (IPredicate<N>) truePred;
40
    }
41
 
42
    @SuppressWarnings("unchecked")
21 ilm 43
    public static final <N> IPredicate<N> falsePredicate() {
44
        return (IPredicate<N>) falsePred;
45
    }
46
 
47
    @SuppressWarnings("unchecked")
19 ilm 48
    public static final <N> IPredicate<N> notNullPredicate() {
49
        return (IPredicate<N>) NotNullPred;
50
    }
51
 
17 ilm 52
    public abstract boolean evaluateChecked(E input);
53
 
21 ilm 54
    public final <F extends E> IPredicate<F> cast() {
55
        // this class never returns E, only takes it as argument
56
        // but we cannot return this instance :
57
        // class Sub<E> extends IPredicate<E> {
58
        // E someMethod();
59
        // }
60
        // Sub<Number> n;
61
        // IPredicate<Integer> cast = n.<Integer> cast();
62
        // Sub<Integer> n2 = (Sub<Integer>) cast;
63
        // the cast in the line above will succeed but n2.someMethod()
64
        // will fail if n.someMethod() returns a BigDecimal
65
 
66
        // since we're returning a new IPredicate instance, the n2 line would correctly fail
67
        return new IPredicate<F>() {
68
            @Override
69
            public boolean evaluateChecked(F input) {
70
                return IPredicate.this.evaluateChecked(input);
71
            }
72
        };
73
    }
74
 
75
    public final IPredicate<E> not() {
76
        if (this == truePred)
77
            return falsePredicate();
78
        else if (this == falsePred)
79
            return truePredicate();
80
        else
81
            return new IPredicate<E>() {
82
                @Override
83
                public boolean evaluateChecked(E input) {
84
                    return !IPredicate.this.evaluateChecked(input);
85
                }
86
            };
87
    }
88
 
89
    public final IPredicate<E> and(final IPredicate<? super E> o) {
90
        if (o == this || o == truePred)
91
            return this;
92
        else if (this == truePred)
93
            return o.cast();
94
        else if (o == falsePred || this == falsePred)
95
            return falsePredicate();
96
        else
97
            return new IPredicate<E>() {
98
                @Override
99
                public boolean evaluateChecked(E input) {
100
                    return IPredicate.this.evaluateChecked(input) && o.evaluateChecked(input);
101
                }
102
            };
103
    }
104
 
105
    public final IPredicate<E> or(final IPredicate<? super E> o) {
106
        if (o == this || o == falsePred)
107
            return this;
108
        else if (this == falsePred)
109
            return o.cast();
110
        else if (o == truePred || this == truePred)
111
            return truePredicate();
112
        else
113
            return new IPredicate<E>() {
114
                @Override
115
                public boolean evaluateChecked(E input) {
116
                    return IPredicate.this.evaluateChecked(input) || o.evaluateChecked(input);
117
                }
118
            };
119
    }
17 ilm 120
}