Skip to content

Commit c6e92a6

Browse files
Merge branch '48-java-8-features' into 'dev'
Use lambdas, method ref, diamonds and try-with-resources See merge request objectbox/objectbox-java!59
2 parents 51c803b + 050be36 commit c6e92a6

File tree

14 files changed

+207
-401
lines changed

14 files changed

+207
-401
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static boolean isObjectBrowserAvailable() {
170170
private final LongHashMap<Class> classByEntityTypeId = new LongHashMap<>();
171171
private final int[] allEntityTypeIds;
172172
private final Map<Class, Box> boxes = new ConcurrentHashMap<>();
173-
private final Set<Transaction> transactions = Collections.newSetFromMap(new WeakHashMap<Transaction, Boolean>());
173+
private final Set<Transaction> transactions = Collections.newSetFromMap(new WeakHashMap<>());
174174
private final ExecutorService threadPool = new ObjectBoxThreadPool(this);
175175
private final ObjectClassPublisher objectClassPublisher;
176176
final boolean debugTxRead;
@@ -243,7 +243,7 @@ public static boolean isObjectBrowserAvailable() {
243243
objectClassPublisher = new ObjectClassPublisher(this);
244244

245245
failedReadTxAttemptCallback = builder.failedReadTxAttemptCallback;
246-
queryAttempts = builder.queryAttempts < 1 ? 1 : builder.queryAttempts;
246+
queryAttempts = Math.max(builder.queryAttempts, 1);
247247
}
248248

249249
static String getCanonicalPath(File directory) {
@@ -278,13 +278,10 @@ static boolean isFileOpen(final String canonicalPath) {
278278
}
279279
if (openFilesCheckerThread == null || !openFilesCheckerThread.isAlive()) {
280280
// Use a thread to avoid finalizers that block us
281-
openFilesCheckerThread = new Thread() {
282-
@Override
283-
public void run() {
284-
isFileOpenSync(canonicalPath, true);
285-
openFilesCheckerThread = null; // Clean ref to itself
286-
}
287-
};
281+
openFilesCheckerThread = new Thread(() -> {
282+
isFileOpenSync(canonicalPath, true);
283+
openFilesCheckerThread = null; // Clean ref to itself
284+
});
288285
openFilesCheckerThread.setDaemon(true);
289286
openFilesCheckerThread.start();
290287
try {
@@ -834,18 +831,15 @@ public <R> R callInTxNoException(Callable<R> callable) {
834831
* See also {@link #runInTx(Runnable)}.
835832
*/
836833
public void runInTxAsync(final Runnable runnable, @Nullable final TxCallback<Void> callback) {
837-
threadPool.submit(new Runnable() {
838-
@Override
839-
public void run() {
840-
try {
841-
runInTx(runnable);
842-
if (callback != null) {
843-
callback.txFinished(null, null);
844-
}
845-
} catch (Throwable failure) {
846-
if (callback != null) {
847-
callback.txFinished(null, failure);
848-
}
834+
threadPool.submit(() -> {
835+
try {
836+
runInTx(runnable);
837+
if (callback != null) {
838+
callback.txFinished(null, null);
839+
}
840+
} catch (Throwable failure) {
841+
if (callback != null) {
842+
callback.txFinished(null, failure);
849843
}
850844
}
851845
});
@@ -858,18 +852,15 @@ public void run() {
858852
* * See also {@link #callInTx(Callable)}.
859853
*/
860854
public <R> void callInTxAsync(final Callable<R> callable, @Nullable final TxCallback<R> callback) {
861-
threadPool.submit(new Runnable() {
862-
@Override
863-
public void run() {
864-
try {
865-
R result = callInTx(callable);
866-
if (callback != null) {
867-
callback.txFinished(result, null);
868-
}
869-
} catch (Throwable failure) {
870-
if (callback != null) {
871-
callback.txFinished(null, failure);
872-
}
855+
threadPool.submit(() -> {
856+
try {
857+
R result = callInTx(callable);
858+
if (callback != null) {
859+
callback.txFinished(result, null);
860+
}
861+
} catch (Throwable failure) {
862+
if (callback != null) {
863+
callback.txFinished(null, failure);
873864
}
874865
}
875866
});

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import java.io.BufferedOutputStream;
2323
import java.io.File;
2424
import java.io.FileInputStream;
25-
import java.io.FileNotFoundException;
2625
import java.io.FileOutputStream;
2726
import java.io.InputStream;
2827
import java.io.OutputStream;
29-
import java.lang.reflect.InvocationTargetException;
3028
import java.lang.reflect.Method;
3129
import java.util.ArrayList;
3230
import java.util.List;
@@ -353,12 +351,7 @@ public BoxStoreBuilder failedReadTxAttemptCallback(TxCallback failedReadTxAttemp
353351
*/
354352
@Experimental
355353
public BoxStoreBuilder initialDbFile(final File initialDbFile) {
356-
return initialDbFile(new Factory<InputStream>() {
357-
@Override
358-
public InputStream provide() throws FileNotFoundException {
359-
return new FileInputStream(initialDbFile);
360-
}
361-
});
354+
return initialDbFile(() -> new FileInputStream(initialDbFile));
362355
}
363356

364357
/**

objectbox-java/src/main/java/io/objectbox/Cursor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,8 @@ protected <TARGET> void checkApplyToManyToDb(List<TARGET> orders, Class<TARGET>
324324
if (orders instanceof ToMany) {
325325
ToMany<TARGET> toMany = (ToMany<TARGET>) orders;
326326
if (toMany.internalCheckApplyToDbRequired()) {
327-
Cursor<TARGET> targetCursor = getRelationTargetCursor(targetClass);
328-
try {
327+
try (Cursor<TARGET> targetCursor = getRelationTargetCursor(targetClass)) {
329328
toMany.internalApplyToDb(this, targetCursor);
330-
} finally {
331-
targetCursor.close();
332329
}
333330
}
334331
}

objectbox-java/src/main/java/io/objectbox/ObjectClassPublisher.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,14 @@ private void unsubscribe(DataObserver<Class> observer, int entityTypeId) {
7777

7878
@Override
7979
public void publishSingle(final DataObserver<Class> observer, @Nullable final Object forClass) {
80-
boxStore.internalScheduleThread(new Runnable() {
81-
@Override
82-
public void run() {
83-
Collection<Class> entityClasses = forClass != null ? Collections.singletonList((Class) forClass) :
84-
boxStore.getAllEntityClasses();
85-
for (Class entityClass : entityClasses) {
86-
try {
87-
observer.onData(entityClass);
88-
} catch (RuntimeException e) {
89-
handleObserverException(entityClass);
90-
}
80+
boxStore.internalScheduleThread(() -> {
81+
Collection<Class> entityClasses = forClass != null ? Collections.singletonList((Class) forClass) :
82+
boxStore.getAllEntityClasses();
83+
for (Class entityClass : entityClasses) {
84+
try {
85+
observer.onData(entityClass);
86+
} catch (RuntimeException e) {
87+
handleObserverException(entityClass);
9188
}
9289
}
9390
});

objectbox-java/src/main/java/io/objectbox/internal/ObjectBoxThreadPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ObjectBoxThreadPool extends ThreadPoolExecutor {
4040
private final BoxStore boxStore;
4141

4242
public ObjectBoxThreadPool(BoxStore boxStore) {
43-
super(0, Integer.MAX_VALUE, 20L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
43+
super(0, Integer.MAX_VALUE, 20L, TimeUnit.SECONDS, new SynchronousQueue<>(),
4444
new ObjectBoxThreadFactory());
4545
this.boxStore = boxStore;
4646
}

objectbox-java/src/main/java/io/objectbox/query/LazyList.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,10 @@ public void loadRemaining() {
136136
if (loadedCount != size) {
137137
checkCached();
138138
// use single reader only for efficiency
139-
box.getStore().runInReadTx(new Runnable() {
140-
@Override
141-
public void run() {
142-
for (int i = 0; i < size; i++) {
143-
get(i);
144-
}
139+
box.getStore().runInReadTx(() -> {
140+
for (int i = 0; i < size; i++) {
141+
//noinspection ResultOfMethodCallIgnored
142+
get(i);
145143
}
146144
});
147145
}

0 commit comments

Comments
 (0)