001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    /**
018     * @author Marcellus Tavares
019     */
020    public class AggregatePredicateFilter<T> implements PredicateFilter<T> {
021    
022            public AggregatePredicateFilter(PredicateFilter<T> predicateFilter) {
023                    _predicateFilter = new IdentityPredicateFilter<>(predicateFilter);
024            }
025    
026            public AggregatePredicateFilter<T> and(PredicateFilter<T> predicateFilter) {
027                    _predicateFilter = new AndPredicateFilter<>(
028                            _predicateFilter, new IdentityPredicateFilter<>(predicateFilter));
029    
030                    return this;
031            }
032    
033            @Override
034            public boolean filter(T t) {
035                    return _predicateFilter.filter(t);
036            }
037    
038            public AggregatePredicateFilter<T> negate() {
039                    _predicateFilter = new NegatePredicateFilter<>(_predicateFilter);
040    
041                    return this;
042            }
043    
044            public AggregatePredicateFilter<T> or(PredicateFilter<T> predicateFilter) {
045                    _predicateFilter = new OrPredicateFilter<>(
046                            _predicateFilter, new IdentityPredicateFilter<>(predicateFilter));
047    
048                    return this;
049            }
050    
051            private PredicateFilter<T> _predicateFilter;
052    
053            private static class AndPredicateFilter<T> implements PredicateFilter<T> {
054    
055                    public AndPredicateFilter(
056                            PredicateFilter<T> leftPredicateFilter,
057                            PredicateFilter<T> rightPredicateFilter) {
058    
059                            _leftPredicateFilter = leftPredicateFilter;
060                            _rightPredicateFilter = rightPredicateFilter;
061                    }
062    
063                    @Override
064                    public boolean filter(T t) {
065                            if (_leftPredicateFilter.filter(t) &&
066                                    _rightPredicateFilter.filter(t)) {
067    
068                                    return true;
069                            }
070    
071                            return false;
072                    }
073    
074                    private PredicateFilter<T> _leftPredicateFilter;
075                    private PredicateFilter<T> _rightPredicateFilter;
076    
077            }
078    
079            private static class IdentityPredicateFilter<T>
080                    implements PredicateFilter<T> {
081    
082                    public IdentityPredicateFilter(PredicateFilter<T> predicateFilter) {
083                            _predicateFilter = predicateFilter;
084                    }
085    
086                    @Override
087                    public boolean filter(T t) {
088                            return _predicateFilter.filter(t);
089                    }
090    
091                    private final PredicateFilter<T> _predicateFilter;
092    
093            }
094    
095            private static class NegatePredicateFilter<T>
096                    implements PredicateFilter<T> {
097    
098                    public NegatePredicateFilter(PredicateFilter<T> predicateFilter) {
099                            _predicateFilter = predicateFilter;
100                    }
101    
102                    @Override
103                    public boolean filter(T t) {
104                            return !_predicateFilter.filter(t);
105                    }
106    
107                    private PredicateFilter<T> _predicateFilter;
108    
109            }
110    
111            private static class OrPredicateFilter<T> implements PredicateFilter<T> {
112    
113                    public OrPredicateFilter(
114                            PredicateFilter<T> leftPredicateFilter,
115                            PredicateFilter<T> rightPredicateFilter) {
116    
117                            _leftPredicateFilter = leftPredicateFilter;
118                            _rightPredicateFilter = rightPredicateFilter;
119                    }
120    
121                    @Override
122                    public boolean filter(T t) {
123                            if (_leftPredicateFilter.filter(t) ||
124                                    _rightPredicateFilter.filter(t)) {
125    
126                                    return true;
127                            }
128    
129                            return false;
130                    }
131    
132                    private PredicateFilter<T> _leftPredicateFilter;
133                    private PredicateFilter<T> _rightPredicateFilter;
134    
135            }
136    
137    }