Skip to content

Commit a437d96

Browse files
committed
Documentation and licensing
1 parent e396d0a commit a437d96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1178
-731
lines changed

LICENSE

+12-670
Large diffs are not rendered by default.

application/build.gradle

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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+
121
dependencies {
222
implementation project(":common")
323
implementation "com.google.code.gson:gson:$gsonVersion"

application/src/main/java/com/noahhusby/lib/application/App.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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+
121
package com.noahhusby.lib.application;
222

323
import java.lang.annotation.ElementType;

application/src/main/java/com/noahhusby/lib/application/Application.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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+
121
package com.noahhusby.lib.application;
222

323
/**

application/src/main/java/com/noahhusby/lib/application/ApplicationHandler.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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+
121
package com.noahhusby.lib.application;
222

323
/**

application/src/main/java/com/noahhusby/lib/application/config/Config.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
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+
121
package com.noahhusby.lib.application.config;
222

323
import com.noahhusby.lib.application.config.provider.ConfigurationProvider;

application/src/main/java/com/noahhusby/lib/application/config/ConfigHandler.java

-58
This file was deleted.

application/src/main/java/com/noahhusby/lib/application/config/Configuration.java

+146-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
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+
121
package com.noahhusby.lib.application.config;
222

323
import com.google.gson.JsonElement;
24+
import com.noahhusby.lib.application.config.exception.ClassNotConfigException;
425
import com.noahhusby.lib.application.config.provider.ConfigurationProvider;
26+
import com.noahhusby.lib.application.config.source.FileConfigurationSource;
527
import com.noahhusby.lib.common.util.HusbyUtil;
628
import lombok.Getter;
729
import lombok.NonNull;
830
import lombok.RequiredArgsConstructor;
931
import lombok.SneakyThrows;
1032

33+
import java.io.File;
1134
import java.lang.reflect.Field;
35+
import java.lang.reflect.InvocationTargetException;
1236
import java.lang.reflect.Type;
37+
import java.util.HashMap;
1338
import java.util.Map;
1439

1540
/**
@@ -20,21 +45,36 @@
2045
public class Configuration {
2146
private final ConfigurationProvider provider;
2247

48+
/**
49+
* Loads the config file data
50+
*/
2351
public void load() {
2452
provider.load();
2553
}
2654

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+
*/
2762
@SneakyThrows
2863
public <T> T bind(Class<T> clazz) {
29-
provider.update(ConfigHandler.getProperties(clazz));
64+
provider.update(getProperties(clazz));
3065
JsonElement asElement = HusbyUtil.GSON.toJsonTree(provider.getEntries());
3166
return HusbyUtil.GSON.fromJson(asElement, (Type) clazz);
3267
}
3368

69+
/**
70+
* Updates config file from class, and updates config instance with values from config.
71+
*
72+
* @param clazz Config class.
73+
*/
3474
@SneakyThrows
3575
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));
3878
for (Property property : properties.values()) {
3979
Type type = property.getType();
4080
Object object = HusbyUtil.GSON.fromJson(HusbyUtil.GSON.toJson(provider.getEntries().get(property.getName())), type);
@@ -44,32 +84,135 @@ public void sync(Class<?> clazz) {
4484
}
4585
}
4686

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+
*/
4793
public boolean has(@NonNull String key) {
4894
return provider.getEntries().containsKey(key);
4995
}
5096

97+
/**
98+
* Get the specified member.
99+
*
100+
* @param key Key of member.
101+
* @return the object correlating to the specified member.
102+
*/
51103
public Object get(@NonNull String key) {
52104
return provider.getEntries().get(key);
53105
}
54106

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+
*/
55114
public Object getOrDefault(@NonNull String key, Object def) {
56115
Object object = get(key);
57116
return object == null ? def : object;
58117
}
59118

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+
*/
60125
public String getAsString(String key) {
61126
return (String) get(key);
62127
}
63128

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+
*/
64135
public String[] getAsStringList(String key) {
65136
return (String[]) get(key);
66137
}
67138

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+
*/
68145
public int getAsInteger(String key) {
69146
return (int) get(key);
70147
}
71148

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+
*/
72155
public boolean getAsBoolean(String key) {
73156
return (boolean) get(key);
74157
}
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+
}
75218
}

0 commit comments

Comments
 (0)