Skip to content

Commit a5cca24

Browse files
committed
Initial Commit
0 parents  commit a5cca24

7 files changed

Lines changed: 682 additions & 0 deletions

File tree

.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff
5+
.idea
6+
.idea/**/workspace.xml
7+
.idea/**/tasks.xml
8+
.idea/**/usage.statistics.xml
9+
.idea/**/dictionaries
10+
.idea/**/shelf
11+
12+
# Generated files
13+
.idea/**/contentModel.xml
14+
15+
# Sensitive or high-churn files
16+
.idea/**/dataSources/
17+
.idea/**/dataSources.ids
18+
.idea/**/dataSources.local.xml
19+
.idea/**/sqlDataSources.xml
20+
.idea/**/dynamic.xml
21+
.idea/**/uiDesigner.xml
22+
.idea/**/dbnavigator.xml
23+
24+
# Gradle
25+
.idea/**/gradle.xml
26+
.idea/**/libraries
27+
28+
# Gradle and Maven with auto-import
29+
# When using Gradle or Maven with auto-import, you should exclude module files,
30+
# since they will be recreated, and may cause churn. Uncomment if using
31+
# auto-import.
32+
# .idea/artifacts
33+
# .idea/compiler.xml
34+
# .idea/jarRepositories.xml
35+
# .idea/modules.xml
36+
# .idea/*.iml
37+
# .idea/modules
38+
# *.iml
39+
# *.ipr
40+
41+
# CMake
42+
cmake-build-*/
43+
44+
# Mongo Explorer plugin
45+
.idea/**/mongoSettings.xml
46+
47+
# File-based project format
48+
*.iws
49+
50+
# IntelliJ
51+
out/
52+
53+
# mpeltonen/sbt-idea plugin
54+
.idea_modules/
55+
56+
# JIRA plugin
57+
atlassian-ide-plugin.xml
58+
59+
# Cursive Clojure plugin
60+
.idea/replstate.xml
61+
62+
# Crashlytics plugin (for Android Studio and IntelliJ)
63+
com_crashlytics_export_strings.xml
64+
crashlytics.properties
65+
crashlytics-build.properties
66+
fabric.properties
67+
68+
# Editor-based Rest Client
69+
.idea/httpRequests
70+
71+
# Android studio 3.1+ serialized cache file
72+
.idea/caches/build_file_checksums.ser
73+
74+
target

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# VertConfig (Json Config Api)
2+
3+
VertConfig strives to provide a clean and easy to use JSON config api.
4+
5+
## Info
6+
7+
**JavaDocs:** https://javadocs.vertcode.eu/vertconfigs/
8+
9+
**Discord:** VertCode#0001
10+
11+
## Download
12+
13+
**Last Stable Version:** `1.0.0`
14+
15+
**Maven**
16+
17+
```xml
18+
<repository>
19+
<id>vertcode</id>
20+
<url>https://repo.wesleybreukers.nl/repository/vertcode/</url>
21+
</repository>
22+
```
23+
24+
```xml
25+
<dependency>
26+
<groupId>eu.vertcode</groupId>
27+
<artifactId>vertconfig</artifactId>
28+
<version>VERSION</version>
29+
</dependency>
30+
```
31+
32+
**Gradle**
33+
34+
```gradle
35+
dependencies {
36+
compile 'eu.vertcode:vertconfig:VERSION'
37+
}
38+
39+
repositories {
40+
mavenCentral()
41+
maven {
42+
name 'vertcode'
43+
url 'https://repo.wesleybreukers.nl/repository/vertcode/'
44+
}
45+
}
46+
```
47+
48+
## Usage
49+
50+
**Example Class**
51+
52+
```java
53+
import eu.vertcode.vertconfig.VertConfigs;
54+
import eu.vertcode.vertconfig.object.VertConfig;
55+
56+
import java.io.File;
57+
import java.io.IOException;
58+
import java.util.Arrays;
59+
import java.util.List;
60+
61+
public class Example {
62+
63+
public static void main(String[] args) throws IOException {
64+
VertConfig config = VertConfigs.getInstance().getConfig(new File("config.json"),
65+
Example.class.getClassLoader().getResourceAsStream("config.json"));
66+
67+
String example1 = config.getString("example.1");
68+
List<String> examples = config.getStringList("examples");
69+
70+
System.out.println("example1 = " + example1);
71+
System.out.println("examples = " + String.join(", ", examples));
72+
73+
config.set("example.1", "Hello!");
74+
config.set("examples", Arrays.asList("Example3", "Example4"));
75+
config.save();
76+
77+
example1 = config.getString("example.1");
78+
examples = config.getStringList("examples");
79+
80+
System.out.println("example1 = " + example1);
81+
System.out.println("examples = " + String.join(", ", examples));
82+
}
83+
84+
}
85+
```
86+
87+
**config.json in the resources folder**
88+
89+
```json
90+
{
91+
"example": {
92+
"1": "Example1"
93+
},
94+
"examples": [
95+
"Example1",
96+
"Example2"
97+
]
98+
}
99+
```
100+
101+
**Output**
102+
103+
```console
104+
example1 = "Example1"
105+
examples = Example1, Example2
106+
example1 = "Hello!"
107+
examples = Example3, Example4
108+
```

VertConfig.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

pom.xml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>eu.vertcode</groupId>
8+
<artifactId>vertconfig</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
<description>The easy to use JSON config api.</description>
12+
<url>https://vertcode.eu</url>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<finalName>InfusedLibrary-Shared</finalName>
21+
<defaultGoal>clean package</defaultGoal>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-source-plugin</artifactId>
26+
<version>3.2.1</version>
27+
<configuration>
28+
<attach>true</attach>
29+
</configuration>
30+
<executions>
31+
<execution>
32+
<id>attach-sources</id>
33+
<goals>
34+
<goal>jar</goal>
35+
</goals>
36+
</execution>
37+
</executions>
38+
</plugin>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-compiler-plugin</artifactId>
42+
<version>3.8.1</version>
43+
<configuration>
44+
<source>${java.version}</source>
45+
<target>${java.version}</target>
46+
</configuration>
47+
</plugin>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-shade-plugin</artifactId>
51+
<version>3.2.4</version>
52+
<executions>
53+
<execution>
54+
<phase>package</phase>
55+
<goals>
56+
<goal>shade</goal>
57+
</goals>
58+
<configuration>
59+
<createDependencyReducedPom>false</createDependencyReducedPom>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
<resources>
66+
<resource>
67+
<directory>src/main/resources</directory>
68+
<filtering>true</filtering>
69+
</resource>
70+
</resources>
71+
</build>
72+
73+
<distributionManagement>
74+
<repository>
75+
<id>wesley-repo</id>
76+
<name>Wesley Breukers Mvn Repository</name>
77+
<url>https://repo.wesleybreukers.nl/repository/vertcode/</url>
78+
</repository>
79+
</distributionManagement>
80+
81+
<dependencies>
82+
<dependency>
83+
<groupId>org.projectlombok</groupId>
84+
<artifactId>lombok</artifactId>
85+
<version>1.18.20</version>
86+
<scope>compile</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.jetbrains</groupId>
90+
<artifactId>annotations</artifactId>
91+
<version>20.1.0</version>
92+
<scope>compile</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>com.google.code.gson</groupId>
96+
<artifactId>gson</artifactId>
97+
<version>2.8.6</version>
98+
<scope>compile</scope>
99+
</dependency>
100+
</dependencies>
101+
102+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package eu.vertcode.vertconfig;
2+
3+
import eu.vertcode.vertconfig.object.VertConfig;
4+
import lombok.Getter;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.io.File;
8+
import java.io.InputStream;
9+
import java.nio.file.Files;
10+
import java.nio.file.StandardCopyOption;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Optional;
14+
15+
@Getter
16+
public class VertConfigs {
17+
18+
private static VertConfigs instance;
19+
private final List<VertConfig> loadedConfigs;
20+
21+
public VertConfigs() {
22+
this.loadedConfigs = new ArrayList<>();
23+
}
24+
25+
public static VertConfigs getInstance() {
26+
if (instance == null) instance = new VertConfigs();
27+
return instance;
28+
}
29+
30+
/**
31+
* Loads the config from the disk, if it doesn't exists it will throw a {@link NullPointerException}
32+
*
33+
* @param file The file you want to get the config from
34+
* @return The Config
35+
*/
36+
@Nullable
37+
public VertConfig getConfig(File file) {
38+
return this.getConfig(file, null);
39+
}
40+
41+
/**
42+
* Loads the config from the disk, if it doesn't exist it will create the default config from the {@link InputStream}.
43+
*
44+
* @param file The file you want to get the config from
45+
* @param defaultStream The InputStream of the default file
46+
* @return The Config
47+
*/
48+
public VertConfig getConfig(File file, InputStream defaultStream) {
49+
Optional<VertConfig> optional = this.loadedConfigs.stream().filter(vertConfig ->
50+
vertConfig.getPath().equalsIgnoreCase(file.getAbsolutePath())).findFirst();
51+
if (optional.isPresent()) return optional.get();
52+
53+
if (file.exists()) {
54+
VertConfig vertConfig = new VertConfig(file);
55+
this.loadedConfigs.add(vertConfig);
56+
return vertConfig;
57+
}
58+
59+
if (defaultStream == null) return null;
60+
61+
try {
62+
Files.copy(defaultStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
63+
VertConfig vertConfig = new VertConfig(file);
64+
this.loadedConfigs.add(vertConfig);
65+
return vertConfig;
66+
} catch (Exception ex) {
67+
ex.printStackTrace();
68+
return null;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)