1 package be
.nikiroo
.fanfix
.library
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Collections
;
6 import java
.util
.Comparator
;
9 import be
.nikiroo
.fanfix
.data
.MetaData
;
11 public class MetaResultList
{
12 private List
<MetaData
> metas
;
15 // TODO: sync-protect them?
16 private List
<String
> sources
;
17 private List
<String
> authors
;
18 private List
<String
> tags
;
20 // can be null (will consider it empty)
21 public MetaResultList(List
<MetaData
> metas
) {
23 metas
= new ArrayList
<MetaData
>();
26 Collections
.sort(metas
);
32 public List
<MetaData
> getMetas() {
36 public List
<String
> getSources() {
37 if (sources
== null) {
38 sources
= new ArrayList
<String
>();
39 for (MetaData meta
: metas
) {
40 if (!sources
.contains(meta
.getSource()))
41 sources
.add(meta
.getSource());
49 // A -> (A), A/ -> (A, A/*) if we can find something for "*"
50 public List
<String
> getSources(String source
) {
51 List
<String
> linked
= new ArrayList
<String
>();
52 if (source
!= null && !source
.isEmpty()) {
53 if (!source
.endsWith("/")) {
56 linked
.add(source
.substring(0, source
.length() - 1));
57 for (String src
: getSources()) {
58 if (src
.startsWith(source
)) {
69 public List
<String
> getAuthors() {
70 if (authors
== null) {
71 authors
= new ArrayList
<String
>();
72 for (MetaData meta
: metas
) {
73 if (!authors
.contains(meta
.getAuthor()))
74 authors
.add(meta
.getAuthor());
82 public List
<String
> getTags() {
84 tags
= new ArrayList
<String
>();
85 for (MetaData meta
: metas
) {
86 for (String tag
: meta
.getTags()) {
87 if (!tags
.contains(tag
))
98 public List
<MetaData
> filter(String source
, String author
, String tag
) {
99 List
<String
> sources
= source
== null ?
null : Arrays
.asList(source
);
100 List
<String
> authors
= author
== null ?
null : Arrays
.asList(author
);
101 List
<String
> tags
= tag
== null ?
null : Arrays
.asList(tag
);
103 return filter(sources
, authors
, tags
);
106 // null or empty -> no check, rest = must be included
107 // source: a source ending in "/" means "this or any source starting with
109 // i;e., to enable source hierarchy
111 public List
<MetaData
> filter(List
<String
> sources
, List
<String
> authors
,
113 if (sources
!= null && sources
.isEmpty())
115 if (authors
!= null && authors
.isEmpty())
117 if (tags
!= null && tags
.isEmpty())
121 if (sources
== null && authors
== null && tags
== null) {
125 // allow "sources/" hierarchy
126 if (sources
!= null) {
127 List
<String
> folders
= new ArrayList
<String
>();
128 List
<String
> leaves
= new ArrayList
<String
>();
129 for (String source
: sources
) {
130 if (source
.endsWith("/")) {
131 if (!folders
.contains(source
))
134 if (!leaves
.contains(source
))
140 for (String folder
: folders
) {
141 for (String otherLeaf
: getSources(folder
)) {
142 if (!sources
.contains(otherLeaf
)) {
143 sources
.add(otherLeaf
);
149 List
<MetaData
> result
= new ArrayList
<MetaData
>();
150 for (MetaData meta
: metas
) {
151 if (sources
!= null && !sources
.contains(meta
.getSource())) {
154 if (authors
!= null && !authors
.contains(meta
.getAuthor())) {
159 boolean keep
= false;
160 for (String thisTag
: meta
.getTags()) {
161 if (tags
.contains(thisTag
))
172 Collections
.sort(result
);
177 * Sort the given {@link String} values, ignoring case.
182 private void sort(List
<String
> values
) {
183 Collections
.sort(values
, new Comparator
<String
>() {
185 public int compare(String o1
, String o2
) {
186 return ("" + o1
).compareToIgnoreCase("" + o2
);