Skip to content

Refactor/utilities #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 7 additions & 6 deletions mongo-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

<dependencies>

<!-- Common java logging library -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>

<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand All @@ -45,12 +52,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.github.anweisen.Utility</groupId>
<artifactId>database-mongodb</artifactId>
<version>${utilities.version}</version>
</dependency>

</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.codingarea.challenges.mongoconnector;

import net.anweisen.utilities.database.internal.mongodb.MongoDBDatabase;
import net.codingarea.commons.database.mongodb.MongoDBDatabase;
import net.codingarea.challenges.plugin.Challenges;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
package net.codingarea.commons.common.config.document;

import net.codingarea.commons.common.config.Document;
import net.codingarea.commons.common.misc.FileUtils;
import org.bson.BsonArray;
import org.bson.BsonValue;
import org.bson.json.JsonWriterSettings;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.io.*;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.List;
import java.util.function.BiConsumer;

public class BsonDocument extends AbstractDocument {

protected org.bson.Document bsonDocument;

public BsonDocument(@Nonnull File file) throws IOException {
this(FileUtils.newBufferedReader(file));
}

public BsonDocument(@Nonnull Reader reader) {
BufferedReader buffered = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
StringBuilder content = new StringBuilder();
buffered.lines().forEach(content::append);
bsonDocument = org.bson.Document.parse(content.toString());
}

public BsonDocument(@Nonnull org.bson.Document bsonDocument) {
this.bsonDocument = bsonDocument;
}

public BsonDocument(@Nonnull org.bson.Document bsonDocument, @Nonnull Document root, @Nullable Document parent) {
super(root, parent);
this.bsonDocument = bsonDocument;
}

public BsonDocument() {
this(new org.bson.Document());
}

@Nonnull
@Override
public Document getDocument0(@Nonnull String path, @Nonnull Document root, @Nullable Document parent) {
org.bson.Document document = bsonDocument.get(path, org.bson.Document.class);
if (document == null) {
bsonDocument.put(path, document = new org.bson.Document());
}

return new BsonDocument(document, root, parent);
}

@Nonnull
@Override
public List<Document> getDocumentList(@Nonnull String path) {
BsonArray array = bsonDocument.get(path, BsonArray.class);
if (array == null) return new ArrayList<>();
List<Document> documents = new ArrayList<>(array.size());
for (BsonValue value : array) {
if (!value.isDocument()) continue;
String json = value.asDocument().toJson();
org.bson.Document document = org.bson.Document.parse(json);
documents.add(new BsonDocument(document, root, this));
}
return documents;
}

@Nullable
@Override
public String getString(@Nonnull String path) {
Object value = getObject(path);
return value == null ? null : value.toString();
}

@Override
public long getLong(@Nonnull String path, long def) {
try {
return Long.parseLong(getString(path));

Check warning on line 83 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null

Check warning on line 83 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null
} catch (Exception ex) {
return def;
}
}

@Override
public int getInt(@Nonnull String path, int def) {
try {
return Integer.parseInt(getString(path));

Check warning on line 92 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null

Check warning on line 92 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null
} catch (Exception ex) {
return def;
}
}

@Override
public short getShort(@Nonnull String path, short def) {
try {
return Short.parseShort(getString(path));

Check warning on line 101 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null

Check warning on line 101 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null
} catch (Exception ex) {
return def;
}
}

@Override
public byte getByte(@Nonnull String path, byte def) {
try {
return Byte.parseByte(getString(path));

Check warning on line 110 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null

Check warning on line 110 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null
} catch (Exception ex) {
return def;
}
}

@Override
public double getDouble(@Nonnull String path, double def) {
try {
return Double.parseDouble(getString(path));

Check warning on line 119 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null

Check warning on line 119 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null
} catch (Exception ex) {
return def;
}
}

@Override
public float getFloat(@Nonnull String path, float def) {
try {
return Float.parseFloat(getString(path));

Check warning on line 128 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null

Check warning on line 128 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Nullability and data flow problems

Argument `getString(path)` might be null
} catch (Exception ex) {
return def;
}
}

@Override
public boolean getBoolean(@Nonnull String path, boolean def) {
try {
Object value = bsonDocument.get(path);
if (value instanceof Boolean) return (Boolean) value;
if (value instanceof String) return Boolean.parseBoolean((String) value);
} catch (Exception ex) {

Check warning on line 140 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Catch block may ignore exception

Empty `catch` block

Check warning on line 140 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Catch block may ignore exception

Empty `catch` block
}
return def;
}

@Nullable
@Override
public Object getObject(@Nonnull String path) {
return bsonDocument.get(path);
}

@Nonnull
@Override
public List<String> getStringList(@Nonnull String path) {
return bsonDocument.getList(path, String.class);
}

@Nullable
@Override
public UUID getUUID(@Nonnull String path) {
try {
Object value = bsonDocument.get(path);
if (value instanceof UUID) return (UUID) value;
if (value instanceof String) return UUID.fromString((String) value);
} catch (Exception ex) {

Check warning on line 164 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Catch block may ignore exception

Empty `catch` block

Check warning on line 164 in mongo-connector/src/main/java/net/codingarea/commons/common/config/document/BsonDocument.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Catch block may ignore exception

Empty `catch` block
}
return null;
}

@Nullable
@Override
public Date getDate(@Nonnull String path) {
return bsonDocument.getDate(path);
}

@Nullable
@Override
public OffsetDateTime getDateTime(@Nonnull String path) {
Object value = getObject(path);

if (value == null)
return null;
if (value instanceof OffsetDateTime)
return (OffsetDateTime) value;
if (value instanceof Date)
return ((Date)value).toInstant().atOffset(ZoneOffset.UTC);
if (value instanceof String)
return OffsetDateTime.parse((CharSequence) value);

throw new IllegalStateException(value.getClass().getName() + " cannot be converted to java.time.OffsetDateTime");
}

@Nullable
@Override
public Color getColor(@Nonnull String path) {
Object value = getObject(path);

if (value == null)
return null;
if (value instanceof Color)
return (Color) value;
if (value instanceof String)
return Color.decode((String) value);

throw new IllegalStateException(value.getClass().getName() + " cannot be converted to java.awt.Color");
}

@Override
public <T> T getInstance(@Nonnull String path, @Nonnull Class<T> classOfT) {
return copyJson().getInstance(path, classOfT);
}

@Override
public <T> T toInstanceOf(@Nonnull Class<T> classOfT) {
return copyJson().toInstanceOf(classOfT);
}

@Override
public boolean contains(@Nonnull String path) {
return bsonDocument.containsKey(path);
}

@Override
public boolean isList(@Nonnull String path) {
Object value = bsonDocument.get(path);
return value instanceof Iterable || (value != null && value.getClass().isArray());
}

@Override
public boolean isDocument(@Nonnull String path) {
Object value = bsonDocument.get(path);
return value instanceof org.bson.Document;
}

@Override
public boolean isObject(@Nonnull String path) {
return !isList(path) && !isDocument(path);
}

@Override
public int size() {
return bsonDocument.size();
}

@Override
public void clear0() {
bsonDocument.clear();
}

@Override
public void set0(@Nonnull String path, @Nullable Object value) {
bsonDocument.put(path, value);
}

@Override
public void remove0(@Nonnull String path) {
bsonDocument.remove(path);
}

@Override
public void write(@Nonnull Writer writer) throws IOException {
String json = bsonDocument.toString();
writer.write(json);
}

@Nonnull
@Override
public Map<String, Object> values() {
return Collections.unmodifiableMap(bsonDocument);
}

@Nonnull
@Override
public Collection<String> keys() {
return bsonDocument.keySet();
}

@Override
public void forEach(@Nonnull BiConsumer<? super String, ? super Object> action) {
bsonDocument.forEach(action);
}

@Nonnull
@Override
public String toJson() {
return bsonDocument.toJson();
}

@Nonnull
@Override
public String toPrettyJson() {
return bsonDocument.toJson(JsonWriterSettings.builder().indent(true).build());
}

@Override
public String toString() {
return toJson();
}

@Override
public boolean isReadonly() {
return false;
}

}
Loading
Loading