View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.list.impl;
23  
24  import org.opencastproject.list.api.ResourceListFilter;
25  import org.opencastproject.list.api.ResourceListQuery;
26  import org.opencastproject.util.data.Option;
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  public class ResourceListQueryImpl implements ResourceListQuery {
34  
35    protected final List<ResourceListFilter<?>> availableFilters = new ArrayList<>();
36    private final Map<String, ResourceListFilter<?>> filters = new HashMap<>();
37    private Option<Integer> limit;
38    private Option<Integer> offset;
39    protected Option<String> sortBy;
40  
41    public ResourceListQueryImpl() {
42      limit = Option.none();
43      offset = Option.none();
44      sortBy = Option.none();
45    }
46  
47    public void addFilter(ResourceListFilter<?> filter) {
48      this.filters.put(filter.getName(), filter);
49    }
50  
51    public void removeFilter(ResourceListFilter<?> filter) {
52      this.filters.remove(filter.getName());
53    }
54  
55    public void setLimit(Integer limit) {
56      this.limit = Option.option(limit);
57    }
58  
59    public void setOffset(Integer offset) {
60      this.offset = Option.option(offset);
61    }
62  
63    @Override
64    public List<ResourceListFilter<?>> getFilters() {
65      return new ArrayList<>(filters.values());
66    }
67  
68    @Override
69    public ResourceListFilter<?> getFilter(String name) {
70      return filters.get(name);
71    }
72  
73    @Override
74    public Option<Integer> getLimit() {
75      return limit;
76    }
77  
78    @Override
79    public Option<Integer> getOffset() {
80      return offset;
81    }
82  
83    @Override
84    public Option<String> getSortBy() {
85      return sortBy;
86    }
87  
88    @Override
89    public Boolean hasFilter(String name) {
90      return filters.containsKey(name);
91    }
92  
93    @Override
94    public List<ResourceListFilter<?>> getAvailableFilters() {
95      return availableFilters;
96    }
97  
98    /**
99     * Returns the filter value wrapped in an {@link Option} or none if the filter is not existing or has no value.
100    *
101    * @param name
102    *          the filter name
103    * @return an {@link Option} wrapping the value or none.
104    */
105   public <A> Option<A> getFilterValue(String name) {
106     if (this.hasFilter(name)) {
107       return (Option<A>) this.getFilter(name).getValue();
108     }
109 
110     return Option.none();
111   }
112 
113 }