fix library caching issues and change get-by-source, by-author.. into result-list
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / MetaResultList.java
1 package be.nikiroo.fanfix.library;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import be.nikiroo.fanfix.data.MetaData;
9
10 public class MetaResultList {
11 private List<MetaData> metas;
12
13 // Lazy lists:
14 // TODO: sync-protect them?
15 private List<String> sources;
16 private List<String> authors;
17 private List<String> tags;
18
19 // can be null (will consider it empty)
20 public MetaResultList(List<MetaData> metas) {
21 if (metas == null) {
22 metas = new ArrayList<MetaData>();
23 }
24
25 Collections.sort(metas);
26 this.metas = metas;
27 }
28
29 // not NULL
30 // sorted
31 public List<MetaData> getMetas() {
32 return metas;
33 }
34
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());
41 }
42 }
43
44 return sources;
45 }
46
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("/")) {
52 linked.add(source);
53 } else {
54 linked.add(source.substring(0, source.length() - 1));
55 for (String src : getSources()) {
56 if (src.startsWith(source)) {
57 linked.add(src);
58 }
59 }
60 }
61 }
62
63 return linked;
64 }
65
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());
72 }
73 }
74
75 return authors;
76 }
77
78 public List<String> getTags() {
79 if (tags == null) {
80 tags = new ArrayList<String>();
81 for (MetaData meta : metas) {
82 for (String tag : meta.getTags()) {
83 if (!tags.contains(tag))
84 tags.add(tag);
85 }
86 }
87 }
88
89 return authors;
90 }
91
92 // helper
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);
97
98 return filter(sources, authors, tags);
99 }
100
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
104 // + sorted
105 public List<MetaData> filter(List<String> sources, List<String> authors, List<String> tags) {
106 if (sources != null && sources.isEmpty())
107 sources = null;
108 if (authors != null && authors.isEmpty())
109 authors = null;
110 if (tags != null && tags.isEmpty())
111 tags = null;
112
113 // Quick check
114 if (sources == null && authors == null && tags == null) {
115 return metas;
116 }
117
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))
125 folders.add(source);
126 } else {
127 if (!leaves.contains(source))
128 leaves.add(source);
129 }
130 }
131
132 sources = leaves;
133 for (String folder : folders) {
134 for (String otherLeaf : getSources(folder)) {
135 if (!sources.contains(otherLeaf)) {
136 sources.add(otherLeaf);
137 }
138 }
139 }
140 }
141
142 List<MetaData> result = new ArrayList<MetaData>();
143 for (MetaData meta : metas) {
144 if (sources != null && !sources.contains(meta.getSource())) {
145 continue;
146 }
147 if (authors != null && !authors.contains(meta.getAuthor())) {
148 continue;
149 }
150
151 if (tags != null) {
152 boolean keep = false;
153 for (String thisTag : meta.getTags()) {
154 if (tags.contains(thisTag))
155 keep = true;
156 }
157
158 if (!keep)
159 continue;
160 }
161
162 result.add(meta);
163 }
164
165 Collections.sort(result);
166 return result;
167 }
168 }