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.api;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Optional;
29  
30  public class DefaultResourceListQuery implements ResourceListQuery {
31  
32    protected final List<ResourceListFilter<?>> availableFilters = new ArrayList<>();
33    private final Map<String, ResourceListFilter<?>> filters = new HashMap<>();
34    private Optional<Integer> limit;
35    private Optional<Integer> offset;
36    protected Optional<String> sortBy;
37  
38    public DefaultResourceListQuery() {
39      limit = Optional.empty();
40      offset = Optional.empty();
41      sortBy = Optional.empty();
42    }
43  
44    public void addFilter(ResourceListFilter<?> filter) {
45      this.filters.put(filter.getName(), filter);
46    }
47  
48    public void removeFilter(ResourceListFilter<?> filter) {
49      this.filters.remove(filter.getName());
50    }
51  
52    public void setLimit(Integer limit) {
53      this.limit = Optional.ofNullable(limit);
54    }
55  
56    public void setOffset(Integer offset) {
57      this.offset = Optional.ofNullable(offset);
58    }
59  
60    @Override
61    public List<ResourceListFilter<?>> getFilters() {
62      return new ArrayList<>(filters.values());
63    }
64  
65    @Override
66    public ResourceListFilter<?> getFilter(String name) {
67      return filters.get(name);
68    }
69  
70    @Override
71    public Optional<Integer> getLimit() {
72      return limit;
73    }
74  
75    @Override
76    public Optional<Integer> getOffset() {
77      return offset;
78    }
79  
80    @Override
81    public Optional<String> getSortBy() {
82      return sortBy;
83    }
84  
85    @Override
86    public Boolean hasFilter(String name) {
87      return filters.containsKey(name);
88    }
89  
90    @Override
91    public List<ResourceListFilter<?>> getAvailableFilters() {
92      return availableFilters;
93    }
94  
95    /**
96     * Returns the filter value wrapped in an {@link Optional} or none if the filter is not existing or has no value.
97     *
98     * @param name
99     *          the filter name
100    * @return an {@link Optional} wrapping the value or none.
101    */
102   public <A> Optional<A> getFilterValue(String name) {
103     if (this.hasFilter(name)) {
104       return (Optional<A>) this.getFilter(name).getValue();
105     }
106 
107     return Optional.empty();
108   }
109 
110 }