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 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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