1 | package eu.clarin.sru.server.fcs.utils; |
---|
2 | |
---|
3 | import java.util.Collections; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import eu.clarin.sru.server.fcs.ResourceInfoInventory; |
---|
7 | import eu.clarin.sru.server.fcs.ResourceInfo; |
---|
8 | |
---|
9 | |
---|
10 | /** |
---|
11 | * A very simple resource info inventory that is initialized with a static list |
---|
12 | * of resource info records. Mostly used together with |
---|
13 | * {@link SimpleResourceInfoInventoryParser}, but it is agnostic how the static |
---|
14 | * list of resource info records is generated. |
---|
15 | * |
---|
16 | * @see ResourceInfoInventory |
---|
17 | * @see SimpleResourceInfoInventoryParser |
---|
18 | */ |
---|
19 | public class SimpleResourceInfoInventory implements ResourceInfoInventory { |
---|
20 | private final boolean pidCaseSensitive; |
---|
21 | private final List<ResourceInfo> entries; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * Constructor. |
---|
26 | * |
---|
27 | * @param entries |
---|
28 | * a static list of resource info records |
---|
29 | * @param pidCaseSensitive |
---|
30 | * <code>true</code> if comparison of persistent identifiers |
---|
31 | * should be performed case-sensitive, <code>false</code> otherwise |
---|
32 | */ |
---|
33 | public SimpleResourceInfoInventory(List<ResourceInfo> entries, |
---|
34 | boolean pidCaseSensitive) { |
---|
35 | if (entries == null) { |
---|
36 | throw new NullPointerException("entries == null"); |
---|
37 | } |
---|
38 | this.entries = Collections.unmodifiableList(entries); |
---|
39 | this.pidCaseSensitive = pidCaseSensitive; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | @Override |
---|
44 | public void destroy() { |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | @Override |
---|
49 | public List<ResourceInfo> getResourceInfoList(String id) { |
---|
50 | if (id == null) { |
---|
51 | throw new NullPointerException("id == null"); |
---|
52 | } |
---|
53 | if (id.isEmpty()) { |
---|
54 | throw new IllegalArgumentException("id is empty"); |
---|
55 | } |
---|
56 | if (!pidCaseSensitive) { |
---|
57 | id = id.toLowerCase(); |
---|
58 | } |
---|
59 | if (id.equals(PID_ROOT)) { |
---|
60 | return entries; |
---|
61 | } else { |
---|
62 | ResourceInfo ri = findRecursive(entries, id); |
---|
63 | if (ri != null) { |
---|
64 | return ri.getSubResources(); |
---|
65 | } |
---|
66 | } |
---|
67 | return null; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | private ResourceInfo findRecursive(List<ResourceInfo> items, String pid) { |
---|
72 | if ((items != null) && !items.isEmpty()) { |
---|
73 | for (ResourceInfo item : items) { |
---|
74 | if (pidCaseSensitive) { |
---|
75 | if (pid.equals(item.getPid())) { |
---|
76 | return item; |
---|
77 | } |
---|
78 | } else { |
---|
79 | if (pid.equalsIgnoreCase(item.getPid())) { |
---|
80 | return item; |
---|
81 | } |
---|
82 | } |
---|
83 | if (item.hasSubResources()) { |
---|
84 | ResourceInfo ri = |
---|
85 | findRecursive(item.getSubResources(), pid); |
---|
86 | if (ri != null) { |
---|
87 | return ri; |
---|
88 | } |
---|
89 | } |
---|
90 | } // for |
---|
91 | } |
---|
92 | return null; |
---|
93 | } |
---|
94 | |
---|
95 | } // class SimpleResourceInfoInventory |
---|