Skip to content

Commit

Permalink
141 replace all null checks with nonnull annotation of lombok (#237)
Browse files Browse the repository at this point in the history
* using lombok annotations rather that manual null checks and locks

* handling some more inspection warnings

* Removing whole qualifier in javaDoc

* use jdk 22 underscore

---------

Co-authored-by: Jonas Pohl <[email protected]>
  • Loading branch information
Til7701 and JayPi4c authored Aug 5, 2024
1 parent 6e4d672 commit 203515b
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class AbstractController implements MessageShowingController {
protected abstract NotificationPane getNotificationPane();

protected void initNotificationPane() {
getNotificationPane().setOnHidden(e -> showNextMessage());
getNotificationPane().setOnHidden(_ -> showNextMessage());
}

@Override
Expand All @@ -43,7 +43,7 @@ public void showMessages(List<SceneChangeMessage> messages) {

private void showNextMessage() {
SceneChangeMessage message = queue.poll();
if (message == null)
if (message == null)
return;
log.info("Showing message {}", message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class ChatController {
public void initialize() {
chatListView.setItems(chatService.getChatMessages());
senderNameTextField.setText(chatService.getCurrentUsername());
chatListView.setCellFactory(listView -> new CustomListCell());
chatService.getChatMessages().addListener((ListChangeListener<String>) change -> onNewChatMessage());
root.widthProperty().addListener((observable, oldValue, newValue) -> updateScrollDownButtonPosition());
scrollDownButton.widthProperty().addListener((observable, oldValue, newValue) -> updateScrollDownButtonPosition());
chatTextField.heightProperty().addListener((observable, oldValue, newValue) -> updateScrollDownButtonPosition());
chatListView.setCellFactory(_ -> new CustomListCell());
chatService.getChatMessages().addListener((ListChangeListener<String>) _ -> onNewChatMessage());
root.widthProperty().addListener((_, _, _) -> updateScrollDownButtonPosition());
scrollDownButton.widthProperty().addListener((_, _, _) -> updateScrollDownButtonPosition());
chatTextField.heightProperty().addListener((_, _, _) -> updateScrollDownButtonPosition());
setPaused(false);
}

Expand All @@ -62,7 +62,7 @@ private void onScrollDownButton() {
private void onNewChatMessage() {
if (verticalBar == null) {
verticalBar = (ScrollBar) chatListView.lookup(".scroll-bar:vertical");
verticalBar.valueProperty().addListener((obs, oldValue, newValue) ->
verticalBar.valueProperty().addListener((_, _, newValue) ->
setPaused(newValue.doubleValue() < verticalBar.getMax())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ public void leaveLobby() {
public void initialize() {
userListView.setItems(lobbyService.getLobbyUsersList());

userListView.setCellFactory(userListView -> new ListCell<>() {
userListView.setCellFactory(_ -> new ListCell<>() {
@Override
protected void updateItem(IUser user, boolean empty) {
super.updateItem(user, empty);
IUser owner = lobbyService.getCurrentLobby().get().getOwner();
if (empty || user == null) {
setText(null);
} else {
setText(user.getUsername() + (user.equals(owner) ? " (Owner)" : ""));
}
lobbyService.getCurrentLobby().ifPresentOrElse(lobby -> {
IUser owner = lobby.getOwner();
if (empty || user == null) {
setText(null);
} else {
setText(user.getUsername() + (user.equals(owner) ? " (Owner)" : ""));
}
}, () -> log.warn("No lobby found"));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class MainMenuController {
private void initialize() {
lobbiesListView.setItems(sessionService.getLobbyList());
lobbiesListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
lobbiesListView.setCellFactory(lobbyInfoListView -> new ListCell<>() {
lobbiesListView.setCellFactory(_ -> new ListCell<>() {
@Override
protected void updateItem(LobbyInfo lobbyInfo, boolean empty) {
super.updateItem(lobbyInfo, empty);
Expand All @@ -52,7 +52,7 @@ protected void updateItem(LobbyInfo lobbyInfo, boolean empty) {
}
}
});
lobbiesListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) ->
lobbiesListView.getSelectionModel().selectedItemProperty().addListener((_, _, newValue) ->
joinLobbyButton.setDisable(newValue == null)
);
lobbiesListView.setOnMouseClicked(event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class RegisterController {
@FXML
private PasswordField passwordRepeatField;

// @FXML
// private JFXSnackbar snackbar;

@FXML
private void handleRegister() {
log.info("Register button clicked");
Expand Down Expand Up @@ -65,7 +62,6 @@ void onRegisterFailedResponse(RegisterFailedResponse rfr) {
Platform.runLater(() -> {
passwordField.setText("");
passwordRepeatField.setText("");
// snackbar.fireEvent(new JFXSnackbar.SnackbarEvent(new JFXSnackbarLayout("Registration Failed"), Duration.seconds(2), null));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,37 @@ public class SceneController {
private final ApplicationEventPublisher eventBus;

@EventListener
public void onLoginSuccessfulResponse(LoginSuccessfulResponse lsr) {
public void onLoginSuccessfulResponse(LoginSuccessfulResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.MAIN));
}

@EventListener
public void onLogoutSuccessfulResponse(LogoutSuccessfulResponse lsr) {
public void onLogoutSuccessfulResponse(LogoutSuccessfulResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.LOGIN));
}

@EventListener
public void onRegisterSuccessfulResponse(RegisterSuccessfulResponse rsr) {
public void onRegisterSuccessfulResponse(RegisterSuccessfulResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.LOGIN));
}

@EventListener
public void onLobbyCreatedSuccessfullyResponse(LobbyCreatedSuccessfullyResponse lcsr) {
public void onLobbyCreatedSuccessfullyResponse(LobbyCreatedSuccessfullyResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.LOBBY));
}

@EventListener
public void onJoinLobbySuccessfullyResponse(JoinLobbySuccessfullyResponse jlsr) {
public void onJoinLobbySuccessfullyResponse(JoinLobbySuccessfullyResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.LOBBY));
}

@EventListener
public void onLeaveLobbySuccessfullyResponse(LeaveLobbySuccessfullyResponse llsr) {
public void onLeaveLobbySuccessfullyResponse(LeaveLobbySuccessfullyResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.MAIN));
}

@EventListener
public void onDeletionSuccessfulResponse(DeletionSuccessfulResponse dsr) {
public void onDeletionSuccessfulResponse(DeletionSuccessfulResponse ignored) {
eventBus.publishEvent(new SceneChangeEvent(Scene.LOGIN, SceneChangeMessage.USER_DELETED_SUCCESSFULLY));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public enum SceneChangeEventType {
SUCCESS,
WARNING,
ERROR,
INFO;
INFO

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void start() {
log.info("Connection cancelled by user.");
close(ConnectionStatusEvent.Status.NOT_CONNECTED);
} else if (!f.isSuccess()) {
log.error("Connection failed!");
f.cause().printStackTrace();
log.error("Connection failed!", f.cause());
close(ConnectionStatusEvent.Status.FAILED);
} else {
log.info("Connected to server.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onJoinLobbySuccessfullyResponse(JoinLobbySuccessfullyResponse jlsr)
}

@EventListener
public void onLeaveLobbySuccessfullyResponse(LeaveLobbySuccessfullyResponse llsr) {
public void onLeaveLobbySuccessfullyResponse(LeaveLobbySuccessfullyResponse ignored) {
currentChatID = GLOBAL_CHAT_ID;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void init() {
* Extracts the locale from the filename of a language file.
* <p>
* The filename is expected to be in the format "messages_{languageTag}.properties".
* Example: "messages_de_DE.properties" -> {@link Locale.GERMANY}
* Example: "messages_de_DE.properties" -> {@link Locale#GERMANY}
*
* @param filename the filename of the language file
* @return the locale extracted from the filename
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,49 @@
package org.schlunzis.kurtama.server.auth;

import lombok.NonNull;
import org.schlunzis.kurtama.server.net.ISession;
import org.schlunzis.kurtama.server.user.ServerUser;
import org.schlunzis.zis.commons.collections.BiMap;
import org.schlunzis.zis.commons.collections.ConcurrentBiHashMap;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

@Component
class UserSessionMap {

private final BiMap<ServerUser, ISession> map = new ConcurrentBiHashMap<>();

public void put(ServerUser user, ISession session) {
Objects.requireNonNull(user);
Objects.requireNonNull(session);
public void put(@NonNull ServerUser user, @NonNull ISession session) {
map.put(user, session);
}

public Optional<ISession> get(ServerUser user) {
Objects.requireNonNull(user);
public Optional<ISession> get(@NonNull ServerUser user) {
return Optional.ofNullable(map.get(user));
}

public Optional<ServerUser> get(ISession session) {
Objects.requireNonNull(session);
public Optional<ServerUser> get(@NonNull ISession session) {
return Optional.ofNullable(map.getByValue(session));
}

public Collection<ISession> getFor(Collection<ServerUser> users) {
Objects.requireNonNull(users);
public Collection<ISession> getFor(@NonNull Collection<ServerUser> users) {
return map.getForKeys(users);
}

public Collection<ISession> getAllSessions() {
return map.values();
}

public boolean contains(ISession session) {
Objects.requireNonNull(session);
public boolean contains(@NonNull ISession session) {
return map.containsValue(session);
}

public void remove(ServerUser user) {
Objects.requireNonNull(user);
public void remove(@NonNull ServerUser user) {
map.remove(user);
}

public void remove(ISession session) {
Objects.requireNonNull(session);
public void remove(@NonNull ISession session) {
map.removeByValue(session);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.UUID;

@EqualsAndHashCode
Expand All @@ -18,15 +18,13 @@ public class Chat {
private final UUID id;
private final Collection<ServerUser> chatters = new ArrayList<>();

void addChatter(ServerUser user) {
Objects.requireNonNull(user);
void addChatter(@NonNull ServerUser user) {
if (!chatters.contains(user)) {
chatters.add(user);
}
}

void removeChatter(ServerUser user) {
Objects.requireNonNull(user);
void removeChatter(@NonNull ServerUser user) {
chatters.remove(user);
}

Expand Down
Loading

0 comments on commit 203515b

Please sign in to comment.