Skip to content
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

And nullable/nonnullable annotation to improve Kotlin API #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions centrifuge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.google.protobuf:protobuf-javalite:3.21.12'
implementation 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.0'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
ntoskrnl marked this conversation as resolved.
Show resolved Hide resolved

testImplementation 'junit:junit:4.13.2'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

import io.github.centrifugal.centrifuge.internal.backoff.Backoff;
import io.github.centrifugal.centrifuge.internal.protocol.Protocol;

Expand Down Expand Up @@ -352,7 +354,7 @@ public void onClosed(WebSocket webSocket, int code, String reason) {
}

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
super.onFailure(webSocket, t, response);
try {
Client.this.executor.submit(() -> {
Expand Down Expand Up @@ -770,7 +772,7 @@ private void sendRefresh() {
}
return;
}
if (result.getExpires()) {
if (result != null && result.getExpires()) {
int ttl = result.getTtl();
Client.this.refreshTask = Client.this.scheduler.schedule(Client.this::sendRefresh, ttl, TimeUnit.SECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public interface CompletionCallback {
/**
* Called when operation done. Caller must check possible error (it's null in case of success).
*/
void onDone(Throwable e);
void onDone(@Nullable Throwable e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import java.net.UnknownHostException;
import java.util.List;

import javax.annotation.Nonnull;

/**
* Represents DNS client used to resolve addresses of a host.
*/
public interface Dns {

@Nonnull
List<InetAddress> resolve(String host) throws UnknownHostException;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public class RefreshError extends Throwable {
private final Throwable error;

Expand All @@ -8,7 +10,7 @@ public class RefreshError extends Throwable {
this.error = error;
}

public Throwable getError() {
public @Nullable Throwable getError() {
return error;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public interface ResultCallback<T> {
/**
* onDone will be called when the operation completed. Either Throwable or T will be not null.
*/
void onDone(Throwable e, T result);
void onDone(@Nullable Throwable e, @Nullable T result);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public class ServerSubscribedEvent {
ServerSubscribedEvent(String channel, Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, StreamPosition streamPosition, byte[] data) {
ServerSubscribedEvent(String channel, Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, @Nullable StreamPosition streamPosition, @Nullable byte[] data) {
this.channel = channel;
this.wasRecovering = wasRecovering;
this.recovered = recovered;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public class SubscribedEvent {
SubscribedEvent(Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, StreamPosition streamPosition, byte[] data) {
SubscribedEvent(Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, @Nullable StreamPosition streamPosition, @Nullable byte[] data) {
this.wasRecovering = wasRecovering;
this.recovered = recovered;
this.data = data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public interface TokenCallback {
/* Call this with exception or token. If called with null error and empty token then client considers access unauthorized */
void Done(Throwable e, String token);
void Done(@Nullable Throwable e, @Nullable String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* File: package-info.java
* Make all method parameters @NonNull by default
*/
@ParametersAreNonnullByDefault
package io.github.centrifugal.centrifuge;

import javax.annotation.ParametersAreNonnullByDefault;
1 change: 1 addition & 0 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation project(':centrifuge')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -26,13 +27,16 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class MainActivity extends AppCompatActivity {

private Client client;
private boolean activityPaused;

@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.text);
Expand Down
1 change: 1 addition & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dependencies {
implementation project(':centrifuge')
implementation 'ch.qos.logback:logback-classic:1.0.1'
implementation 'ch.qos.logback:logback-core:1.0.1'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.centrifugal.centrifuge.example;

import io.github.centrifugal.centrifuge.Client;
import io.github.centrifugal.centrifuge.ClientState;
import io.github.centrifugal.centrifuge.ConnectingEvent;
import io.github.centrifugal.centrifuge.ConnectionTokenEvent;
import io.github.centrifugal.centrifuge.ConnectionTokenGetter;
import io.github.centrifugal.centrifuge.DuplicateSubscriptionException;
import io.github.centrifugal.centrifuge.HistoryOptions;
import io.github.centrifugal.centrifuge.JoinEvent;
Expand All @@ -28,14 +25,16 @@
import io.github.centrifugal.centrifuge.SubscriptionErrorEvent;
import io.github.centrifugal.centrifuge.SubscribedEvent;
import io.github.centrifugal.centrifuge.SubscriptionOptions;
import io.github.centrifugal.centrifuge.TokenCallback;
import io.github.centrifugal.centrifuge.UnsubscribedEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.nio.charset.StandardCharsets.UTF_8;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class Main {

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
Expand Down Expand Up @@ -182,15 +181,15 @@ public void onLeave(Subscription sub, LeaveEvent event) {
});

sub.presenceStats((err, res) -> {
if (err != null) {
if (res == null || err != null) {
System.out.println("error presence stats: " + err);
return;
}
System.out.println("Num clients connected: " + res.getNumClients());
});

sub.history(new HistoryOptions.Builder().withLimit(-1).build(), (err, res) -> {
if (err != null) {
if (res == null || err != null) {
System.out.println("error history: " + err);
return;
}
Expand Down