Skip to content

Commit b6cc144

Browse files
authored
Merge pull request #104 from coding-area-net/refactor/utilities
Refactor/utilities
2 parents ee5e253 + b9d4bfb commit b6cc144

File tree

495 files changed

+20080
-1010
lines changed

Some content is hidden

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

495 files changed

+20080
-1010
lines changed

mongo-connector/pom.xml

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
<dependencies>
2222

23+
<!-- Common java logging library -->
24+
<dependency>
25+
<groupId>org.slf4j</groupId>
26+
<artifactId>slf4j-api</artifactId>
27+
<version>1.7.32</version>
28+
</dependency>
29+
2330
<dependency>
2431
<groupId>org.spigotmc</groupId>
2532
<artifactId>spigot-api</artifactId>
@@ -45,12 +52,6 @@
4552
<version>${project.version}</version>
4653
</dependency>
4754

48-
<dependency>
49-
<groupId>com.github.anweisen.Utility</groupId>
50-
<artifactId>database-mongodb</artifactId>
51-
<version>${utilities.version}</version>
52-
</dependency>
53-
5455
</dependencies>
5556

5657
<repositories>

mongo-connector/src/main/java/net/codingarea/challenges/mongoconnector/MongoConnector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.codingarea.challenges.mongoconnector;
22

3-
import net.anweisen.utilities.database.internal.mongodb.MongoDBDatabase;
3+
import net.codingarea.commons.database.mongodb.MongoDBDatabase;
44
import net.codingarea.challenges.plugin.Challenges;
55
import org.bukkit.plugin.java.JavaPlugin;
66

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
package net.codingarea.commons.common.config.document;
2+
3+
import net.codingarea.commons.common.config.Document;
4+
import net.codingarea.commons.common.misc.FileUtils;
5+
import org.bson.BsonArray;
6+
import org.bson.BsonValue;
7+
import org.bson.json.JsonWriterSettings;
8+
9+
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
11+
import java.awt.*;
12+
import java.io.*;
13+
import java.time.OffsetDateTime;
14+
import java.time.ZoneOffset;
15+
import java.util.*;
16+
import java.util.List;
17+
import java.util.function.BiConsumer;
18+
19+
public class BsonDocument extends AbstractDocument {
20+
21+
protected org.bson.Document bsonDocument;
22+
23+
public BsonDocument(@Nonnull File file) throws IOException {
24+
this(FileUtils.newBufferedReader(file));
25+
}
26+
27+
public BsonDocument(@Nonnull Reader reader) {
28+
BufferedReader buffered = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
29+
StringBuilder content = new StringBuilder();
30+
buffered.lines().forEach(content::append);
31+
bsonDocument = org.bson.Document.parse(content.toString());
32+
}
33+
34+
public BsonDocument(@Nonnull org.bson.Document bsonDocument) {
35+
this.bsonDocument = bsonDocument;
36+
}
37+
38+
public BsonDocument(@Nonnull org.bson.Document bsonDocument, @Nonnull Document root, @Nullable Document parent) {
39+
super(root, parent);
40+
this.bsonDocument = bsonDocument;
41+
}
42+
43+
public BsonDocument() {
44+
this(new org.bson.Document());
45+
}
46+
47+
@Nonnull
48+
@Override
49+
public Document getDocument0(@Nonnull String path, @Nonnull Document root, @Nullable Document parent) {
50+
org.bson.Document document = bsonDocument.get(path, org.bson.Document.class);
51+
if (document == null) {
52+
bsonDocument.put(path, document = new org.bson.Document());
53+
}
54+
55+
return new BsonDocument(document, root, parent);
56+
}
57+
58+
@Nonnull
59+
@Override
60+
public List<Document> getDocumentList(@Nonnull String path) {
61+
BsonArray array = bsonDocument.get(path, BsonArray.class);
62+
if (array == null) return new ArrayList<>();
63+
List<Document> documents = new ArrayList<>(array.size());
64+
for (BsonValue value : array) {
65+
if (!value.isDocument()) continue;
66+
String json = value.asDocument().toJson();
67+
org.bson.Document document = org.bson.Document.parse(json);
68+
documents.add(new BsonDocument(document, root, this));
69+
}
70+
return documents;
71+
}
72+
73+
@Nullable
74+
@Override
75+
public String getString(@Nonnull String path) {
76+
Object value = getObject(path);
77+
return value == null ? null : value.toString();
78+
}
79+
80+
@Override
81+
public long getLong(@Nonnull String path, long def) {
82+
try {
83+
return Long.parseLong(getString(path));
84+
} catch (Exception ex) {
85+
return def;
86+
}
87+
}
88+
89+
@Override
90+
public int getInt(@Nonnull String path, int def) {
91+
try {
92+
return Integer.parseInt(getString(path));
93+
} catch (Exception ex) {
94+
return def;
95+
}
96+
}
97+
98+
@Override
99+
public short getShort(@Nonnull String path, short def) {
100+
try {
101+
return Short.parseShort(getString(path));
102+
} catch (Exception ex) {
103+
return def;
104+
}
105+
}
106+
107+
@Override
108+
public byte getByte(@Nonnull String path, byte def) {
109+
try {
110+
return Byte.parseByte(getString(path));
111+
} catch (Exception ex) {
112+
return def;
113+
}
114+
}
115+
116+
@Override
117+
public double getDouble(@Nonnull String path, double def) {
118+
try {
119+
return Double.parseDouble(getString(path));
120+
} catch (Exception ex) {
121+
return def;
122+
}
123+
}
124+
125+
@Override
126+
public float getFloat(@Nonnull String path, float def) {
127+
try {
128+
return Float.parseFloat(getString(path));
129+
} catch (Exception ex) {
130+
return def;
131+
}
132+
}
133+
134+
@Override
135+
public boolean getBoolean(@Nonnull String path, boolean def) {
136+
try {
137+
Object value = bsonDocument.get(path);
138+
if (value instanceof Boolean) return (Boolean) value;
139+
if (value instanceof String) return Boolean.parseBoolean((String) value);
140+
} catch (Exception ex) {
141+
}
142+
return def;
143+
}
144+
145+
@Nullable
146+
@Override
147+
public Object getObject(@Nonnull String path) {
148+
return bsonDocument.get(path);
149+
}
150+
151+
@Nonnull
152+
@Override
153+
public List<String> getStringList(@Nonnull String path) {
154+
return bsonDocument.getList(path, String.class);
155+
}
156+
157+
@Nullable
158+
@Override
159+
public UUID getUUID(@Nonnull String path) {
160+
try {
161+
Object value = bsonDocument.get(path);
162+
if (value instanceof UUID) return (UUID) value;
163+
if (value instanceof String) return UUID.fromString((String) value);
164+
} catch (Exception ex) {
165+
}
166+
return null;
167+
}
168+
169+
@Nullable
170+
@Override
171+
public Date getDate(@Nonnull String path) {
172+
return bsonDocument.getDate(path);
173+
}
174+
175+
@Nullable
176+
@Override
177+
public OffsetDateTime getDateTime(@Nonnull String path) {
178+
Object value = getObject(path);
179+
180+
if (value == null)
181+
return null;
182+
if (value instanceof OffsetDateTime)
183+
return (OffsetDateTime) value;
184+
if (value instanceof Date)
185+
return ((Date)value).toInstant().atOffset(ZoneOffset.UTC);
186+
if (value instanceof String)
187+
return OffsetDateTime.parse((CharSequence) value);
188+
189+
throw new IllegalStateException(value.getClass().getName() + " cannot be converted to java.time.OffsetDateTime");
190+
}
191+
192+
@Nullable
193+
@Override
194+
public Color getColor(@Nonnull String path) {
195+
Object value = getObject(path);
196+
197+
if (value == null)
198+
return null;
199+
if (value instanceof Color)
200+
return (Color) value;
201+
if (value instanceof String)
202+
return Color.decode((String) value);
203+
204+
throw new IllegalStateException(value.getClass().getName() + " cannot be converted to java.awt.Color");
205+
}
206+
207+
@Override
208+
public <T> T getInstance(@Nonnull String path, @Nonnull Class<T> classOfT) {
209+
return copyJson().getInstance(path, classOfT);
210+
}
211+
212+
@Override
213+
public <T> T toInstanceOf(@Nonnull Class<T> classOfT) {
214+
return copyJson().toInstanceOf(classOfT);
215+
}
216+
217+
@Override
218+
public boolean contains(@Nonnull String path) {
219+
return bsonDocument.containsKey(path);
220+
}
221+
222+
@Override
223+
public boolean isList(@Nonnull String path) {
224+
Object value = bsonDocument.get(path);
225+
return value instanceof Iterable || (value != null && value.getClass().isArray());
226+
}
227+
228+
@Override
229+
public boolean isDocument(@Nonnull String path) {
230+
Object value = bsonDocument.get(path);
231+
return value instanceof org.bson.Document;
232+
}
233+
234+
@Override
235+
public boolean isObject(@Nonnull String path) {
236+
return !isList(path) && !isDocument(path);
237+
}
238+
239+
@Override
240+
public int size() {
241+
return bsonDocument.size();
242+
}
243+
244+
@Override
245+
public void clear0() {
246+
bsonDocument.clear();
247+
}
248+
249+
@Override
250+
public void set0(@Nonnull String path, @Nullable Object value) {
251+
bsonDocument.put(path, value);
252+
}
253+
254+
@Override
255+
public void remove0(@Nonnull String path) {
256+
bsonDocument.remove(path);
257+
}
258+
259+
@Override
260+
public void write(@Nonnull Writer writer) throws IOException {
261+
String json = bsonDocument.toString();
262+
writer.write(json);
263+
}
264+
265+
@Nonnull
266+
@Override
267+
public Map<String, Object> values() {
268+
return Collections.unmodifiableMap(bsonDocument);
269+
}
270+
271+
@Nonnull
272+
@Override
273+
public Collection<String> keys() {
274+
return bsonDocument.keySet();
275+
}
276+
277+
@Override
278+
public void forEach(@Nonnull BiConsumer<? super String, ? super Object> action) {
279+
bsonDocument.forEach(action);
280+
}
281+
282+
@Nonnull
283+
@Override
284+
public String toJson() {
285+
return bsonDocument.toJson();
286+
}
287+
288+
@Nonnull
289+
@Override
290+
public String toPrettyJson() {
291+
return bsonDocument.toJson(JsonWriterSettings.builder().indent(true).build());
292+
}
293+
294+
@Override
295+
public String toString() {
296+
return toJson();
297+
}
298+
299+
@Override
300+
public boolean isReadonly() {
301+
return false;
302+
}
303+
304+
}

0 commit comments

Comments
 (0)