Skip to content

Commit 0e7a14c

Browse files
Use lambdas, method references and Comparator APIs.
1 parent ee24a84 commit 0e7a14c

14 files changed

+218
-436
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreBuilderTest.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,23 @@ public void testMaxReaders() throws InterruptedException {
8282
builder = createBoxStoreBuilder(false);
8383
store = builder.maxReaders(1).build();
8484
final Exception[] exHolder = {null};
85-
final Thread thread = new Thread(new Runnable() {
86-
@Override
87-
public void run() {
88-
try {
89-
getTestEntityBox().count();
90-
} catch (Exception e) {
91-
exHolder[0] = e;
92-
}
93-
getTestEntityBox().closeThreadResources();
85+
final Thread thread = new Thread(() -> {
86+
try {
87+
getTestEntityBox().count();
88+
} catch (Exception e) {
89+
exHolder[0] = e;
9490
}
91+
getTestEntityBox().closeThreadResources();
9592
});
9693

9794
getTestEntityBox().count();
98-
store.runInReadTx(new Runnable() {
99-
@Override
100-
public void run() {
101-
getTestEntityBox().count();
102-
thread.start();
103-
try {
104-
thread.join(5000);
105-
} catch (InterruptedException e) {
106-
e.printStackTrace();
107-
}
95+
store.runInReadTx(() -> {
96+
getTestEntityBox().count();
97+
thread.start();
98+
try {
99+
thread.join(5000);
100+
} catch (InterruptedException e) {
101+
e.printStackTrace();
108102
}
109103
});
110104

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTest.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,9 @@ public void testCallInReadTxWithRetry_callback() {
194194
final int[] countHolderCallback = {0};
195195

196196
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir)
197-
.failedReadTxAttemptCallback(new TxCallback() {
198-
@Override
199-
public void txFinished(@Nullable Object result, @Nullable Throwable error) {
200-
assertNotNull(error);
201-
countHolderCallback[0]++;
202-
}
197+
.failedReadTxAttemptCallback((result, error) -> {
198+
assertNotNull(error);
199+
countHolderCallback[0]++;
203200
});
204201
store = builder.build();
205202
String value = store.callInReadTxWithRetry(createTestCallable(countHolder), 5, 0, true);
@@ -209,15 +206,12 @@ public void txFinished(@Nullable Object result, @Nullable Throwable error) {
209206
}
210207

211208
private Callable<String> createTestCallable(final int[] countHolder) {
212-
return new Callable<String>() {
213-
@Override
214-
public String call() throws Exception {
215-
int count = ++countHolder[0];
216-
if (count < 5) {
217-
throw new DbException("Count: " + count);
218-
}
219-
return "42";
209+
return () -> {
210+
int count = ++countHolder[0];
211+
if (count < 5) {
212+
throw new DbException("Count: " + count);
220213
}
214+
return "42";
221215
};
222216
}
223217

tests/objectbox-java-test/src/test/java/io/objectbox/BoxTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,11 @@ public void testPanicModeRemoveAllObjects() {
193193
@Test
194194
public void testRunInTx() {
195195
final long[] counts = {0, 0};
196-
store.runInTx(new Runnable() {
197-
@Override
198-
public void run() {
199-
box.put(new TestEntity());
200-
counts[0] = box.count();
201-
box.put(new TestEntity());
202-
counts[1] = box.count();
203-
}
196+
store.runInTx(() -> {
197+
box.put(new TestEntity());
198+
counts[0] = box.count();
199+
box.put(new TestEntity());
200+
counts[1] = box.count();
204201
});
205202
assertEquals(1, counts[0]);
206203
assertEquals(2, counts[1]);

tests/objectbox-java-test/src/test/java/io/objectbox/CursorTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,12 @@ public void testWriteTxBlocksOtherWriteTx() throws InterruptedException {
221221
long duration = System.currentTimeMillis() - time; // Usually 0 on desktop
222222
final CountDownLatch latchBeforeBeginTx = new CountDownLatch(1);
223223
final CountDownLatch latchAfterBeginTx = new CountDownLatch(1);
224-
new Thread() {
225-
@Override
226-
public void run() {
227-
latchBeforeBeginTx.countDown();
228-
Transaction tx2 = store.beginTx();
229-
latchAfterBeginTx.countDown();
230-
tx2.close();
231-
}
232-
}.start();
224+
new Thread(() -> {
225+
latchBeforeBeginTx.countDown();
226+
Transaction tx2 = store.beginTx();
227+
latchAfterBeginTx.countDown();
228+
tx2.close();
229+
}).start();
233230
assertTrue(latchBeforeBeginTx.await(1, TimeUnit.SECONDS));
234231
long waitTime = 100 + duration * 10;
235232
assertFalse(latchAfterBeginTx.await(waitTime, TimeUnit.MILLISECONDS));

tests/objectbox-java-test/src/test/java/io/objectbox/ObjectClassObserverTest.java

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,16 @@ protected BoxStore createBoxStore() {
4848

4949
final List<Class> classesWithChanges = new ArrayList<>();
5050

51-
DataObserver objectClassObserver = new DataObserver<Class>() {
52-
@Override
53-
public void onData(Class objectClass) {
54-
classesWithChanges.add(objectClass);
55-
observerLatch.countDown();
56-
}
51+
DataObserver objectClassObserver = (DataObserver<Class>) objectClass -> {
52+
classesWithChanges.add(objectClass);
53+
observerLatch.countDown();
5754
};
5855

59-
Runnable txRunnable = new Runnable() {
60-
@Override
61-
public void run() {
62-
putTestEntities(3);
63-
Box<TestEntityMinimal> boxMini = store.boxFor(TestEntityMinimal.class);
64-
boxMini.put(new TestEntityMinimal(), new TestEntityMinimal());
65-
assertEquals(0, classesWithChanges.size());
66-
}
56+
Runnable txRunnable = () -> {
57+
putTestEntities(3);
58+
Box<TestEntityMinimal> boxMini = store.boxFor(TestEntityMinimal.class);
59+
boxMini.put(new TestEntityMinimal(), new TestEntityMinimal());
60+
assertEquals(0, classesWithChanges.size());
6761
};
6862

6963
@Before
@@ -83,12 +77,9 @@ public void testTwoObjectClassesChanged_catchAllObserverWeak() {
8377

8478
public void testTwoObjectClassesChanged_catchAllObserver(boolean weak) {
8579
DataSubscription subscription = subscribe(weak, null);
86-
store.runInTx(new Runnable() {
87-
@Override
88-
public void run() {
89-
// Dummy TX, still will be committed
90-
getTestEntityBox().count();
91-
}
80+
store.runInTx(() -> {
81+
// Dummy TX, still will be committed
82+
getTestEntityBox().count();
9283
});
9384
assertEquals(0, classesWithChanges.size());
9485

@@ -172,31 +163,21 @@ private void testTransform(TestScheduler scheduler) throws InterruptedException
172163
final Thread testThread = Thread.currentThread();
173164

174165
SubscriptionBuilder<Long> subscriptionBuilder = store.subscribe().onlyChanges().
175-
transform(new DataTransformer<Class, Long>() {
176-
@Override
177-
@SuppressWarnings("NullableProblems")
178-
public Long transform(Class source) throws Exception {
179-
assertNotSame(testThread, Thread.currentThread());
180-
return store.boxFor(source).count();
181-
}
182-
});
166+
transform(source -> {
167+
assertNotSame(testThread, Thread.currentThread());
168+
return store.boxFor(source).count();
169+
});
183170
if (scheduler != null) {
184171
subscriptionBuilder.on(scheduler);
185172
}
186-
DataSubscription subscription = subscriptionBuilder.observer(new DataObserver<Long>() {
187-
@Override
188-
public void onData(Long data) {
189-
objectCounts.add(data);
190-
latch.countDown();
191-
}
173+
DataSubscription subscription = subscriptionBuilder.observer(data -> {
174+
objectCounts.add(data);
175+
latch.countDown();
192176
});
193177

194-
store.runInTx(new Runnable() {
195-
@Override
196-
public void run() {
197-
// Dummy TX, still will be committed, should not add anything to objectCounts
198-
getTestEntityBox().count();
199-
}
178+
store.runInTx(() -> {
179+
// Dummy TX, still will be committed, should not add anything to objectCounts
180+
getTestEntityBox().count();
200181
});
201182

202183
store.runInTx(txRunnable);
@@ -262,24 +243,14 @@ public void testTransformError(Scheduler scheduler) throws InterruptedException
262243
final CountDownLatch latch = new CountDownLatch(2);
263244
final Thread testThread = Thread.currentThread();
264245

265-
DataSubscription subscription = store.subscribe().onlyChanges().transform(new DataTransformer<Class, Long>() {
266-
@Override
267-
@SuppressWarnings("NullableProblems")
268-
public Long transform(Class source) throws Exception {
269-
throw new Exception("Boo");
270-
}
271-
}).onError(new ErrorObserver() {
272-
@Override
273-
public void onError(Throwable th) {
274-
assertNotSame(testThread, Thread.currentThread());
275-
errors.add(th);
276-
latch.countDown();
277-
}
278-
}).on(scheduler).observer(new DataObserver<Long>() {
279-
@Override
280-
public void onData(Long data) {
281-
throw new RuntimeException("Should not reach this");
282-
}
246+
DataSubscription subscription = store.subscribe().onlyChanges().transform((DataTransformer<Class, Long>) source -> {
247+
throw new Exception("Boo");
248+
}).onError(throwable -> {
249+
assertNotSame(testThread, Thread.currentThread());
250+
errors.add(throwable);
251+
latch.countDown();
252+
}).on(scheduler).observer(data -> {
253+
throw new RuntimeException("Should not reach this");
283254
});
284255

285256
store.runInTx(txRunnable);
@@ -312,18 +283,12 @@ public void testObserverError(Scheduler scheduler) throws InterruptedException {
312283
final CountDownLatch latch = new CountDownLatch(2);
313284
final Thread testThread = Thread.currentThread();
314285

315-
DataSubscription subscription = store.subscribe().onlyChanges().onError(new ErrorObserver() {
316-
@Override
317-
public void onError(Throwable th) {
318-
assertNotSame(testThread, Thread.currentThread());
319-
errors.add(th);
320-
latch.countDown();
321-
}
322-
}).on(scheduler).observer(new DataObserver<Class>() {
323-
@Override
324-
public void onData(Class data) {
325-
throw new RuntimeException("Boo");
326-
}
286+
DataSubscription subscription = store.subscribe().onlyChanges().onError(th -> {
287+
assertNotSame(testThread, Thread.currentThread());
288+
errors.add(th);
289+
latch.countDown();
290+
}).on(scheduler).observer(data -> {
291+
throw new RuntimeException("Boo");
327292
});
328293

329294
store.runInTx(txRunnable);
@@ -430,13 +395,9 @@ public void testSingle(boolean weak, boolean wrapped) {
430395
@Test
431396
public void testSingleCancelSubscription() throws InterruptedException {
432397
DataSubscription subscription = store.subscribe().single()
433-
.transform(new DataTransformer<Class, Class>() {
434-
@Override
435-
@SuppressWarnings("NullableProblems")
436-
public Class transform(Class source) throws Exception {
437-
Thread.sleep(20);
438-
return source;
439-
}
398+
.transform(source -> {
399+
Thread.sleep(20);
400+
return source;
440401
}).observer(objectClassObserver);
441402
subscription.cancel();
442403
Thread.sleep(40);

0 commit comments

Comments
 (0)