OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | 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
 
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")
17 ilm 55
    public boolean evaluate(Object object) {
56
        return this.evaluateChecked((E) object);
57
    }
58
 
59
    public abstract boolean evaluateChecked(E input);
60
 
21 ilm 61
    public final <F extends E> IPredicate<F> cast() {
62
        // this class never returns E, only takes it as argument
63
        // but we cannot return this instance :
64
        // class Sub<E> extends IPredicate<E> {
65
        // E someMethod();
66
        // }
67
        // Sub<Number> n;
68
        // IPredicate<Integer> cast = n.<Integer> cast();
69
        // Sub<Integer> n2 = (Sub<Integer>) cast;
70
        // the cast in the line above will succeed but n2.someMethod()
71
        // will fail if n.someMethod() returns a BigDecimal
72
 
73
        // since we're returning a new IPredicate instance, the n2 line would correctly fail
74
        return new IPredicate<F>() {
75
            @Override
76
            public boolean evaluateChecked(F input) {
77
                return IPredicate.this.evaluateChecked(input);
78
            }
79
        };
80
    }
81
 
82
    public final IPredicate<E> not() {
83
        if (this == truePred)
84
            return falsePredicate();
85
        else if (this == falsePred)
86
            return truePredicate();
87
        else
88
            return new IPredicate<E>() {
89
                @Override
90
                public boolean evaluateChecked(E input) {
91
                    return !IPredicate.this.evaluateChecked(input);
92
                }
93
            };
94
    }
95
 
96
    public final IPredicate<E> and(final IPredicate<? super E> o) {
97
        if (o == this || o == truePred)
98
            return this;
99
        else if (this == truePred)
100
            return o.cast();
101
        else if (o == falsePred || this == falsePred)
102
            return falsePredicate();
103
        else
104
            return new IPredicate<E>() {
105
                @Override
106
                public boolean evaluateChecked(E input) {
107
                    return IPredicate.this.evaluateChecked(input) && o.evaluateChecked(input);
108
                }
109
            };
110
    }
111
 
112
    public final IPredicate<E> or(final IPredicate<? super E> o) {
113
        if (o == this || o == falsePred)
114
            return this;
115
        else if (this == falsePred)
116
            return o.cast();
117
        else if (o == truePred || this == truePred)
118
            return truePredicate();
119
        else
120
            return new IPredicate<E>() {
121
                @Override
122
                public boolean evaluateChecked(E input) {
123
                    return IPredicate.this.evaluateChecked(input) || o.evaluateChecked(input);
124
                }
125
            };
126
    }
17 ilm 127
}