1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.kaiserpfalzedv.commons.api.resources;
19
20 import com.fasterxml.jackson.annotation.JsonIgnore;
21 import de.kaiserpfalzedv.commons.api.store.StoreService;
22
23 import java.io.Serializable;
24 import java.util.*;
25
26
27
28
29
30
31
32 public interface DefaultResourceSpec extends Serializable, Cloneable {
33
34
35
36
37
38
39 @JsonIgnore
40 default Optional<String> getProperty(final String key) {
41 return Optional.ofNullable(getProperties().get(key));
42 }
43
44
45
46
47
48
49
50
51 @JsonIgnore
52 default String[] getDefaultProperties() {
53 throw new UnsupportedOperationException();
54 }
55
56
57
58
59
60
61
62
63
64
65 @JsonIgnore
66 default Optional<Pointer> getResourcePointer(String key) {
67 try {
68 String property = getProperty(key).orElseThrow();
69
70 return Optional.of(convertStringToResourcePointer(property));
71 } catch (NoSuchElementException e) {
72 return Optional.empty();
73 }
74 }
75
76 @JsonIgnore
77 default List<Pointer> getResourcePointers(String key) {
78 try {
79 String property = getProperty(key).orElseThrow();
80
81 String[] data = property.split(",");
82
83 ArrayList<Pointer> result = new ArrayList<>(data.length);
84 for (String p : data) {
85 result.add(convertStringToResourcePointer(p));
86 }
87
88 return result;
89 } catch (NoSuchElementException e) {
90 return new ArrayList<>();
91 }
92 }
93
94
95 @JsonIgnore
96 Pointer convertStringToResourcePointer(String property);
97
98
99
100
101
102
103
104 @JsonIgnore
105 default void saveResourcePointer(String key, Pointer pointer) {
106 if (pointer != null) {
107 String data = convertResourcePointerToString(pointer);
108
109 getProperties().put(key, data);
110 }
111 }
112
113 default String convertResourcePointerToString(Pointer pointer) {
114 return new StringJoiner("/")
115 .add(pointer.getKind())
116 .add(pointer.getApiVersion())
117 .add(pointer.getNameSpace())
118 .add(pointer.getName())
119 .toString();
120 }
121
122 @JsonIgnore
123 void saveResourcePointers(String key, Collection<Pointer> pointers);
124
125 Map<String, String> getProperties();
126 }