Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Remove problem and guava dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorstmann committed Dec 3, 2016
1 parent 118c519 commit 8655707
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 166 deletions.
22 changes: 2 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand All @@ -325,13 +320,7 @@
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem</artifactId>
<version>0.7.0</version>
</dependency>

<!-- Jackson -->
<!-- Jackson and required modules -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand All @@ -347,8 +336,6 @@
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>

<!-- problem handling and dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
Expand All @@ -364,11 +351,6 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.7.4</version>
</dependency>

<!-- testing -->
<dependency>
Expand All @@ -386,7 +368,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.86-beta</version>
<version>2.2.28</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/zalando/fahrschein/CursorManager.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.zalando.fahrschein;

import com.google.common.collect.Ordering;
import org.zalando.fahrschein.domain.Cursor;
import org.zalando.fahrschein.domain.Partition;
import org.zalando.fahrschein.domain.Subscription;

import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -16,7 +16,7 @@
*/
public interface CursorManager {

Ordering<String> OFFSET_ORDERING = Ordering.natural().nullsFirst().onResultOf((String offset) -> "BEGIN".equals(offset) ? null : Long.parseLong(offset));
Comparator<String> OFFSET_COMPARATOR = Comparator.nullsFirst(Comparator.comparing((String offset) -> "BEGIN".equals(offset) ? null : Long.parseLong(offset)));

void onSuccess(String eventName, Cursor cursor) throws IOException;

Expand Down Expand Up @@ -59,7 +59,7 @@ default void updatePartitions(String eventName, List<Partition> partitions) thro

for (Partition partition : partitions) {
final Cursor cursor = cursorsByPartition.get(partition.getPartition());
if (cursor == null || (!"BEGIN".equals(cursor.getOffset()) && OFFSET_ORDERING.compare(cursor.getOffset(), partition.getOldestAvailableOffset()) < 0)) {
if (cursor == null || (!"BEGIN".equals(cursor.getOffset()) && OFFSET_COMPARATOR.compare(cursor.getOffset(), partition.getOldestAvailableOffset()) < 0)) {
onSuccess(eventName, new Cursor(partition.getPartition(), "BEGIN"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import java.io.IOException;

import static com.google.common.base.Preconditions.checkState;
import static org.zalando.fahrschein.Preconditions.checkState;


public class ExponentialBackoffStrategy implements BackoffStrategy {
private static final Logger LOG = LoggerFactory.getLogger(ExponentialBackoffStrategy.class);
Expand Down
55 changes: 13 additions & 42 deletions src/main/java/org/zalando/fahrschein/IOProblem.java
Original file line number Diff line number Diff line change
@@ -1,90 +1,61 @@
package org.zalando.fahrschein;

import org.zalando.problem.Problem;

import org.springframework.http.HttpStatus;

import javax.annotation.Nullable;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;

@SuppressWarnings("serial")
public class IOProblem extends IOException implements Problem {
static final class Status implements Response.StatusType {
private final int statusCode;
private final String reasonPhrase;

Status(final int statusCode, final String reasonPhrase) {
this.statusCode = statusCode;
this.reasonPhrase = reasonPhrase;
}

@Override
public int getStatusCode() {
return statusCode;
}

@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(statusCode);
}

@Override
public String getReasonPhrase() {
return reasonPhrase;
}
}
public class IOProblem extends IOException {

private final URI type;
private final String title;
private final Response.StatusType status;
private final int statusCode;
@Nullable
private final String detail;
@Nullable
private final URI instance;

public IOProblem(final URI type, final String title, final Response.StatusType status, @Nullable final String detail, @Nullable final URI instance) {
super(formatMessage(type, title, status.getStatusCode(), detail));
public IOProblem(final URI type, final String title, final int statusCode, @Nullable final String detail, @Nullable final URI instance) {
super(formatMessage(type, title, statusCode, detail));
this.type = type;
this.title = title;
this.status = status;
this.statusCode = statusCode;
this.detail = detail;
this.instance = instance;
}

public IOProblem(final URI type, final String title, final Response.StatusType status, @Nullable final String detail) {
this(type, title, status, detail, null);
public IOProblem(final URI type, final String title, final int statusCode, @Nullable final String detail) {
this(type, title, statusCode, detail, null);
}

public IOProblem(final URI type, final String title, final Response.StatusType status) {
this(type, title, status, null, null);
public IOProblem(final URI type, final String title, final int statusCode) {
this(type, title, statusCode, null, null);
}

private static String formatMessage(final URI type, final String title, final int status, @Nullable final String detail) {
return String.format("Problem [%s] with status [%d]: [%s] [%s]", type, status, title, detail == null ? "" : detail);
}

@Override
public URI getType() {
return type;
}

@Override
public String getTitle() {
return title;
}

@Override
public Response.StatusType getStatus() {
return status;
public int getStatusCode() {
return statusCode;
}

@Override
public Optional<String> getDetail() {
return Optional.ofNullable(detail);
}

@Override
public Optional<URI> getInstance() {
return Optional.ofNullable(instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -83,7 +82,7 @@ public ManagedCursorManager(URI baseUri, ClientHttpRequestFactory clientHttpRequ

@Override
public void addSubscription(Subscription subscription) {
final String eventName = Iterables.getOnlyElement(subscription.getEventTypes());
final String eventName = subscription.getEventTypes().iterator().next();

LOG.debug("Adding subscription [{}] to event [{}]", subscription.getId(), eventName);

Expand All @@ -92,7 +91,7 @@ public void addSubscription(Subscription subscription) {

@Override
public void addStreamId(Subscription subscription, String streamId) {
final String eventName = Iterables.getOnlyElement(subscription.getEventTypes());
final String eventName = subscription.getEventTypes().iterator().next();

LOG.debug("Adding stream id [{}] for subscription [{}] to event [{}]", streamId, subscription.getId(), eventName);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zalando/fahrschein/NakadiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Collections;
import java.util.List;

import static com.google.common.base.Preconditions.checkState;
import static org.zalando.fahrschein.Preconditions.checkState;

public class NakadiClient {
private static final Logger LOG = LoggerFactory.getLogger(NakadiClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import javax.annotation.Nullable;
import java.net.URI;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Optional.ofNullable;
import static org.zalando.fahrschein.Preconditions.checkNotNull;

public final class NakadiClientBuilder {
public static final int DEFAULT_CONNECT_TIMEOUT = 500;
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/zalando/fahrschein/NakadiReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -38,10 +36,10 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.zalando.fahrschein.Preconditions.checkState;

class NakadiReader<T> implements IORunnable {

Expand All @@ -67,7 +65,7 @@ class NakadiReader<T> implements IORunnable {
private final MetricsCollector metricsCollector;

NakadiReader(URI uri, ClientHttpRequestFactory clientHttpRequestFactory, BackoffStrategy backoffStrategy, CursorManager cursorManager, ObjectMapper objectMapper, String eventName, Optional<Subscription> subscription, Optional<Lock> lock, Class<T> eventClass, Listener<T> listener, final MetricsCollector metricsCollector) {
checkState(!subscription.isPresent() || eventName.equals(Iterables.getOnlyElement(subscription.get().getEventTypes())), "Only subscriptions to single event types are currently supported");
checkState(!subscription.isPresent() || (subscription.get().getEventTypes().size() == 1 && eventName.equals(subscription.get().getEventTypes().iterator().next())), "Only subscriptions to single event types are currently supported");

this.uri = uri;
this.clientHttpRequestFactory = clientHttpRequestFactory;
Expand All @@ -83,10 +81,6 @@ class NakadiReader<T> implements IORunnable {
this.jsonFactory = objectMapper.getFactory();
this.eventReader = objectMapper.reader().forType(eventClass);
this.cursorHeaderWriter = DefaultObjectMapper.INSTANCE.writerFor(COLLECTION_OF_CURSORS);

if (clientHttpRequestFactory instanceof HttpComponentsClientHttpRequestFactory) {
LOG.warn("Using [{}] might block during reconnection, please consider using another implementation of ClientHttpRequestFactory", clientHttpRequestFactory.getClass().getName());
}
}

static class JsonInput implements Closeable {
Expand Down Expand Up @@ -258,7 +252,9 @@ public void run() throws IOException {
}
}

@VisibleForTesting
/*
* @VisibleForTesting
*/
void runInternal() throws IOException, BackoffException {
LOG.info("Starting to listen for events for [{}]", eventName);

Expand Down Expand Up @@ -307,7 +303,9 @@ void runInternal() throws IOException, BackoffException {
}
}

@VisibleForTesting
/*
* @VisibleForTesting
*/
void readSingleBatch() throws IOException {
try (final JsonInput jsonInput = openJsonInput()) {
final JsonParser jsonParser = jsonInput.getJsonParser();
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/zalando/fahrschein/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.zalando.fahrschein;

import javax.annotation.Nullable;

import static java.lang.String.format;

public final class Preconditions {
private Preconditions() {
}

public static void checkArgument(boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}

public static void checkArgument(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}

public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
if (!expression) {
throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs));
}
}

public static void checkState(boolean expression) {
if (!expression) {
throw new IllegalStateException();
}
}

public static void checkState(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalStateException(String.valueOf(errorMessage));
}
}

public static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
if (!expression) {
throw new IllegalStateException(format(errorMessageTemplate, errorMessageArgs));
}
}

public static <T> T checkNotNull(@Nullable T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}

public static <T> T checkNotNull(@Nullable T reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
return reference;
}

public static <T> T checkNotNull(@Nullable T reference, String errorMessageTemplate, Object... errorMessageArgs) {
if (reference == null) {
throw new NullPointerException(format(errorMessageTemplate, errorMessageArgs));
}
return reference;
}
}
Loading

0 comments on commit 8655707

Please sign in to comment.