1 package be
.nikiroo
.fanfix
.library
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Collections
;
8 import be
.nikiroo
.fanfix
.data
.MetaData
;
10 public class MetaResultList
{
11 private List
<MetaData
> metas
;
14 // TODO: sync-protect them?
15 private List
<String
> sources
;
16 private List
<String
> authors
;
17 private List
<String
> tags
;
19 // can be null (will consider it empty)
20 public MetaResultList(List
<MetaData
> metas
) {
22 metas
= new ArrayList
<MetaData
>();
25 Collections
.sort(metas
);
31 public List
<MetaData
> getMetas() {
35 public List
<String
> getSources() {
36 if (sources
== null) {
37 sources
= new ArrayList
<String
>();
38 for (MetaData meta
: metas
) {
39 if (!sources
.contains(meta
.getSource()))
40 sources
.add(meta
.getSource());
47 // A -> (A), A/ -> (A, A/*) if we can find something for "*"
48 public List
<String
> getSources(String source
) {
49 List
<String
> linked
= new ArrayList
<String
>();
50 if (source
!= null && !source
.isEmpty()) {
51 if (!source
.endsWith("/")) {
54 linked
.add(source
.substring(0, source
.length() - 1));
55 for (String src
: getSources()) {
56 if (src
.startsWith(source
)) {
66 public List
<String
> getAuthors() {
67 if (authors
== null) {
68 authors
= new ArrayList
<String
>();
69 for (MetaData meta
: metas
) {
70 if (!authors
.contains(meta
.getAuthor()))
71 authors
.add(meta
.getAuthor());
78 public List
<String
> getTags() {
80 tags
= new ArrayList
<String
>();
81 for (MetaData meta
: metas
) {
82 for (String tag
: meta
.getTags()) {
83 if (!tags
.contains(tag
))
93 public List
<MetaData
> filter(String source
, String author
, String tag
) {
94 List
<String
> sources
= source
== null ?
null : Arrays
.asList(source
);
95 List
<String
> authors
= author
== null ?
null : Arrays
.asList(author
);
96 List
<String
> tags
= tag
== null ?
null : Arrays
.asList(tag
);
98 return filter(sources
, authors
, tags
);
101 // null or empty -> no check, rest = must be included
102 // source: a source ending in "/" means "this or any source starting with this",
103 // i;e., to enable source hierarchy
105 public List
<MetaData
> filter(List
<String
> sources
, List
<String
> authors
, List
<String
> tags
) {
106 if (sources
!= null && sources
.isEmpty())
108 if (authors
!= null && authors
.isEmpty())
110 if (tags
!= null && tags
.isEmpty())
114 if (sources
== null && authors
== null && tags
== null) {
118 // allow "sources/" hierarchy
119 if (sources
!= null) {
120 List
<String
> folders
= new ArrayList
<String
>();
121 List
<String
> leaves
= new ArrayList
<String
>();
122 for (String source
: sources
) {
123 if (source
.endsWith("/")) {
124 if (!folders
.contains(source
))
127 if (!leaves
.contains(source
))
133 for (String folder
: folders
) {
134 for (String otherLeaf
: getSources(folder
)) {
135 if (!sources
.contains(otherLeaf
)) {
136 sources
.add(otherLeaf
);
142 List
<MetaData
> result
= new ArrayList
<MetaData
>();
143 for (MetaData meta
: metas
) {
144 if (sources
!= null && !sources
.contains(meta
.getSource())) {
147 if (authors
!= null && !authors
.contains(meta
.getAuthor())) {
152 boolean keep
= false;
153 for (String thisTag
: meta
.getTags()) {
154 if (tags
.contains(thisTag
))
165 Collections
.sort(result
);