1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
46 orderBy.setTarget(this);
47
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
91 filter.setTarget(this);
92
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 }