Skip to content

Commit

Permalink
common: Make code Java 7 friendly
Browse files Browse the repository at this point in the history
We can eventually use retrolambda to generate Java 7-compatible class
bytecode.

Change the few places that currently use Java 8 API.
  • Loading branch information
kohlschuetter committed Sep 25, 2023
1 parent f5fabc0 commit 87a86d1
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.newsclub.net.unix;

@FunctionalInterface
public interface AFFunction<T, R> {

/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -647,7 +646,7 @@ static final int unwrapAddressDirectBufferInternal(ByteBuffer socketAddressBuffe
Objects.requireNonNull(address);

if (!(address instanceof AFSocketAddress)) {
Supplier<? extends AFSocketAddress> supp = AFUNIXSocketAddress.supportedAddressSupplier(
AFSupplier<? extends AFSocketAddress> supp = AFUNIXSocketAddress.supportedAddressSupplier(
address);
address = supp == null ? null : supp.get();
if (address == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.newsclub.net.unix;

@FunctionalInterface
interface AFSupplier<T> {

/**
* Gets a result.
*
* @return a result
*/
T get();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<AFUNIXSocketAddress> supplier = supportedAddressSupplier(address);
AFSupplier<AFUNIXSocketAddress> supplier = supportedAddressSupplier(address);
if (supplier == null) {
throw new SocketException("Unsupported address");
}
Expand Down Expand Up @@ -527,7 +526,7 @@ public static boolean isSupportedAddress(SocketAddress addr) {
* @param addr The address.
* @return The supplier, or {@code null}.
*/
static Supplier<AFUNIXSocketAddress> supportedAddressSupplier(SocketAddress addr) {
static AFSupplier<AFUNIXSocketAddress> supportedAddressSupplier(SocketAddress addr) {
if (addr == null) {
return null;
} else if (addr instanceof AFUNIXSocketAddress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -73,7 +72,7 @@ public final class FileDescriptorCast implements FileDescriptorAccess {
private static final Map<Class<?>, CastingProviderMap> PRIMARY_TYPE_PROVIDERS_MAP = Collections
.synchronizedMap(new HashMap<>());

private static final Function<FileDescriptor, FileInputStream> FD_IS_PROVIDER = System
private static final AFFunction<FileDescriptor, FileInputStream> FD_IS_PROVIDER = System
.getProperty("osv.version") != null ? LenientFileInputStream::new : FileInputStream::new;

private static final CastingProviderMap GLOBAL_PROVIDERS_FINAL = new CastingProviderMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,7 +42,7 @@
* @author Christian Kohlschütter
*/
final class NativeUnixSocket {
private static final CompletableFuture<Boolean> LOADED = new CompletableFuture<>();
private static final AtomicBoolean LOADED = new AtomicBoolean(false);

static final int DOMAIN_UNIX = 1;
static final int DOMAIN_TIPC = 30;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -38,7 +37,7 @@ private SocketAddressUtil() {
* @param address The address.
* @return A supplier for the given address, or {@code null}.
*/
static Supplier<AFUNIXSocketAddress> supplyAFUNIXSocketAddress(SocketAddress address) {
static AFSupplier<AFUNIXSocketAddress> supplyAFUNIXSocketAddress(SocketAddress address) {
if (address instanceof UnixDomainSocketAddress) {
UnixDomainSocketAddress udsa = (UnixDomainSocketAddress) address;

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

import java.net.SocketAddress;
import java.net.SocketException;
import java.util.function.Supplier;

/**
* {@link SocketAddress}-related helper methods.
Expand All @@ -37,7 +36,7 @@ private SocketAddressUtil() {
* @param address The address.
* @return A supplier for the given address, or {@code null}.
*/
static Supplier<AFUNIXSocketAddress> supplyAFUNIXSocketAddress(SocketAddress address) {
static AFSupplier<AFUNIXSocketAddress> supplyAFUNIXSocketAddress(SocketAddress address) {
return null;
}
}

0 comments on commit 87a86d1

Please sign in to comment.