From 87a86d1293566309553296a781e9da3b7c6f8b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Mon, 25 Sep 2023 19:30:24 +0200 Subject: [PATCH] common: Make code Java 7 friendly We can eventually use retrolambda to generate Java 7-compatible class bytecode. Change the few places that currently use Java 8 API. --- .../java/org/newsclub/net/unix/AFFunction.java | 13 +++++++++++++ .../org/newsclub/net/unix/AFSocketAddress.java | 3 +-- .../java/org/newsclub/net/unix/AFSupplier.java | 12 ++++++++++++ .../newsclub/net/unix/AFUNIXSocketAddress.java | 5 ++--- .../org/newsclub/net/unix/FileDescriptorCast.java | 3 +-- .../org/newsclub/net/unix/NativeUnixSocket.java | 15 ++++----------- .../org/newsclub/net/unix/SocketAddressUtil.java | 3 +-- .../org/newsclub/net/unix/SocketAddressUtil.java | 3 +-- 8 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 junixsocket-common/src/main/java/org/newsclub/net/unix/AFFunction.java create mode 100644 junixsocket-common/src/main/java/org/newsclub/net/unix/AFSupplier.java diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFFunction.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFFunction.java new file mode 100644 index 000000000..abaa87435 --- /dev/null +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFFunction.java @@ -0,0 +1,13 @@ +package org.newsclub.net.unix; + +@FunctionalInterface +public interface AFFunction { + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + */ + R apply(T t); +} \ No newline at end of file diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketAddress.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketAddress.java index 071b52111..278f00037 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketAddress.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSocketAddress.java @@ -37,7 +37,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import java.util.function.Supplier; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -647,7 +646,7 @@ static final int unwrapAddressDirectBufferInternal(ByteBuffer socketAddressBuffe Objects.requireNonNull(address); if (!(address instanceof AFSocketAddress)) { - Supplier supp = AFUNIXSocketAddress.supportedAddressSupplier( + AFSupplier supp = AFUNIXSocketAddress.supportedAddressSupplier( address); address = supp == null ? null : supp.get(); if (address == null) { diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSupplier.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSupplier.java new file mode 100644 index 000000000..a0d50d2a5 --- /dev/null +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSupplier.java @@ -0,0 +1,12 @@ +package org.newsclub.net.unix; + +@FunctionalInterface +interface AFSupplier { + + /** + * Gets a result. + * + * @return a result + */ + T get(); +} diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFUNIXSocketAddress.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFUNIXSocketAddress.java index 72b35db5c..098d9ab6f 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFUNIXSocketAddress.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFUNIXSocketAddress.java @@ -35,7 +35,6 @@ import java.util.Locale; import java.util.Objects; import java.util.Set; -import java.util.function.Supplier; import org.eclipse.jdt.annotation.NonNull; @@ -331,7 +330,7 @@ public static AFUNIXSocketAddress unwrap(InetAddress address, int port) throws S public static AFUNIXSocketAddress unwrap(SocketAddress address) throws SocketException { // FIXME: add support for UnixDomainSocketAddress Objects.requireNonNull(address); - Supplier supplier = supportedAddressSupplier(address); + AFSupplier supplier = supportedAddressSupplier(address); if (supplier == null) { throw new SocketException("Unsupported address"); } @@ -527,7 +526,7 @@ public static boolean isSupportedAddress(SocketAddress addr) { * @param addr The address. * @return The supplier, or {@code null}. */ - static Supplier supportedAddressSupplier(SocketAddress addr) { + static AFSupplier supportedAddressSupplier(SocketAddress addr) { if (addr == null) { return null; } else if (addr instanceof AFUNIXSocketAddress) { diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/FileDescriptorCast.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/FileDescriptorCast.java index 6bcb283d1..2d074c5f6 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/FileDescriptorCast.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/FileDescriptorCast.java @@ -32,7 +32,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.function.Function; import org.eclipse.jdt.annotation.NonNull; @@ -73,7 +72,7 @@ public final class FileDescriptorCast implements FileDescriptorAccess { private static final Map, CastingProviderMap> PRIMARY_TYPE_PROVIDERS_MAP = Collections .synchronizedMap(new HashMap<>()); - private static final Function FD_IS_PROVIDER = System + private static final AFFunction FD_IS_PROVIDER = System .getProperty("osv.version") != null ? LenientFileInputStream::new : FileInputStream::new; private static final CastingProviderMap GLOBAL_PROVIDERS_FINAL = new CastingProviderMap() { diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/NativeUnixSocket.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/NativeUnixSocket.java index be828626f..0e67dc9d4 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/NativeUnixSocket.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/NativeUnixSocket.java @@ -29,8 +29,7 @@ import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.spi.AbstractSelectableChannel; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; import org.newsclub.net.unix.AFSelector.PollFd; @@ -43,7 +42,7 @@ * @author Christian Kohlschütter */ final class NativeUnixSocket { - private static final CompletableFuture LOADED = new CompletableFuture<>(); + private static final AtomicBoolean LOADED = new AtomicBoolean(false); static final int DOMAIN_UNIX = 1; static final int DOMAIN_TIPC = 30; @@ -105,13 +104,7 @@ private NativeUnixSocket() { } static boolean isLoaded() { - boolean loadSuccessful; - try { - loadSuccessful = LOADED.get(); - } catch (InterruptedException | ExecutionException e) { - loadSuccessful = false; - } - return loadSuccessful; + return LOADED.get(); } static void ensureSupported() throws UnsupportedOperationException { @@ -319,6 +312,6 @@ static native boolean initPipe(FileDescriptor source, FileDescriptor sink, boole static native int systemResolveCtlId(FileDescriptor fd, String ctlName) throws IOException; static void setLoaded(boolean successful) { - LOADED.complete(successful); + LOADED.compareAndSet(false, successful); } } diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/SocketAddressUtil.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/SocketAddressUtil.java index 58b208213..2cfcb493c 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/SocketAddressUtil.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/SocketAddressUtil.java @@ -20,7 +20,6 @@ import java.net.SocketAddress; import java.net.SocketException; import java.net.UnixDomainSocketAddress; -import java.util.function.Supplier; /** * {@link SocketAddress}-related helper methods. @@ -38,7 +37,7 @@ private SocketAddressUtil() { * @param address The address. * @return A supplier for the given address, or {@code null}. */ - static Supplier supplyAFUNIXSocketAddress(SocketAddress address) { + static AFSupplier supplyAFUNIXSocketAddress(SocketAddress address) { if (address instanceof UnixDomainSocketAddress) { UnixDomainSocketAddress udsa = (UnixDomainSocketAddress) address; diff --git a/junixsocket-common/src/main/java15/org/newsclub/net/unix/SocketAddressUtil.java b/junixsocket-common/src/main/java15/org/newsclub/net/unix/SocketAddressUtil.java index 2badb8610..a0b03965b 100644 --- a/junixsocket-common/src/main/java15/org/newsclub/net/unix/SocketAddressUtil.java +++ b/junixsocket-common/src/main/java15/org/newsclub/net/unix/SocketAddressUtil.java @@ -19,7 +19,6 @@ import java.net.SocketAddress; import java.net.SocketException; -import java.util.function.Supplier; /** * {@link SocketAddress}-related helper methods. @@ -37,7 +36,7 @@ private SocketAddressUtil() { * @param address The address. * @return A supplier for the given address, or {@code null}. */ - static Supplier supplyAFUNIXSocketAddress(SocketAddress address) { + static AFSupplier supplyAFUNIXSocketAddress(SocketAddress address) { return null; } }