View Javadoc
1   /*
2    * Copyright (C) 2003-2012 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.social.common.jcr.filter;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  
24  public abstract class AbstractFilterLiteral<P> implements FilterLiteral<P> {
25  
26    //TODO Improves ThreadLocal here
27    private List<FilterOption<P>> filterOptions = new ArrayList<FilterOption<P>>(0);
28    //
29    private List<OrderByOption<P>> orderByOptions = new ArrayList<OrderByOption<P>>(0);
30    
31    protected abstract void start();
32    
33    public abstract void destroy();
34    
35    public AbstractFilterLiteral() {
36      start();
37    }
38  
39    @Override
40    public FilterLiteral<P> append(OrderByOption<P> orderBy) {
41      
42      if (orderByOptions.contains(orderBy) == false) {
43        List<OrderByOption<P>> results = new ArrayList<OrderByOption<P>>(orderByOptions.size() + 1);
44        results.addAll(orderByOptions);
45        // setTarget
46        orderBy.setTarget(this);
47        // Add the Filter Option to the new position.
48        results.add(orderBy);
49        orderByOptions = results;
50      }
51  
52      return this;
53    }
54    
55    @Override
56    public OrderByOption<P> with(OrderByOption<P> orderBy) {
57      this.append(orderBy);
58      return this.get(orderBy);
59    }
60  
61    @Override
62    public FilterLiteral<P> remove(OrderByOption<P> orderBy) {
63      orderByOptions.remove(orderBy);
64      return this;
65    }
66  
67    @Override
68    public void clear() {
69      filterOptions = new ArrayList<FilterOption<P>>(0);
70      orderByOptions = new ArrayList<OrderByOption<P>>(0);
71    }
72  
73  
74    @Override
75    public OrderByOption<P> get(OrderByOption<P> orderBy) {
76      return orderByOptions.indexOf(orderBy) > -1 ? orderByOptions.get(orderByOptions.indexOf(orderBy)) : null;
77    }
78    
79    @Override
80    public Iterator<OrderByOption<P>> getOrders() {
81      return orderByOptions.iterator();
82    }
83  
84    @Override
85    public FilterLiteral<P> append(FilterOption<P> filter) {
86      if (filterOptions.contains(filter) == false) {
87        List<FilterOption<P>> results = new ArrayList<FilterOption<P>>(filterOptions.size() + 1);
88        results.addAll(filterOptions);
89        
90        // setTarget
91        filter.setTarget(this);
92        // Add the Filter Option to the new position.
93        results.add(filter);
94        filterOptions = results;
95      }
96  
97      return this;
98    }
99  
100   @Override
101   public FilterLiteral<P> remove(FilterOption<P> filter) {
102     filterOptions.remove(filter);
103     return this;
104   }
105 
106   @Override
107   public FilterOption<P> get(FilterOption<P> filter) {
108     return filterOptions.indexOf(filter) > -1 ? filterOptions.get(filterOptions.indexOf(filter)) : null;
109   }
110   
111   @Override
112   public FilterOption<P> with(FilterOption<P> filter) {
113     this.append(filter);
114     return this.get(filter);
115   }
116 }