Skip to content

Commit b781889

Browse files
authored
Merge pull request #15 from celesteoficial/most_abstraction
Most Abstraction
2 parents d93a42c + 8b322a0 commit b781889

3 files changed

Lines changed: 88 additions & 177 deletions

File tree

storage/src/main/java/com/celeste/databases/storage/model/database/dao/AbstractStorageDao.java

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package com.celeste.databases.storage.model.database.dao;
22

3+
import com.celeste.databases.core.model.database.dao.exception.ValueNotFoundException;
34
import com.celeste.databases.core.model.database.provider.Database;
5+
import com.celeste.databases.core.model.database.provider.exception.FailedConnectionException;
46
import com.celeste.databases.storage.model.entity.Data;
7+
import java.util.Collection;
8+
import java.util.List;
9+
import java.util.concurrent.CompletableFuture;
510
import java.util.concurrent.ExecutorService;
611
import java.util.concurrent.SynchronousQueue;
712
import java.util.concurrent.ThreadPoolExecutor;
813
import java.util.concurrent.TimeUnit;
914

1015
public abstract class AbstractStorageDao<T extends Database, U> implements StorageDao<U> {
1116

12-
protected static final ExecutorService EXECUTOR;
13-
14-
static {
15-
EXECUTOR = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5L, TimeUnit.MINUTES,
16-
new SynchronousQueue<>());
17-
}
18-
1917
protected final T storage;
2018
protected final Class<U> clazz;
2119
protected final Data<U> data;
@@ -39,4 +37,87 @@ public T getDatabase() {
3937
return storage;
4038
}
4139

40+
@Override
41+
public CompletableFuture<Void> saveAsync(final U... entities) {
42+
return CompletableFuture.runAsync(() -> {
43+
try {
44+
save(entities);
45+
} catch (FailedConnectionException exception) {
46+
exception.printStackTrace();
47+
}
48+
});
49+
}
50+
51+
@Override
52+
public CompletableFuture<Void> saveAsync(final Collection<U> entities) {
53+
return CompletableFuture.runAsync(() -> {
54+
try {
55+
save(entities);
56+
} catch (FailedConnectionException exception) {
57+
exception.printStackTrace();
58+
}
59+
});
60+
}
61+
62+
@Override
63+
public CompletableFuture<Void> deleteAsync(final U... entities) {
64+
return CompletableFuture.runAsync(() -> {
65+
try {
66+
delete(entities);
67+
} catch (FailedConnectionException exception) {
68+
exception.printStackTrace();
69+
}
70+
});
71+
}
72+
73+
@Override
74+
public CompletableFuture<Void> deleteAsync(final Collection<U> entities) {
75+
return CompletableFuture.runAsync(() -> {
76+
try {
77+
delete(entities);
78+
} catch (FailedConnectionException exception) {
79+
exception.printStackTrace();
80+
}
81+
});
82+
}
83+
84+
@Override
85+
public CompletableFuture<Boolean> containsAsync(final Object key) {
86+
return CompletableFuture.supplyAsync(() -> {
87+
try {
88+
return contains(key);
89+
} catch (FailedConnectionException exception) {
90+
exception.printStackTrace();
91+
}
92+
93+
return null;
94+
});
95+
}
96+
97+
@Override
98+
public CompletableFuture<U> findAsync(final Object key) {
99+
return CompletableFuture.supplyAsync(() -> {
100+
try {
101+
return find(key);
102+
} catch (FailedConnectionException | ValueNotFoundException exception) {
103+
exception.printStackTrace();
104+
}
105+
106+
return null;
107+
});
108+
}
109+
110+
@Override
111+
public CompletableFuture<List<U>> findAllAsync() {
112+
return CompletableFuture.supplyAsync(() -> {
113+
try {
114+
return findAll();
115+
} catch (FailedConnectionException exception) {
116+
exception.printStackTrace();
117+
}
118+
119+
return null;
120+
});
121+
}
122+
42123
}

storage/src/main/java/com/celeste/databases/storage/model/database/dao/impl/mongodb/MongoDbDao.java

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -132,91 +132,6 @@ public List<T> findAll() throws FailedConnectionException {
132132
}
133133
}
134134

135-
@Override
136-
@SafeVarargs
137-
public final CompletableFuture<Void> saveAsync(final T... entities) {
138-
return CompletableFuture.runAsync(() -> {
139-
try {
140-
save(entities);
141-
} catch (FailedConnectionException exception) {
142-
exception.printStackTrace();
143-
}
144-
});
145-
}
146-
147-
@Override
148-
public CompletableFuture<Void> saveAsync(final Collection<T> entities) {
149-
return CompletableFuture.runAsync(() -> {
150-
try {
151-
save(entities);
152-
} catch (FailedConnectionException exception) {
153-
exception.printStackTrace();
154-
}
155-
}, EXECUTOR);
156-
}
157-
158-
@Override
159-
@SafeVarargs
160-
public final CompletableFuture<Void> deleteAsync(final T... entities) {
161-
return CompletableFuture.runAsync(() -> {
162-
try {
163-
delete(entities);
164-
} catch (FailedConnectionException exception) {
165-
exception.printStackTrace();
166-
}
167-
}, EXECUTOR);
168-
}
169-
170-
@Override
171-
public CompletableFuture<Void> deleteAsync(final Collection<T> entities) {
172-
return CompletableFuture.runAsync(() -> {
173-
try {
174-
delete(entities);
175-
} catch (FailedConnectionException exception) {
176-
exception.printStackTrace();
177-
}
178-
}, EXECUTOR);
179-
}
180-
181-
@Override
182-
public CompletableFuture<Boolean> containsAsync(final Object key) {
183-
return CompletableFuture.supplyAsync(() -> {
184-
try {
185-
return contains(key);
186-
} catch (FailedConnectionException exception) {
187-
exception.printStackTrace();
188-
}
189-
190-
return false;
191-
}, EXECUTOR);
192-
}
193-
194-
@Override
195-
public CompletableFuture<T> findAsync(final Object key) {
196-
return CompletableFuture.supplyAsync(() -> {
197-
try {
198-
return find(key);
199-
} catch (FailedConnectionException | ValueNotFoundException exception) {
200-
exception.printStackTrace();
201-
}
202-
203-
return null;
204-
}, EXECUTOR);
205-
}
206-
207-
@Override
208-
public CompletableFuture<List<T>> findAllAsync() {
209-
return CompletableFuture.supplyAsync(() -> {
210-
try {
211-
return findAll();
212-
} catch (FailedConnectionException exception) {
213-
exception.printStackTrace();
214-
}
215-
216-
return new ArrayList<>();
217-
}, EXECUTOR);
218-
}
219-
220135
private MongoCollection<Document> createCollection() throws FailedConnectionException {
221136
try {
222137
final MongoDatabase database = storage.getDatabase();

storage/src/main/java/com/celeste/databases/storage/model/database/dao/impl/sql/SqlDao.java

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -109,91 +109,6 @@ public List<T> findAll() throws FailedConnectionException {
109109
return getAll(findAll);
110110
}
111111

112-
@Override
113-
@SafeVarargs
114-
public final CompletableFuture<Void> saveAsync(final T... entities) {
115-
return CompletableFuture.runAsync(() -> {
116-
try {
117-
save(entities);
118-
} catch (FailedConnectionException exception) {
119-
exception.printStackTrace();
120-
}
121-
});
122-
}
123-
124-
@Override
125-
public CompletableFuture<Void> saveAsync(final Collection<T> entities) {
126-
return CompletableFuture.runAsync(() -> {
127-
try {
128-
save(entities);
129-
} catch (FailedConnectionException exception) {
130-
exception.printStackTrace();
131-
}
132-
}, EXECUTOR);
133-
}
134-
135-
@Override
136-
@SafeVarargs
137-
public final CompletableFuture<Void> deleteAsync(final T... entities) {
138-
return CompletableFuture.runAsync(() -> {
139-
try {
140-
delete(entities);
141-
} catch (FailedConnectionException exception) {
142-
exception.printStackTrace();
143-
}
144-
}, EXECUTOR);
145-
}
146-
147-
@Override
148-
public CompletableFuture<Void> deleteAsync(final Collection<T> entities) {
149-
return CompletableFuture.runAsync(() -> {
150-
try {
151-
delete(entities);
152-
} catch (FailedConnectionException exception) {
153-
exception.printStackTrace();
154-
}
155-
}, EXECUTOR);
156-
}
157-
158-
@Override
159-
public CompletableFuture<Boolean> containsAsync(final Object key) {
160-
return CompletableFuture.supplyAsync(() -> {
161-
try {
162-
return contains(key);
163-
} catch (FailedConnectionException exception) {
164-
exception.printStackTrace();
165-
}
166-
167-
return false;
168-
}, EXECUTOR);
169-
}
170-
171-
@Override
172-
public CompletableFuture<T> findAsync(final Object key) {
173-
return CompletableFuture.supplyAsync(() -> {
174-
try {
175-
return find(key);
176-
} catch (FailedConnectionException | ValueNotFoundException exception) {
177-
exception.printStackTrace();
178-
}
179-
180-
return null;
181-
}, EXECUTOR);
182-
}
183-
184-
@Override
185-
public CompletableFuture<List<T>> findAllAsync() {
186-
return CompletableFuture.supplyAsync(() -> {
187-
try {
188-
return findAll();
189-
} catch (FailedConnectionException exception) {
190-
exception.printStackTrace();
191-
}
192-
193-
return new ArrayList<>();
194-
}, EXECUTOR);
195-
}
196-
197112
private void createTable() throws FailedConnectionException {
198113
executeUpdate(createTable);
199114
}

0 commit comments

Comments
 (0)