1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.admin.domain.model;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26
27 import com.google.common.collect.LinkedListMultimap;
28 import com.google.common.collect.Multimap;
29
30 import de.smartics.properties.api.core.domain.PropertyDescriptor;
31 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
32 import de.smartics.properties.api.core.domain.PropertyKey;
33
34
35
36
37 public class PropertyDescriptors implements Serializable
38 {
39
40
41
42
43
44
45
46 private static final long serialVersionUID = 1L;
47
48
49
50
51
52
53 private final PropertyDescriptorRegistry registry;
54
55
56
57
58 private final Multimap<String, PropertyDescriptor> sets = LinkedListMultimap
59 .create();
60
61
62
63
64
65
66
67
68
69
70 public PropertyDescriptors(final PropertyDescriptorRegistry registry)
71 {
72 this.registry = registry;
73 init();
74 }
75
76
77
78
79
80
81
82 private void init()
83 {
84 final Set<Class<?>> propertySets = registry.getPropertySetTypes();
85 for (final Class<?> propertySet : propertySets)
86 {
87 final List<? extends PropertyDescriptor> descriptors =
88 registry.get(propertySet);
89 if (descriptors.isEmpty())
90 {
91 continue;
92 }
93
94 final String propertySetName = getPropertySetName(descriptors);
95
96 for (final PropertyDescriptor descriptor : descriptors)
97 {
98 sets.put(propertySetName, descriptor);
99 }
100 }
101 }
102
103 private String getPropertySetName(
104 final List<? extends PropertyDescriptor> descriptors)
105 {
106 final PropertyDescriptor firstDescriptor = descriptors.get(0);
107 final PropertyKey key = firstDescriptor.getKey();
108 return key.getPropertySet();
109 }
110
111
112
113
114
115
116
117
118 public PropertyDescriptorRegistry getRegistry()
119 {
120 return registry;
121 }
122
123
124
125
126
127
128 public Collection<String> getPropertyKeys()
129 {
130 return new TreeSet<String>(sets.keySet());
131 }
132
133
134
135
136
137
138
139 public List<PropertyDescriptor> get(final String propertySet)
140 {
141 final List<PropertyDescriptor> descriptors =
142 new ArrayList<PropertyDescriptor>(sets.get(propertySet));
143 Collections.sort(descriptors, new Comparator<PropertyDescriptor>()
144 {
145 @Override
146 public int compare(final PropertyDescriptor o1,
147 final PropertyDescriptor o2)
148 {
149 return o1.getKey().compareTo(o2.getKey());
150 }
151 });
152 return descriptors;
153 }
154
155
156
157
158
159 }