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  package org.opencastproject.assetmanager.api.query;
22  
23  import org.opencastproject.assetmanager.api.Property;
24  import org.opencastproject.assetmanager.api.Snapshot;
25  import org.opencastproject.assetmanager.api.Version;
26  import org.opencastproject.assetmanager.api.fn.ARecords;
27  
28  import java.util.Iterator;
29  import java.util.LinkedHashSet;
30  import java.util.List;
31  import java.util.Optional;
32  import java.util.stream.Collectors;
33  
34  /**
35   * Extensions for {@link AResult}.
36   */
37  public final class RichAResult implements AResult {
38    private final AResult result;
39  
40    public RichAResult(AResult result) {
41      this.result = result;
42    }
43  
44    public List<Property> getProperties() {
45      return ARecords.getProperties(result.getRecords());
46    }
47  
48    /** Count all properties contained in the result. */
49    public int countProperties() {
50      return getProperties().size();
51    }
52  
53    /** Get all selected snapshots. */
54    public List<Snapshot> getSnapshots() {
55      return result.getRecords()
56          .stream()
57          .map(record -> record.getSnapshot())
58          .filter(Optional::isPresent)
59          .map(Optional::get)
60          .collect(Collectors.toList());
61    }
62  
63    /** Count all snapshots contained in the result. */
64    public int countSnapshots() {
65      return getSnapshots().size();
66    }
67  
68    /** Get all selected versions. */
69    public List<Version> getVersions() {
70      return getSnapshots()
71          .stream()
72          .map(snapshot -> snapshot.getVersion())
73          .collect(Collectors.toList());
74    }
75  
76    //
77    // delegates
78    //
79  
80    @Override public Iterator<ARecord> iterator() {
81      return result.iterator();
82    }
83  
84    @Override public LinkedHashSet<ARecord> getRecords() {
85      return result.getRecords();
86    }
87  
88    @Override public long getSize() {
89      return result.getSize();
90    }
91  
92    @Override public String getQuery() {
93      return result.getQuery();
94    }
95  
96    @Override public long getTotalSize() {
97      return result.getTotalSize();
98    }
99  
100   @Override public long getLimit() {
101     return result.getLimit();
102   }
103 
104   @Override public long getOffset() {
105     return result.getOffset();
106   }
107 
108   @Override public long getSearchTime() {
109     return result.getSearchTime();
110   }
111 }