Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
injectives committed Jul 11, 2023
1 parent 88c2c22 commit fea09d5
Show file tree
Hide file tree
Showing 65 changed files with 332 additions and 433 deletions.
50 changes: 14 additions & 36 deletions driver/src/main/java/org/neo4j/driver/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.internal.AsValue;
Expand Down Expand Up @@ -223,10 +224,7 @@ public static Value value(Object value) {
* @return the array of values
*/
public static Value[] values(final Object... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values = Arrays.stream(input).map(Values::value).toArray(Value[]::new);
return values;
}

Expand Down Expand Up @@ -257,10 +255,7 @@ public static Value value(byte... input) {
* @return the value
*/
public static Value value(String... input) {
var values = new StringValue[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = new StringValue(input[i]);
}
var values = Arrays.stream(input).map(StringValue::new).toArray(StringValue[]::new);
return new ListValue(values);
}

Expand All @@ -270,10 +265,8 @@ public static Value value(String... input) {
* @return the value
*/
public static Value value(boolean... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values =
IntStream.range(0, input.length).mapToObj(i -> value(input[i])).toArray(Value[]::new);
return new ListValue(values);
}

Expand All @@ -283,10 +276,8 @@ public static Value value(boolean... input) {
* @return the value
*/
public static Value value(char... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values =
IntStream.range(0, input.length).mapToObj(i -> value(input[i])).toArray(Value[]::new);
return new ListValue(values);
}

Expand All @@ -296,10 +287,7 @@ public static Value value(char... input) {
* @return the value
*/
public static Value value(long... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values = Arrays.stream(input).mapToObj(Values::value).toArray(Value[]::new);
return new ListValue(values);
}

Expand All @@ -309,10 +297,8 @@ public static Value value(long... input) {
* @return the value
*/
public static Value value(short... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values =
IntStream.range(0, input.length).mapToObj(i -> value(input[i])).toArray(Value[]::new);
return new ListValue(values);
}
/**
Expand All @@ -321,10 +307,7 @@ public static Value value(short... input) {
* @return the value
*/
public static Value value(int... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values = Arrays.stream(input).mapToObj(Values::value).toArray(Value[]::new);
return new ListValue(values);
}
/**
Expand All @@ -333,10 +316,7 @@ public static Value value(int... input) {
* @return the value
*/
public static Value value(double... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values = Arrays.stream(input).mapToObj(Values::value).toArray(Value[]::new);
return new ListValue(values);
}

Expand All @@ -346,10 +326,8 @@ public static Value value(double... input) {
* @return the value
*/
public static Value value(float... input) {
var values = new Value[input.length];
for (var i = 0; i < input.length; i++) {
values[i] = value(input[i]);
}
var values =
IntStream.range(0, input.length).mapToObj(i -> value(input[i])).toArray(Value[]::new);
return new ListValue(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public InternalPath(List<Segment> segments, List<Node> nodes, List<Relationship>
}

private <T> List<T> newList(int size) {
return size == 0 ? Collections.<T>emptyList() : new ArrayList<T>(size);
return size == 0 ? Collections.emptyList() : new ArrayList<>(size);
}

@Override
Expand Down
21 changes: 11 additions & 10 deletions driver/src/main/java/org/neo4j/driver/internal/Scheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.driver.internal;

import java.util.List;

public class Scheme {
public static final String BOLT_URI_SCHEME = "bolt";
public static final String BOLT_HIGH_TRUST_URI_SCHEME = "bolt+s";
Expand All @@ -36,9 +38,7 @@ public static void validateScheme(String scheme) {
BOLT_HIGH_TRUST_URI_SCHEME,
NEO4J_URI_SCHEME,
NEO4J_LOW_TRUST_URI_SCHEME,
NEO4J_HIGH_TRUST_URI_SCHEME -> {
return;
}
NEO4J_HIGH_TRUST_URI_SCHEME -> {}
default -> throw new IllegalArgumentException("Invalid address format " + scheme);
}
}
Expand All @@ -52,15 +52,16 @@ public static boolean isLowTrustScheme(String scheme) {
}

public static boolean isSecurityScheme(String scheme) {
return scheme.equals(BOLT_LOW_TRUST_URI_SCHEME)
|| scheme.equals(NEO4J_LOW_TRUST_URI_SCHEME)
|| scheme.equals(BOLT_HIGH_TRUST_URI_SCHEME)
|| scheme.equals(NEO4J_HIGH_TRUST_URI_SCHEME);
return List.of(
BOLT_LOW_TRUST_URI_SCHEME,
NEO4J_LOW_TRUST_URI_SCHEME,
BOLT_HIGH_TRUST_URI_SCHEME,
NEO4J_HIGH_TRUST_URI_SCHEME)
.contains(scheme);
}

public static boolean isRoutingScheme(String scheme) {
return scheme.equals(NEO4J_LOW_TRUST_URI_SCHEME)
|| scheme.equals(NEO4J_HIGH_TRUST_URI_SCHEME)
|| scheme.equals(NEO4J_URI_SCHEME);
return List.of(NEO4J_LOW_TRUST_URI_SCHEME, NEO4J_HIGH_TRUST_URI_SCHEME, NEO4J_URI_SCHEME)
.contains(scheme);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/
package org.neo4j.driver.internal;

import java.io.Serial;
import java.io.Serializable;
import org.neo4j.driver.Config;

public record SecuritySettings(boolean encrypted, Config.TrustStrategy trustStrategy) implements Serializable {
@Serial
private static final long serialVersionUID = 4494615367164106576L;

private static final boolean DEFAULT_ENCRYPTED = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import static java.lang.System.lineSeparator;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.Bookmark;
Expand Down Expand Up @@ -82,11 +84,9 @@ private void logLeakIfNeeded() {
}

private static String captureStackTrace() {
var result = new StringBuilder();
var elements = Thread.currentThread().getStackTrace();
for (var element : elements) {
result.append("\t").append(element).append(lineSeparator());
}
return result.toString();
return Arrays.stream(elements)
.map(element -> "\t" + element + lineSeparator())
.collect(Collectors.joining());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.neo4j.driver.internal.util.Futures.completedWithNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -54,13 +55,11 @@ private CompletableFuture<Throwable>[] retrieveAllFailures() {
private static Throwable findFirstFailure(CompletableFuture<Throwable>[] completedFailureFutures) {
// all given futures should be completed, it is thus safe to get their values

for (var failureFuture : completedFailureFutures) {
var failure = failureFuture.getNow(null); // does not block
if (failure != null) {
return failure;
}
}
return null;
return Arrays.stream(completedFailureFutures)
.map(failureFuture -> failureFuture.getNow(null))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}

private static CompletionStage<Throwable> retrieveFailure(CompletionStage<? extends FailableCursor> cursorStage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,16 @@ public String toString() {

private List<BoltServerAddress> newWithoutAddressIfPresent(
List<BoltServerAddress> addresses, BoltServerAddress addressToSkip) {
List<BoltServerAddress> newList = new ArrayList<>(addresses.size());
for (var address : addresses) {
if (!address.equals(addressToSkip)) {
newList.add(address);
}
}
return Collections.unmodifiableList(newList);
return addresses.stream()
.filter(address -> !address.equals(addressToSkip))
.toList();
}

private List<BoltServerAddress> newWithAddressReplacedIfPresent(
List<BoltServerAddress> addresses, BoltServerAddress oldAddress, BoltServerAddress newAddress) {
List<BoltServerAddress> newList = new ArrayList<>(addresses.size());
for (var address : addresses) {
newList.add(address.equals(oldAddress) ? newAddress : address);
}
return Collections.unmodifiableList(newList);
return addresses.stream()
.map(address -> address.equals(oldAddress) ? newAddress : address)
.toList();
}

private List<BoltServerAddress> newWithReusedAddresses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.time.Clock;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -33,6 +32,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
import org.neo4j.driver.internal.BoltServerAddress;
Expand Down Expand Up @@ -165,10 +165,9 @@ private CompletionStage<ConnectionContextAndHandler> ensureDatabaseNameIsComplet
public Set<BoltServerAddress> allServers() {
// obviously we just had a snapshot of all servers in all routing tables
// after we read it, the set could already be changed.
Set<BoltServerAddress> servers = new HashSet<>();
for (var tableHandler : routingTableHandlers.values()) {
servers.addAll(tableHandler.servers());
}
var servers = routingTableHandlers.values().stream()
.flatMap(tableHandler -> tableHandler.servers().stream())
.collect(Collectors.toSet());
return servers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,13 @@ private static List<BoltServerAddress> getAddressesByMode(AccessMode mode, Routi
return switch (mode) {
case READ -> routingTable.readers();
case WRITE -> routingTable.writers();
default -> throw unknownMode(mode);
};
}

private BoltServerAddress selectAddress(AccessMode mode, List<BoltServerAddress> addresses) {
return switch (mode) {
case READ -> loadBalancingStrategy.selectReader(addresses);
case WRITE -> loadBalancingStrategy.selectWriter(addresses);
default -> throw unknownMode(mode);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;

import java.io.PrintWriter;
import java.io.Serial;
import java.io.Serializable;
import java.io.StringWriter;
import java.time.LocalDateTime;
Expand All @@ -39,6 +40,7 @@
* @see Logging#console(Level)
*/
public class ConsoleLogging implements Logging, Serializable {
@Serial
private static final long serialVersionUID = 9205935204074879150L;

private final Level level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
*/
package org.neo4j.driver.internal.logging;

import java.io.Serial;
import java.io.Serializable;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;

public class DevNullLogging implements Logging, Serializable {
@Serial
private static final long serialVersionUID = -2632752338512373821L;

public static final Logging DEV_NULL_LOGGING = new DevNullLogging();
Expand All @@ -40,6 +42,7 @@ public Logger getLog(String name) {
// An enum would be preferable, but would not be API compatible.
// Reference: https://docs.oracle.com/en/java/javase/17/docs/specs/serialization/input.html#the-readresolve-method
// andJoshua Bloch, Effective Java 3rd edition
@Serial
private Object readResolve() {
return DEV_NULL_LOGGING;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.neo4j.driver.internal.logging;

import java.io.Serial;
import java.io.Serializable;
import java.util.logging.Level;
import org.neo4j.driver.Logger;
Expand All @@ -30,6 +31,7 @@
* @see Logging#javaUtilLogging(Level)
*/
public class JULogging implements Logging, Serializable {
@Serial
private static final long serialVersionUID = -1145576859241657833L;

private final Level loggingLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.neo4j.driver.internal.logging;

import java.io.Serial;
import java.io.Serializable;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
Expand All @@ -30,6 +31,7 @@
* @see Logging#slf4j()
*/
public class Slf4jLogging implements Logging, Serializable {
@Serial
private static final long serialVersionUID = 4120390028025944991L;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static java.lang.String.format;

import java.util.Objects;
import org.neo4j.driver.internal.messaging.Message;

/**
Expand Down Expand Up @@ -52,7 +53,6 @@ public boolean equals(Object o) {

var that = (FailureMessage) o;

return !(code != null ? !code.equals(that.code) : that.code != null)
&& !(message != null ? !message.equals(that.message) : that.message != null);
return Objects.equals(code, that.code) && Objects.equals(message, that.message);
}
}
Loading

0 comments on commit fea09d5

Please sign in to comment.