1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright 2020-2021 noahhusby
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
7
+ * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
8
+ * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
9
+ * is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
+ *
19
+ */
20
+
1
21
package com .noahhusby .lib .application .config ;
2
22
3
23
import com .google .gson .JsonElement ;
24
+ import com .noahhusby .lib .application .config .exception .ClassNotConfigException ;
4
25
import com .noahhusby .lib .application .config .provider .ConfigurationProvider ;
26
+ import com .noahhusby .lib .application .config .source .FileConfigurationSource ;
5
27
import com .noahhusby .lib .common .util .HusbyUtil ;
6
28
import lombok .Getter ;
7
29
import lombok .NonNull ;
8
30
import lombok .RequiredArgsConstructor ;
9
31
import lombok .SneakyThrows ;
10
32
33
+ import java .io .File ;
11
34
import java .lang .reflect .Field ;
35
+ import java .lang .reflect .InvocationTargetException ;
12
36
import java .lang .reflect .Type ;
37
+ import java .util .HashMap ;
13
38
import java .util .Map ;
14
39
15
40
/**
20
45
public class Configuration {
21
46
private final ConfigurationProvider provider ;
22
47
48
+ /**
49
+ * Loads the config file data
50
+ */
23
51
public void load () {
24
52
provider .load ();
25
53
}
26
54
55
+ /**
56
+ * Gets a config class object from the config file.
57
+ *
58
+ * @param clazz Config class.
59
+ * @param <T> Config class type.
60
+ * @return T class
61
+ */
27
62
@ SneakyThrows
28
63
public <T > T bind (Class <T > clazz ) {
29
- provider .update (ConfigHandler . getProperties (clazz ));
64
+ provider .update (getProperties (clazz ));
30
65
JsonElement asElement = HusbyUtil .GSON .toJsonTree (provider .getEntries ());
31
66
return HusbyUtil .GSON .fromJson (asElement , (Type ) clazz );
32
67
}
33
68
69
+ /**
70
+ * Updates config file from class, and updates config instance with values from config.
71
+ *
72
+ * @param clazz Config class.
73
+ */
34
74
@ SneakyThrows
35
75
public void sync (Class <?> clazz ) {
36
- Map <String , Property > properties = ConfigHandler . getProperties (clazz );
37
- provider .update (ConfigHandler . getProperties (clazz ));
76
+ Map <String , Property > properties = getProperties (clazz );
77
+ provider .update (getProperties (clazz ));
38
78
for (Property property : properties .values ()) {
39
79
Type type = property .getType ();
40
80
Object object = HusbyUtil .GSON .fromJson (HusbyUtil .GSON .toJson (provider .getEntries ().get (property .getName ())), type );
@@ -44,32 +84,135 @@ public void sync(Class<?> clazz) {
44
84
}
45
85
}
46
86
87
+ /**
88
+ * Checks if the config contains the specified member.
89
+ *
90
+ * @param key Key of member.
91
+ * @return True if the member exists, false if not.
92
+ */
47
93
public boolean has (@ NonNull String key ) {
48
94
return provider .getEntries ().containsKey (key );
49
95
}
50
96
97
+ /**
98
+ * Get the specified member.
99
+ *
100
+ * @param key Key of member.
101
+ * @return the object correlating to the specified member.
102
+ */
51
103
public Object get (@ NonNull String key ) {
52
104
return provider .getEntries ().get (key );
53
105
}
54
106
107
+ /**
108
+ * Get the specified member or a default value if the specific member does not exist.
109
+ *
110
+ * @param key Key of member.
111
+ * @param def Default object if member doesn't exist.
112
+ * @return the object correlating to the specified member or a default value if the specific member does not exist.
113
+ */
55
114
public Object getOrDefault (@ NonNull String key , Object def ) {
56
115
Object object = get (key );
57
116
return object == null ? def : object ;
58
117
}
59
118
119
+ /**
120
+ * Get the specified member as a string.
121
+ *
122
+ * @param key Key of member.
123
+ * @return the string correlating to the specified member.
124
+ */
60
125
public String getAsString (String key ) {
61
126
return (String ) get (key );
62
127
}
63
128
129
+ /**
130
+ * Get the specified member as a string array.
131
+ *
132
+ * @param key Key of member.
133
+ * @return the string array correlating to the specified member.
134
+ */
64
135
public String [] getAsStringList (String key ) {
65
136
return (String []) get (key );
66
137
}
67
138
139
+ /**
140
+ * Get the specified member as an integer.
141
+ *
142
+ * @param key Key of member.
143
+ * @return the integer correlating to the specified member.
144
+ */
68
145
public int getAsInteger (String key ) {
69
146
return (int ) get (key );
70
147
}
71
148
149
+ /**
150
+ * Get the specified member as a boolean.
151
+ *
152
+ * @param key Key of member.
153
+ * @return the boolean correlating to the specified member.
154
+ */
72
155
public boolean getAsBoolean (String key ) {
73
156
return (boolean ) get (key );
74
157
}
158
+
159
+ /**
160
+ * Create a configuration instance from a class with the {@link Config} annotation.
161
+ *
162
+ * @param clazz Class with a {@link Config} annotation.
163
+ * @return {@link Configuration}.
164
+ * @throws ClassNotConfigException if the class does not have the {@link Config} annotation.
165
+ */
166
+ public static Configuration of (@ NonNull Class <?> clazz ) throws ClassNotConfigException {
167
+ return of (clazz , new File (System .getProperty ("user.dir" )));
168
+ }
169
+
170
+ /**
171
+ * Create a configuration instance from a class with the {@link Config} annotation.
172
+ *
173
+ * @param clazz Class with a {@link Config} annotation.
174
+ * @param directory A custom directory for the file.
175
+ * @return {@link Configuration} instance.
176
+ * @throws ClassNotConfigException if the class does not have the {@link Config} annotation.
177
+ */
178
+ public static Configuration of (@ NonNull Class <?> clazz , @ NonNull File directory ) throws ClassNotConfigException {
179
+ if (!clazz .isAnnotationPresent (Config .class )) {
180
+ throw new ClassNotConfigException ();
181
+ }
182
+
183
+ Config config = clazz .getAnnotation (Config .class );
184
+ String fileName = config .name ().contains ("." ) ? config .name () : config .name () + config .type ().getExtension ();
185
+ FileConfigurationSource fileSource = new FileConfigurationSource (new File (directory , fileName ));
186
+ Class <? extends ConfigurationProvider > configurationProviderClass = config .type ().getProvider ();
187
+ try {
188
+ ConfigurationProvider provider = (ConfigurationProvider ) configurationProviderClass .getConstructors ()[0 ].newInstance (fileSource );
189
+ return new Configuration (provider );
190
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
191
+ e .printStackTrace ();
192
+ }
193
+
194
+ return null ;
195
+ }
196
+
197
+ /**
198
+ * Get a map of properties from a config class.
199
+ *
200
+ * @param clazz Class with a {@link Config} annotation.
201
+ * @return A map of properties
202
+ * @throws IllegalAccessException if a given property cannot be accessed
203
+ */
204
+ public static Map <String , Property > getProperties (Class <?> clazz ) throws IllegalAccessException {
205
+ Map <String , Property > properties = new HashMap <>();
206
+ for (Field field : clazz .getFields ()) {
207
+ if (field .isAnnotationPresent (Config .Ignore .class )) {
208
+ continue ;
209
+ }
210
+ Config .Name nameAnnotation = field .getAnnotation (Config .Name .class );
211
+ Config .Comment commentAnnotation = field .getAnnotation (Config .Comment .class );
212
+ String name = nameAnnotation == null ? field .getName () : nameAnnotation .value ();
213
+ String [] comment = commentAnnotation == null ? null : commentAnnotation .value ();
214
+ properties .put (name , new Property (name , comment , field .get (clazz ), field .getType (), field ));
215
+ }
216
+ return properties ;
217
+ }
75
218
}
0 commit comments