OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | 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
 
16
import org.apache.commons.collections.Predicate;
17
 
18
public abstract class IPredicate<E> implements Predicate {
19
 
20
    private static final IPredicate<Object> truePred = new IPredicate<Object>() {
21
        @Override
22
        public boolean evaluateChecked(Object input) {
23
            return true;
24
        }
25
    };
21 ilm 26
    private static final IPredicate<Object> falsePred = new IPredicate<Object>() {
27
        @Override
28
        public boolean evaluateChecked(Object input) {
29
            return false;
30
        }
31
    };
19 ilm 32
    private static final IPredicate<Object> NotNullPred = new IPredicate<Object>() {
33
        @Override
34
        public boolean evaluateChecked(Object input) {
35
            return input != null;
36
        }
37
    };
17 ilm 38
 
39
    @SuppressWarnings("unchecked")
40
    public static final <N> IPredicate<N> truePredicate() {
41
        return (IPredicate<N>) truePred;
42
    }
43
 
44
    @SuppressWarnings("unchecked")
21 ilm 45
    public static final <N> IPredicate<N> falsePredicate() {
46
        return (IPredicate<N>) falsePred;
47
    }
48
 
49
    @SuppressWarnings("unchecked")
19 ilm 50
    public static final <N> IPredicate<N> notNullPredicate() {
51
        return (IPredicate<N>) NotNullPred;
52
    }
53
 
54
    @SuppressWarnings("unchecked")
144 ilm 55
    @Override
17 ilm 56
    public boolean evaluate(Object object) {
57
        return this.evaluateChecked((E) object);
58
    }
59
 
60
    public abstract boolean evaluateChecked(E input);
61
 
21 ilm 62
    public final <F extends E> IPredicate<F> cast() {
63
        // this class never returns E, only takes it as argument
64
        // but we cannot return this instance :
65
        // class Sub<E> extends IPredicate<E> {
66
        // E someMethod();
67
        // }
68
        // Sub<Number> n;
69
        // IPredicate<Integer> cast = n.<Integer> cast();
70
        // Sub<Integer> n2 = (Sub<Integer>) cast;
71
        // the cast in the line above will succeed but n2.someMethod()
72
        // will fail if n.someMethod() returns a BigDecimal
73
 
74
        // since we're returning a new IPredicate instance, the n2 line would correctly fail
75
        return new IPredicate<F>() {
76
            @Override
77
            public boolean evaluateChecked(F input) {
78
                return IPredicate.this.evaluateChecked(input);
79
            }
80
        };
81
    }
82
 
83
    public final IPredicate<E> not() {
84
        if (this == truePred)
85
            return falsePredicate();
86
        else if (this == falsePred)
87
            return truePredicate();
88
        else
89
            return new IPredicate<E>() {
90
                @Override
91
                public boolean evaluateChecked(E input) {
92
                    return !IPredicate.this.evaluateChecked(input);
93
                }
94
            };
95
    }
96
 
97
    public final IPredicate<E> and(final IPredicate<? super E> o) {
98
        if (o == this || o == truePred)
99
            return this;
100
        else if (this == truePred)
101
            return o.cast();
102
        else if (o == falsePred || this == falsePred)
103
            return falsePredicate();
104
        else
105
            return new IPredicate<E>() {
106
                @Override
107
                public boolean evaluateChecked(E input) {
108
                    return IPredicate.this.evaluateChecked(input) && o.evaluateChecked(input);
109
                }
110
            };
111
    }
112
 
113
    public final IPredicate<E> or(final IPredicate<? super E> o) {
114
        if (o == this || o == falsePred)
115
            return this;
116
        else if (this == falsePred)
117
            return o.cast();
118
        else if (o == truePred || this == truePred)
119
            return truePredicate();
120
        else
121
            return new IPredicate<E>() {
122
                @Override
123
                public boolean evaluateChecked(E input) {
124
                    return IPredicate.this.evaluateChecked(input) || o.evaluateChecked(input);
125
                }
126
            };
127
    }
17 ilm 128
}