Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public static DefaultNodeConfiguration fromMap(Map<String, Object> map) {

config.host4 = m.getString("host4", config.host4);
config.host6 = m.getString("host6", config.host6);
if (config.host4 == null || config.host4.isEmpty() && config.host6 == null || config.host6.isEmpty())
if ((config.host4 == null || config.host4.isEmpty()) && (config.host6 == null || config.host6.isEmpty()))
throw new IllegalArgumentException("Missing host4 or host6");

config.port = m.getPort("port", config.port);
Expand Down Expand Up @@ -367,7 +367,7 @@ public Map<String, Object> toMap() {
map.put("host6", host6);

map.put("port", port);
map.put("privateKey", privateKey);
map.put("privateKey", Base58.encode(privateKey.bytes()));

if (dataDir != null)
map.put("dataDir", dataDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ default boolean hasEffectedRows(SqlResult<?> result) {
* @return a future completing when the connection is closed
*/
default Future<Void> close() {
return getClient().close();
SqlClient client = getClient();
if (client == null)
return Future.succeededFuture();

return client.close();
}
}
9 changes: 3 additions & 6 deletions api/src/main/java/io/bosonnetwork/utils/AddressUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ProtocolFamily;
import java.net.SocketException;
import java.net.StandardProtocolFamily;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
Expand All @@ -58,7 +56,7 @@ public class AddressUtils {
// IPv4 Bogon ranges (excludes ranges covered by InetAddress methods)
// Reference: RFC 1918, RFC 6890
private static final String[] IPV4_BOGON_RANGES = {
// "0.0.0.0/8", // Any local
// "0.0.0.0/8", // Any local
// "10.0.0.0/8", // Site local
"100.64.0.0/10", // Private network - shared address space (RFC 6598)
// "127.0.0.0/8", // Loopback
Expand Down Expand Up @@ -555,10 +553,9 @@ else if (type == Inet6Address.class)
* @throws IllegalArgumentException if the type is not supported
*/
public static InetAddress getDefaultRouteAddress(Class<? extends InetAddress> type) {
InetAddress target = null;
ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

try (DatagramSocket socket = new DatagramSocket()) {
InetAddress target;

if (type == Inet4Address.class)
target = InetAddress.getByAddress(new byte[]{8, 8, 8, 8});
else if (type == Inet6Address.class)
Expand Down
46 changes: 45 additions & 1 deletion api/src/main/java/io/bosonnetwork/utils/ConfigMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Objects;
import java.util.Set;

import io.bosonnetwork.Id;

/**
* A wrapper around a {@code Map<String, Object>} that provides type-safe configuration value retrieval.
* <p>
Expand Down Expand Up @@ -375,7 +377,6 @@ else if (val instanceof String s)
return port;
}


/**
* Retrieves a valid port number for the specified key, or returns a default value if the key is not present.
* <p>
Expand All @@ -396,6 +397,49 @@ public int getPort(String key, int def) {
return port;
}

/**
* Retrieves the Id associated with the specified key from the map.
* The key must not be null, and the corresponding value in the map must be a valid String
* representation of an Id. If the key is not present or the value is invalid, an exception
* is thrown.
*
* @param key the key whose associated Id is to be retrieved
* @return the Id associated with the specified key
* @throws NullPointerException if the key is null
* @throws IllegalArgumentException if the key is not present in the map, or if the
* value associated with the key is not a valid String representation of an Id
*/
public Id getId(String key) {
Objects.requireNonNull(key);
Object val = map.get(key);
if (val == null)
throw new IllegalArgumentException("Missing Id value - " + key);
else if (val instanceof String s)
try {
return Id.of(s);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid Id value - " + key + ": " + val);
}
else
throw new IllegalArgumentException("Invalid Id value - " + key + ": " + val);
}

/**
* Retrieves the identifier associated with the specified key. If the key is not found
* in the map, the provided default identifier is returned.
*
* @param key the key whose associated Id is to be retrieved
* @param def the default identifier to return if the key is not present in the map
* @return the identifier associated with the given key, or the default identifier if the key is not found
* @throws NullPointerException if the key is null
* @throws IllegalArgumentException if the value associated with the key is not a valid String
* representation of an Id
*/
public Id getId(String key, Id def) {
Objects.requireNonNull(key);
return map.containsKey(key) ? getId(key) : def;
}

/**
* Retrieves a nested configuration object for the specified key.
* <p>
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/bosonnetwork/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static Path normalizePath(Path path) {
}
}

return path;
return path == null ? null : path.toAbsolutePath();
}

/**
Expand Down
33 changes: 25 additions & 8 deletions api/src/main/java/io/bosonnetwork/utils/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package io.bosonnetwork.utils;

import java.util.Objects;
import java.util.function.Function;

/**
Expand All @@ -38,25 +39,25 @@ public class Pair<A, B> {
/**
* Create a value pair object from the given values.
*
* @param <C> type for value a.
* @param <D>type for value b.
* @param a value a.
* @param b value b.
* @return the new Pair object.
*/
public static <C, D> Pair<C, D> of(C a, D b) {
return new Pair<C, D>(a, b);
public Pair(A a, B b) {
this.a = a;
this.b = b;
}

/**
* Create a value pair object from the given values.
*
* @param <A1> type for value a.
* @param <B1>type for value b.
* @param a value a.
* @param b value b.
* @return the new Pair object.
*/
public Pair(A a, B b) {
this.a = a;
this.b = b;
public static <A1, B1> Pair<A1, B1> of(A1 a, B1 b) {
return new Pair<>(a, b);
}

/**
Expand All @@ -77,6 +78,22 @@ public B b() {
return b;
}

@Override
public int hashCode() {
return Objects.hash(a, b);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;

if (obj instanceof Pair<?, ?> that)
return Objects.equals(this.a, that.a) && Objects.equals(this.b, that.b);

return false;
}

@Override
public String toString() {
StringBuilder repr = new StringBuilder();
Expand Down
150 changes: 150 additions & 0 deletions api/src/main/java/io/bosonnetwork/utils/Quadruple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2023 - bosonnetwork.io
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.bosonnetwork.utils;

import java.util.Objects;
import java.util.function.Function;

/**
* This class is a simple holder for a quadruple of values.
* <p>
* It follows the same conventions as {@link Pair} and {@link Triple}, but stores four values instead of two/three.
*
* @param <A> type for value a.
* @param <B> type for value b.
* @param <C> type for value c.
* @param <D> type for value d.
*/
public class Quadruple<A, B, C, D> {
private final A a;
private final B b;
private final C c;
private final D d;

/**
* Create a value quadruple object from the given values.
*
* @param a value a.
* @param b value b.
* @param c value c.
* @param d value d.
*/
public Quadruple(A a, B b, C c, D d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}

/**
* Create a value quadruple object from the given values.
*
* @param <A1> type for value a.
* @param <B1> type for value b.
* @param <C1> type for value c.
* @param <D1> type for value d.
* @param a value a.
* @param b value b.
* @param c value c.
* @param d value d.
* @return the new Quadruple object.
*/
public static <A1, B1, C1, D1> Quadruple<A1, B1, C1, D1> of(A1 a, B1 b, C1 c, D1 d) {
return new Quadruple<>(a, b, c, d);
}

/**
* Gets the value a from the quadruple object.
*
* @return the value a.
*/
public A a() {
return a;
}

/**
* Gets the value b from the quadruple object.
*
* @return the value b.
*/
public B b() {
return b;
}

/**
* Gets the value c from the quadruple object.
*
* @return the value c.
*/
public C c() {
return c;
}

/**
* Gets the value d from the quadruple object.
*
* @return the value d.
*/
public D d() {
return d;
}

@Override
public int hashCode() {
return Objects.hash(a, b, c, d);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;

if (obj instanceof Quadruple<?, ?, ?, ?> that)
return Objects.equals(this.a, that.a)
&& Objects.equals(this.b, that.b)
&& Objects.equals(this.c, that.c)
&& Objects.equals(this.d, that.d);

return false;
}

@Override
public String toString() {
StringBuilder repr = new StringBuilder();

Function<Object, String> valueOf = (v) ->
(v instanceof String) ? "\"" + v + "\"" : String.valueOf(v);

repr.append("<")
.append(valueOf.apply(a))
.append(", ")
.append(valueOf.apply(b))
.append(", ")
.append(valueOf.apply(c))
.append(", ")
.append(valueOf.apply(d))
.append(">");

return repr.toString();
}
}
Loading
Loading