Skip to content

Commit

Permalink
Multiple improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinFactory committed Jul 21, 2020
1 parent c5715a8 commit d807dd8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.javafactorydev</groupId>
<artifactId>lightningstorage</artifactId>
<version>3.1.5</version>
<version>3.1.6</version>
<name>LightningStorage</name>
<description>Store data in a better way</description>
<url>https://github.com/JavaFactoryDev/LightningStorage</url>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/leonhard/storage/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import java.io.File;
import java.io.InputStream;
import java.util.List;
import lombok.NonNull;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings({"unused"})
public class Config extends Yaml {

private List<String> header;

public Config(final Config config) {
public Config(@NonNull final Config config) {
super(config);
}

Expand Down
21 changes: 8 additions & 13 deletions src/main/java/de/leonhard/storage/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ public Json(
@Nullable final ReloadSettings reloadSettings) {
super(name, path, FileType.JSON);

if (create() || file.length() == 0) {
if (inputStream != null) {
if (create() || file.length() == 0)
if (inputStream != null)
FileUtils.writeToFile(file, inputStream);
}
}

if (reloadSettings != null) {
if (reloadSettings != null)
this.reloadSettings = reloadSettings;
}
forceReload();
}

Expand All @@ -73,15 +70,14 @@ public Json(final File file) {
@Override
public Map getMap(final String key) {
final String finalKey = (pathPrefix == null) ? key : pathPrefix + "." + key;
if (!contains(finalKey)) {
if (!contains(finalKey))
return new HashMap<>();
} else {
else {
final Object map = get(key);
if (map instanceof Map) {
if (map instanceof Map)
return (Map<?, ?>) fileData.get(key);
} else if (map instanceof JSONObject) {
else if (map instanceof JSONObject)
return ((JSONObject) map).toMap();
}
// Exception in casting
throw new IllegalArgumentException(
"ClassCastEx: Json contains key: '" + key + "' but it is not a Map");
Expand All @@ -94,9 +90,8 @@ public Map getMap(final String key) {

@Override
protected Map<String, Object> readToMap() throws IOException {
if (file.length() == 0) {
if (file.length() == 0)
Files.write(file.toPath(), Collections.singletonList("{}"));
}

final JSONTokener jsonTokener = new JSONTokener(FileUtils.createInputStream(file));
return new JSONObject(jsonTokener).toMap();
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/de/leonhard/storage/Toml.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import de.leonhard.storage.internal.editor.toml.TomlManager;
import de.leonhard.storage.internal.settings.ReloadSettings;
import de.leonhard.storage.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import lombok.NonNull;

public class Toml extends FlatFile {

public Toml(final Toml toml) {
public Toml(@NonNull final Toml toml) {
super(toml.getFile());
fileData = toml.getFileData();
}
Expand All @@ -34,13 +34,11 @@ public Toml(
final ReloadSettings reloadSettings) {
super(name, path, FileType.TOML);

if (create() && inputStream != null) {
if (create() && inputStream != null)
FileUtils.writeToFile(file, inputStream);
}

if (reloadSettings != null) {
if (reloadSettings != null)
this.reloadSettings = reloadSettings;
}

forceReload();
}
Expand Down
32 changes: 11 additions & 21 deletions src/main/java/de/leonhard/storage/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
import de.leonhard.storage.internal.settings.DataType;
import de.leonhard.storage.internal.settings.ReloadSettings;
import de.leonhard.storage.util.FileUtils;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.Cleanup;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.jetbrains.annotations.Nullable;

Expand All @@ -34,7 +31,7 @@ public class Yaml extends FlatFile {
@Setter
private ConfigSettings configSettings = ConfigSettings.SKIP_COMMENTS;

public Yaml(final Yaml yaml) {
public Yaml(@NonNull final Yaml yaml) {
super(yaml.getFile());
fileData = yaml.getFileData();
yamlEditor = yaml.getYamlEditor();
Expand Down Expand Up @@ -64,26 +61,22 @@ public Yaml(
super(name, path, FileType.YAML);
this.inputStream = inputStream;

if (create() && inputStream != null) {
if (create() && inputStream != null)
FileUtils.writeToFile(file, inputStream);
}

yamlEditor = new YamlEditor(file);
parser = new YamlParser(yamlEditor);

if (reloadSettings != null) {
if (reloadSettings != null)
this.reloadSettings = reloadSettings;
}

if (configSettings != null) {
if (configSettings != null)
this.configSettings = configSettings;
}

if (dataType != null) {
if (dataType != null)
this.dataType = dataType;
} else {
else
this.dataType = DataType.fromConfigSettings(configSettings);
}

forceReload();
}
Expand All @@ -103,21 +96,18 @@ public Yaml addDefaultsFromInputStream() {
public Yaml addDefaultsFromInputStream(@Nullable final InputStream inputStream) {
reloadIfNeeded();
// Creating & setting defaults
if (inputStream == null) {
if (inputStream == null)
return this;
}

try {
final Map<String, Object> data = new SimpleYamlReader(
new InputStreamReader(inputStream)).readToMap();

final FileData newData = new FileData(data, DataType.UNSORTED);

for (final String key : newData.keySet()) {
if (!fileData.containsKey(key)) {
for (final String key : newData.keySet())
if (!fileData.containsKey(key))
fileData.insert(key, newData.get(key));
}
}

write();
} catch (final Exception ex) {
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/de/leonhard/storage/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package de.leonhard.storage.util;

import de.leonhard.storage.internal.provider.LightningProviders;
import lombok.Cleanup;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -18,6 +12,11 @@
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import lombok.Cleanup;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Nullable;

/**
* Class for easier, more convenient & strait interaction with files
Expand Down Expand Up @@ -60,7 +59,8 @@ public List<File> listFiles(
return result;
}

public File getAndMake(@NonNull final String name,
public File getAndMake(
@NonNull final String name,
@NonNull final String path) {
return getAndMake(new File(path, name));
}
Expand Down Expand Up @@ -107,8 +107,8 @@ public String getParentDirPath(@NonNull final File file) {
}

/**
* Since file.getParentFile can be null we created an extension function to get the path
* of the parent file
* Since file.getParentFile can be null we created an extension function to get the path of the
* parent file
*
* @param fileOrDirPath Path to file
* @return Path to file as String
Expand Down Expand Up @@ -219,10 +219,10 @@ private byte[] readAllBytes(@NonNull final File file) {
}
}

public List<String> readAllLines(final File file) {
public List<String> readAllLines(@NonNull final File file) {
final byte[] fileBytes = readAllBytes(file);
final String asString = new String(fileBytes);
return new ArrayList<>(Arrays.asList(asString.split("\n")));
return new ArrayList<>(Arrays.asList(asString.split(System.lineSeparator())));
}

// ----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -312,9 +312,10 @@ public void extractResource(
if (target.exists() && !replace)
return;

try (final InputStream inputStream = LightningProviders
.inputStreamProvider()
.createInputStreamFromInnerResource(resourcePath)) {
try (
final InputStream inputStream = LightningProviders
.inputStreamProvider()
.createInputStreamFromInnerResource(resourcePath)) {

Valid.notNull(
inputStream,
Expand Down

0 comments on commit d807dd8

Please sign in to comment.