Skip to content

Add Spotless plugin for style #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 12 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name: Java CI

on: [push]
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 8, 11, 17, 21 ]
java: [ 8, 11 ]
name: Java ${{ matrix.java }} build
steps:
- uses: actions/checkout@v4
Expand All @@ -26,5 +32,8 @@ jobs:
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: ./mvnw clean install -Dgpg.skip
run: ./mvnw clean package -Dgpg.skip
- if: ${{ matrix.java != 8 }}
name: Check style with Spotless
run: ./mvnw spotless:check

Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,40 @@
package org.cloudfoundry.reactor;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

abstract class AbstractPayloadCachingRootProvider extends AbstractRootProvider {

private final ConcurrentMap<ConnectionContext, Mono<Map<String, String>>> payloads = new ConcurrentHashMap<>(1);
private final ConcurrentMap<ConnectionContext, Mono<Map<String, String>>> payloads =
new ConcurrentHashMap<>(1);

protected abstract Mono<Map<String, String>> doGetPayload(ConnectionContext connectionContext);

@Override
protected final Mono<UriComponents> doGetRoot(String key, ConnectionContext connectionContext) {
return this.payloads.computeIfAbsent(connectionContext, this::getPayload)
.map(payload -> {
if (!payload.containsKey(key)) {
throw new IllegalArgumentException(String.format("Payload does not contain key '%s;", key));
}

return normalize(UriComponentsBuilder.fromUriString(payload.get(key)));
});
return this.payloads
.computeIfAbsent(connectionContext, this::getPayload)
.map(
payload -> {
if (!payload.containsKey(key)) {
throw new IllegalArgumentException(
String.format("Payload does not contain key '%s;", key));
}

return normalize(UriComponentsBuilder.fromUriString(payload.get(key)));
});
}

abstract ObjectMapper getObjectMapper();

private Mono<Map<String, String>> getPayload(ConnectionContext connectionContext) {
Mono<Map<String, String>> cached = doGetPayload(connectionContext);

return connectionContext.getCacheDuration()
.map(cached::cache)
.orElseGet(cached::cache);
return connectionContext.getCacheDuration().map(cached::cache).orElseGet(cached::cache);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.cloudfoundry.reactor;

import io.netty.handler.codec.http.HttpHeaders;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.cloudfoundry.reactor.util.JsonCodec;
import org.cloudfoundry.reactor.util.Operator;
import org.cloudfoundry.reactor.util.OperatorContext;
Expand All @@ -27,10 +30,6 @@
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* An abstract implementation of {@link RootProvider} that ensures that returned values are trusted (if configured) and cached.
*/
Expand All @@ -47,16 +46,20 @@ public final void checkForValidApiHost() {
Matcher matcher = HOSTNAME_PATTERN.matcher(getApiHost());

if (!matcher.matches()) {
throw new IllegalArgumentException(String.format("API hostname %s is not correctly formatted (e.g. 'api.local.pcfdev.io')", getApiHost()));
throw new IllegalArgumentException(
String.format(
"API hostname %s is not correctly formatted (e.g."
+ " 'api.local.pcfdev.io')",
getApiHost()));
}
}

public Mono<Operator> createOperator(ConnectionContext connectionContext) {
HttpClient httpClient = connectionContext.getHttpClient();
return getRoot(connectionContext)
.map(root -> OperatorContext.of(connectionContext, root))
.map(operatorContext -> new Operator(operatorContext, httpClient))
.map(operator -> operator.headers(this::addHeaders));
.map(root -> OperatorContext.of(connectionContext, root))
.map(operatorContext -> new Operator(operatorContext, httpClient))
.map(operator -> operator.headers(this::addHeaders));
}

/**
Expand All @@ -66,32 +69,32 @@ public Mono<Operator> createOperator(ConnectionContext connectionContext) {

@Override
public final Mono<String> getRoot(String key, ConnectionContext connectionContext) {
Mono<String> cached = doGetRoot(key, connectionContext)
.delayUntil(uri -> trust(uri.getHost(), uri.getPort(), connectionContext))
.map(UriComponents::toUriString);
Mono<String> cached =
doGetRoot(key, connectionContext)
.delayUntil(uri -> trust(uri.getHost(), uri.getPort(), connectionContext))
.map(UriComponents::toUriString);

return connectionContext.getCacheDuration()
.map(cached::cache)
.orElseGet(cached::cache);
return connectionContext.getCacheDuration().map(cached::cache).orElseGet(cached::cache);
}

@Override
public final Mono<String> getRoot(ConnectionContext connectionContext) {
Mono<String> cached = doGetRoot(connectionContext)
.delayUntil(uri -> trust(uri.getHost(), uri.getPort(), connectionContext))
.map(UriComponents::toUriString);
Mono<String> cached =
doGetRoot(connectionContext)
.delayUntil(uri -> trust(uri.getHost(), uri.getPort(), connectionContext))
.map(UriComponents::toUriString);

return connectionContext.getCacheDuration()
.map(cached::cache)
.orElseGet(cached::cache);
return connectionContext.getCacheDuration().map(cached::cache).orElseGet(cached::cache);
}

protected abstract Mono<UriComponents> doGetRoot(ConnectionContext connectionContext);

protected abstract Mono<UriComponents> doGetRoot(String key, ConnectionContext connectionContext);
protected abstract Mono<UriComponents> doGetRoot(
String key, ConnectionContext connectionContext);

protected final UriComponents getRoot() {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance().scheme("https").host(getApiHost());
UriComponentsBuilder builder =
UriComponentsBuilder.newInstance().scheme("https").host(getApiHost());
getPort().ifPresent(builder::port);

return normalize(builder);
Expand Down Expand Up @@ -135,5 +138,4 @@ private String getScheme() {
private Mono<Void> trust(String host, int port, ConnectionContext connectionContext) {
return connectionContext.trust(host, port);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package org.cloudfoundry.reactor;

import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;
import java.util.Optional;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

/**
* Common, reusable, connection context
Expand Down Expand Up @@ -60,5 +59,4 @@ public interface ConnectionContext {
* @param port the port of the endpoint to trust
*/
Mono<Void> trust(String host, int port);

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ public interface RootProvider {
* @return the normalized API root
*/
Mono<String> getRoot(String key, ConnectionContext connectionContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ public interface TokenProvider {
*
* @param connectionContext A {@link ConnectionContext} to be used to identity which connection the tokens should be invalidated for
*/
default void invalidate(ConnectionContext connectionContext) {
}

default void invalidate(ConnectionContext connectionContext) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.cloudfoundry.reactor.client;

import static org.cloudfoundry.util.tuple.TupleUtils.consumer;

import com.github.zafarkhaja.semver.Version;
import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.client.v2.info.GetInfoRequest;
Expand All @@ -24,8 +26,6 @@
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

import static org.cloudfoundry.util.tuple.TupleUtils.consumer;

final class CloudFoundryClientCompatibilityChecker {

private final Logger logger = LoggerFactory.getLogger("cloudfoundry-client.compatibility");
Expand All @@ -38,21 +38,28 @@ final class CloudFoundryClientCompatibilityChecker {

void check() {
this.info
.get(GetInfoRequest.builder()
.build())
.map(response -> Version.valueOf(response.getApiVersion()))
.zipWith(Mono.just(Version.valueOf(CloudFoundryClient.SUPPORTED_API_VERSION)))
.subscribe(consumer((server, supported) -> logCompatibility(server, supported, this.logger)), t -> this.logger.error("An error occurred while checking version compatibility:", t));
.get(GetInfoRequest.builder().build())
.map(response -> Version.valueOf(response.getApiVersion()))
.zipWith(Mono.just(Version.valueOf(CloudFoundryClient.SUPPORTED_API_VERSION)))
.subscribe(
consumer(
(server, supported) ->
logCompatibility(server, supported, this.logger)),
t ->
this.logger.error(
"An error occurred while checking version compatibility:",
t));
}

private static void logCompatibility(Version server, Version supported, Logger logger) {
String message = "Client supports API version {} and is connected to server with API version {}. Things may not work as expected.";
String message =
"Client supports API version {} and is connected to server with API version {}."
+ " Things may not work as expected.";

if (server.greaterThan(supported)) {
logger.info(message, supported, server);
} else if (server.lessThan(supported)) {
logger.warn(message, supported, server);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ public final class MethodNameComparator implements Comparator<Method> {

public static final MethodNameComparator INSTANCE = new MethodNameComparator();

private MethodNameComparator() {
}
private MethodNameComparator() {}

@Override
public int compare(Method a, Method b) {
return a.getName().compareTo(b.getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,36 @@

package org.cloudfoundry.reactor.client;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.cloudfoundry.QueryParameter;
import org.cloudfoundry.reactor.util.AnnotationUtils;
import org.cloudfoundry.reactor.util.AnnotationUtils.AnnotatedValue;
import org.cloudfoundry.reactor.util.UriQueryParameter;
import org.cloudfoundry.reactor.util.UriQueryParameterBuilder;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A builder for Cloud Foundry queries
*/
public final class QueryBuilder implements UriQueryParameterBuilder {

public Stream<UriQueryParameter> build(Object instance) {
return AnnotationUtils.streamAnnotatedValues(instance, QueryParameter.class)
.map(QueryBuilder::processValue)
.filter(Objects::nonNull);
.map(QueryBuilder::processValue)
.filter(Objects::nonNull);
}

private static UriQueryParameter processCollection(QueryParameter queryParameter, Object value) {
return processValue(queryParameter.value(), ((Collection<?>) value).stream()
.map(Object::toString)
.map(String::trim)
.collect(Collectors.joining(queryParameter.delimiter())));
private static UriQueryParameter processCollection(
QueryParameter queryParameter, Object value) {
return processValue(
queryParameter.value(),
((Collection<?>) value)
.stream()
.map(Object::toString)
.map(String::trim)
.collect(Collectors.joining(queryParameter.delimiter())));
}

private static UriQueryParameter processValue(AnnotatedValue<QueryParameter> annotatedValue) {
Expand All @@ -58,5 +61,4 @@ private static UriQueryParameter processValue(AnnotatedValue<QueryParameter> ann
private static UriQueryParameter processValue(String name, String value) {
return UriQueryParameter.of(name, value);
}

}
Loading