1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.support;
17
18 import java.util.Properties;
19
20 import javax.annotation.concurrent.NotThreadSafe;
21
22 import de.smartics.properties.api.config.domain.Property;
23 import de.smartics.properties.api.config.domain.PropertyCollection;
24 import de.smartics.properties.api.config.domain.PropertyLocation;
25 import de.smartics.properties.api.config.domain.PropertyProvider;
26 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
27 import de.smartics.util.lang.Arg;
28
29
30
31
32
33 @NotThreadSafe
34 public final class PropertiesPropertyProvider implements PropertyProvider
35 {
36
37
38
39
40
41
42
43
44
45 private static final long serialVersionUID = 1L;
46
47
48
49
50
51
52
53
54 private final ConfigurationKey<?> configurationKey;
55
56
57
58
59
60
61 private final PropertyLocation sourceId;
62
63
64
65
66
67
68 private final Properties properties;
69
70
71
72
73
74
75
76
77
78
79
80
81 public PropertiesPropertyProvider(final ConfigurationKey<?> configurationKey,
82 final PropertyLocation sourceId, final Properties properties)
83 throws NullPointerException
84 {
85 this.configurationKey =
86 Arg.checkNotNull("configurationKey", configurationKey);
87 this.sourceId = Arg.checkNotNull("sourceId", sourceId);
88 this.properties = Arg.checkNotNull("properties", properties);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103 @Override
104 public ConfigurationKey<?> getConfigurationKey()
105 {
106 return configurationKey;
107 }
108
109 @Override
110 public PropertyLocation getSourceId()
111 {
112 return sourceId;
113 }
114
115 @Override
116 public boolean isLazy()
117 {
118 return false;
119 }
120
121
122
123 @Override
124 public Property getProperty(final String name) throws NullPointerException,
125 IllegalArgumentException
126 {
127 Arg.checkNotNull("name", name);
128 final Property property = ensureProperty(name, properties.get(name));
129 return property;
130 }
131
132 private Property ensureProperty(final String name, final Object value)
133 {
134 if (value instanceof Property)
135 {
136 return (Property) value;
137 }
138 final Property property = new Property(sourceId, name, value);
139 return property;
140 }
141
142 @Override
143 public PropertyCollection getProperties()
144 {
145 return new PropertiesPropertyCollection(properties);
146 }
147
148 @Override
149 public boolean containsKey(final String name) throws NullPointerException
150 {
151 Arg.checkNotNull("name", name);
152 return properties.containsKey(name);
153 }
154
155
156
157
158
159
160
161
162 @Override
163 public String toString()
164 {
165 final StringBuilder buffer = new StringBuilder();
166
167 buffer.append(sourceId).append('=').append(properties);
168
169 return buffer.toString();
170 }
171 }