prepare new system for getting metas in libraries
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / MetaResultList.java
CommitLineData
1f2a7d5f
NR
1package be.nikiroo.fanfix.library;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6
7import be.nikiroo.fanfix.data.MetaData;
8
9public class MetaResultList {
10 private List<MetaData> metas;
11
12 // Lazy lists:
13 // TODO: sync-protect them?
14 private List<String> sources;
15 private List<String> authors;
16 private List<String> tags;
17
18 // can be null (will consider it empty)
19 public MetaResultList(List<MetaData> metas) {
20 if (metas == null) {
21 metas = new ArrayList<MetaData>();
22 }
23
24 Collections.sort(metas);
25 this.metas = metas;
26 }
27
28 // not NULL
29 // sorted
30 public List<MetaData> getMetas() {
31 return metas;
32 }
33
34 public List<String> getSources() {
35 if (sources == null) {
36 sources = new ArrayList<String>();
37 for (MetaData meta : metas) {
38 if (!sources.contains(meta.getSource()))
39 sources.add(meta.getSource());
40 }
41 }
42
43 return sources;
44 }
45
46 // A -> (A), A/ -> (A, A/*) if we can find something for "*"
47 public List<String> getSources(String source) {
48 List<String> linked = new ArrayList<String>();
49 if (source != null && !source.isEmpty()) {
50 if (!source.endsWith("/")) {
51 linked.add(source);
52 } else {
53 linked.add(source.substring(0, source.length() - 1));
54 for (String src : getSources()) {
55 if (src.startsWith(source)) {
56 linked.add(src);
57 }
58 }
59 }
60 }
61
62 return linked;
63 }
64
65 public List<String> getAuthors() {
66 if (authors == null) {
67 authors = new ArrayList<String>();
68 for (MetaData meta : metas) {
69 if (!authors.contains(meta.getAuthor()))
70 authors.add(meta.getAuthor());
71 }
72 }
73
74 return authors;
75 }
76
77 public List<String> getTags() {
78 if (tags == null) {
79 tags = new ArrayList<String>();
80 for (MetaData meta : metas) {
81 for (String tag : meta.getTags()) {
82 if (!tags.contains(tag))
83 tags.add(tag);
84 }
85 }
86 }
87
88 return authors;
89 }
90
91 // null or empty -> no check, rest = must be included
92 // source: a source ending in "/" means "this or any source starting with this",
93 // i;e., to enable source hierarchy
94 // + sorted
95 public List<MetaData> filter(List<String> sources, List<String> authors, List<String> tags) {
96 if (sources != null && sources.isEmpty())
97 sources = null;
98 if (authors != null && authors.isEmpty())
99 authors = null;
100 if (tags != null && tags.isEmpty())
101 tags = null;
102
103 // Quick check
104 if (sources == null && authors == null && tags == null) {
105 return metas;
106 }
107
108 // allow "sources/" hierarchy
109 if (sources != null) {
110 List<String> folders = new ArrayList<String>();
111 List<String> leaves = new ArrayList<String>();
112 for (String source : sources) {
113 if (source.endsWith("/")) {
114 if (!folders.contains(source))
115 folders.add(source);
116 } else {
117 if (!leaves.contains(source))
118 leaves.add(source);
119 }
120 }
121
122 sources = leaves;
123 for (String folder : folders) {
124 for (String otherLeaf : getSources(folder)) {
125 if (!sources.contains(otherLeaf)) {
126 sources.add(otherLeaf);
127 }
128 }
129 }
130 }
131
132 List<MetaData> result = new ArrayList<MetaData>();
133 for (MetaData meta : metas) {
134 if (sources != null && !sources.contains(meta.getSource())) {
135 continue;
136 }
137 if (authors != null && !authors.contains(meta.getAuthor())) {
138 continue;
139 }
140
141 if (tags != null) {
142 boolean keep = false;
143 for (String thisTag : meta.getTags()) {
144 if (tags.contains(thisTag))
145 keep = true;
146 }
147
148 if (!keep)
149 continue;
150 }
151
152 result.add(meta);
153 }
154
155 Collections.sort(result);
156 return result;
157 }
158}