Skip to content

Commit 8254df2

Browse files
feffijknack
authored andcommitted
fixed minor code issues (jooby-project#1156)
1 parent 889a3b4 commit 8254df2

File tree

7 files changed

+56
-70
lines changed

7 files changed

+56
-70
lines changed

modules/jooby-couchbase/src/main/java/org/jooby/couchbase/AsyncDatastore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ public interface AsyncDatastore {
284284
class AsyncViewQueryResult<T> {
285285

286286
/** Total number of rows in the view. */
287-
private int totalRows;
287+
private final int totalRows;
288288

289289
/** List of rows from current execution. */
290-
private Observable<List<T>> rows;
290+
private final Observable<List<T>> rows;
291291

292292
/**
293293
* Creates a new {@link AsyncViewQueryResult}.

modules/jooby-couchbase/src/main/java/org/jooby/couchbase/Couchbase.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
import java.util.Optional;
245245
import java.util.Set;
246246
import java.util.function.Function;
247+
import java.util.stream.Stream;
247248

248249
/**
249250
* <h1>couchbase</h1>
@@ -586,9 +587,9 @@ public class Couchbase implements Module {
586587

587588
private Function<Config, CouchbaseEnvironment> env = c -> DefaultCouchbaseEnvironment.create();
588589

589-
private String db;
590+
private final String db;
590591

591-
private Set<String> buckets = new LinkedHashSet<>();
592+
private final Set<String> buckets = new LinkedHashSet<>();
592593

593594
private Optional<String> sessionBucket = Optional.empty();
594595

@@ -675,18 +676,14 @@ public void configure(final Env env, final Config conf, final Binder binder) {
675676

676677
// dump couchbase.env.* as system properties
677678
if (conf.hasPath("couchbase.env")) {
678-
conf.getConfig("couchbase.env").entrySet().forEach(e -> {
679-
System.setProperty("com.couchbase." + e.getKey(), e.getValue().unwrapped().toString());
680-
});
679+
conf.getConfig("couchbase.env").entrySet().forEach(e -> System.setProperty("com.couchbase." + e.getKey(), e.getValue().unwrapped().toString()));
681680
}
682681

683682
log.debug("Starting {}", cstr);
684683

685684
ServiceKey serviceKey = env.serviceKey();
686685
Throwing.Function3<Class, String, Object, Void> bind = (type, name, value) -> {
687-
serviceKey.generate(type, name, k -> {
688-
binder.bind(k).toInstance(value);
689-
});
686+
serviceKey.generate(type, name, k -> binder.bind(k).toInstance(value));
690687
return null;
691688
};
692689

@@ -710,15 +707,13 @@ public void configure(final Env env, final Config conf, final Binder binder) {
710707
Set<String> buckets = Sets.newHashSet(defbucket);
711708
buckets.addAll(this.buckets);
712709

713-
Function<String, String> password = name -> {
714-
return Arrays.asList(
715-
"couchbase.bucket." + name + ".password",
716-
"couchbase.bucket.password").stream()
717-
.filter(conf::hasPath)
718-
.map(conf::getString)
719-
.findFirst()
720-
.orElse(null);
721-
};
710+
Function<String, String> password = name -> Stream.of(
711+
"couchbase.bucket." + name + ".password",
712+
"couchbase.bucket.password")
713+
.filter(conf::hasPath)
714+
.map(conf::getString)
715+
.findFirst()
716+
.orElse(null);
722717
buckets.forEach(name -> {
723718
Bucket bucket = cluster.openBucket(name, password.apply(name));
724719
log.debug(" bucket opened: {}", name);
@@ -750,11 +745,9 @@ public void configure(final Env env, final Config conf, final Binder binder) {
750745
bind.apply(Bucket.class, "session", cluster.openBucket(session, password.apply(session)));
751746

752747
env.onStop(r -> {
753-
buckets.forEach(n -> {
754-
Try.apply(() -> r.require(n, Bucket.class).close())
755-
.onFailure(x -> log.debug("bucket {} close operation resulted in exception", n, x))
756-
.orElse(false);
757-
});
748+
buckets.forEach(n -> Try.apply(() -> r.require(n, Bucket.class).close())
749+
.onFailure(x -> log.debug("bucket {} close operation resulted in exception", n, x))
750+
.orElse(false));
758751
Try.run(cluster::disconnect)
759752
.onFailure(x -> log.debug("disconnect operation resulted in exception", x));
760753

@@ -787,10 +780,8 @@ private static String defbucket(final String cstr) {
787780
}
788781

789782
private static Function<Object, Object> idGen(final Bucket bucket) {
790-
return entity -> {
791-
return IdGenerator.getOrGenId(entity,
792-
() -> bucket.counter(entity.getClass().getName(), 1, 1).content());
793-
};
783+
return entity -> IdGenerator.getOrGenId(entity,
784+
() -> bucket.counter(entity.getClass().getName(), 1, 1).content());
794785
}
795786

796787
}

modules/jooby-couchbase/src/main/java/org/jooby/couchbase/CouchbaseSessionStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ public class CouchbaseSessionStore implements Session.Store {
307307

308308
private static final String SESSION = "session";
309309

310-
private Bucket bucket;
310+
private final Bucket bucket;
311311

312-
private int expiry;
312+
private final int expiry;
313313

314314
public CouchbaseSessionStore(final Bucket bucket, final int timeout) {
315315
this.bucket = requireNonNull(bucket, "Bucket is required.");

modules/jooby-couchbase/src/main/java/org/jooby/couchbase/Datastore.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,10 @@ public interface Datastore {
287287
*/
288288
class ViewQueryResult<T> {
289289
/** Total number of rows in the view. */
290-
private int totalRows;
290+
private final int totalRows;
291291

292292
/** List of rows from current execution. */
293-
private List<T> rows;
293+
private final List<T> rows;
294294

295295
/**
296296
* Creates a new {@link ViewQueryResult}.
@@ -517,9 +517,7 @@ public Long execute(final Class<?> entityClass, final Object id, final PersistTo
517517
*/
518518
@SuppressWarnings("rawtypes")
519519
static <T> Observable<T> notFound(final Class entityClass, final Object id) {
520-
return Observable.create(s -> {
521-
s.onError(new DocumentDoesNotExistException(N1Q.qualifyId(entityClass, id)));
522-
});
520+
return Observable.create(s -> s.onError(new DocumentDoesNotExistException(N1Q.qualifyId(entityClass, id))));
523521
}
524522

525523
/**
@@ -541,7 +539,7 @@ static <T> Observable<T> notFound(final Class entityClass, final Object id) {
541539
*/
542540
default <T> T get(final Class<T> entityClass, final Object id)
543541
throws DocumentDoesNotExistException {
544-
return async().<T> get(entityClass, id)
542+
return async().get(entityClass, id)
545543
.switchIfEmpty(notFound(entityClass, id))
546544
.toBlocking().single();
547545
}
@@ -561,7 +559,7 @@ default <T> T get(final Class<T> entityClass, final Object id)
561559
*/
562560
default <T> T getFromReplica(final Class<T> entityClass, final Object id,
563561
final ReplicaMode mode) throws DocumentDoesNotExistException {
564-
return async().<T> getFromReplica(entityClass, id, mode)
562+
return async().getFromReplica(entityClass, id, mode)
565563
.switchIfEmpty(notFound(entityClass, id))
566564
.toBlocking().single();
567565
}
@@ -589,7 +587,7 @@ default <T> T getFromReplica(final Class<T> entityClass, final Object id,
589587
*/
590588
default <T> T getAndLock(final Class<T> entityClass, final Object id, final int lockTime)
591589
throws DocumentDoesNotExistException {
592-
return async().<T> getAndLock(entityClass, id, lockTime)
590+
return async().getAndLock(entityClass, id, lockTime)
593591
.switchIfEmpty(notFound(entityClass, id))
594592
.toBlocking().single();
595593
}
@@ -611,7 +609,7 @@ default <T> T getAndLock(final Class<T> entityClass, final Object id, final int
611609
*/
612610
default <T> T getAndTouch(final Class<T> entityClass, final Object id, final int expiry)
613611
throws DocumentDoesNotExistException {
614-
return async().<T> getAndTouch(entityClass, id, expiry)
612+
return async().getAndTouch(entityClass, id, expiry)
615613
.switchIfEmpty(notFound(entityClass, id))
616614
.toBlocking().single();
617615
}

modules/jooby-couchbase/src/main/java/org/jooby/internal/couchbase/AsyncDatastoreImpl.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private static abstract class BaseCommandImpl implements AsyncCommand {
239239

240240
protected MutationToken mutationToken;
241241

242-
protected Function<Object, Object> idGen;
242+
protected final Function<Object, Object> idGen;
243243

244244
public BaseCommandImpl(final Function<Object, Object> idGen) {
245245
this.idGen = idGen;
@@ -304,13 +304,13 @@ public Observable<Long> execute(final Object entity, final PersistTo persistTo,
304304

305305
private static final Func1 CAS = e -> ((Document) e).cas();
306306

307-
private AsyncBucket bucket;
307+
private final AsyncBucket bucket;
308308

309-
private AsyncRepository repo;
309+
private final AsyncRepository repo;
310310

311-
private JacksonMapper converter;
311+
private final JacksonMapper converter;
312312

313-
private Function<Object, Object> idGen;
313+
private final Function<Object, Object> idGen;
314314

315315
public AsyncDatastoreImpl(final AsyncBucket bucket, final AsyncRepository repo,
316316
final Function<Object, Object> idGen, final JacksonMapper converter) {
@@ -408,29 +408,27 @@ public Observable<Long> execute(final Class<?> entityClass, final Object id,
408408
@Override
409409
public <T> Observable<List<T>> query(final N1qlQuery query) {
410410
return bucket.query(query)
411-
.flatMap(aqr -> {
412-
return Observable.zip(aqr.rows().toList(),
413-
aqr.errors().toList(),
414-
aqr.finalSuccess().singleOrDefault(Boolean.FALSE),
415-
(rows, errors, finalSuccess) -> {
416-
if (!finalSuccess) {
411+
.flatMap(aqr -> Observable.zip(aqr.rows().toList(),
412+
aqr.errors().toList(),
413+
aqr.finalSuccess().singleOrDefault(Boolean.FALSE),
414+
(rows, errors, finalSuccess) -> {
415+
if (!finalSuccess) {
416+
throw new QueryExecutionException(
417+
"execution of query resulted in exception: ",
418+
Try.apply(() -> errors.get(0)).orElse(null));
419+
}
420+
List<T> value = new ArrayList<>();
421+
for (AsyncN1qlQueryRow row : rows) {
422+
try {
423+
T v = converter.fromBytes(row.byteValue());
424+
value.add(v);
425+
} catch (IOException ex) {
417426
throw new QueryExecutionException(
418-
"execution of query resulted in exception: ",
419-
Try.apply(() -> errors.get(0)).orElse((JsonObject) null));
427+
"execution of query resulted in exception", null, ex);
420428
}
421-
List<T> value = new ArrayList<>();
422-
for (AsyncN1qlQueryRow row : rows) {
423-
try {
424-
T v = converter.fromBytes(row.byteValue());
425-
value.add(v);
426-
} catch (IOException ex) {
427-
throw new QueryExecutionException(
428-
"execution of query resulted in exception", null, ex);
429-
}
430-
}
431-
return value;
432-
});
433-
});
429+
}
430+
return value;
431+
}));
434432
}
435433

436434
@Override

modules/jooby-couchbase/src/main/java/org/jooby/internal/couchbase/DatastoreImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208

209209
public class DatastoreImpl implements Datastore {
210210

211-
private AsyncDatastore ds;
211+
private final AsyncDatastore ds;
212212

213213
public DatastoreImpl(final AsyncDatastore ds) {
214214
this.ds = ds;

modules/jooby-couchbase/src/main/java/org/jooby/internal/couchbase/IdGenerator.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@
217217
@SuppressWarnings("rawtypes")
218218
public final class IdGenerator {
219219

220-
private static LoadingCache<Class, Field> CACHE = CacheBuilder.newBuilder()
220+
private static final LoadingCache<Class, Field> CACHE = CacheBuilder.newBuilder()
221221
.build(new CacheLoader<Class, Field>() {
222222
@Override
223-
public Field load(final Class key) throws Exception {
223+
public Field load(final Class key) {
224224
if (key == Object.class) {
225225
throw new IllegalArgumentException("Entity class: " + key.getName()
226226
+ " must have an 'id' field or a field annotated with @" + Id.class.getName());
@@ -267,10 +267,9 @@ public static Object getId(final Object bean) {
267267
}
268268

269269
private static Field field(final Object bean) {
270-
Field id = Try.apply(() -> CACHE.getUnchecked(bean.getClass()))
270+
return Try.apply(() -> CACHE.getUnchecked(bean.getClass()))
271271
.unwrap(UncheckedExecutionException.class)
272272
.get();
273-
return id;
274273
}
275274

276275
private static Object getId(final Object bean, final Field id) {

0 commit comments

Comments
 (0)