Skip to content

Commit

Permalink
fix: Reduce High severity issues reported by SonarCloud #2865 (#3103)
Browse files Browse the repository at this point in the history
* fix: add UnsupportedOperationException in KingsHand and GameLoopTest

* refactor: update DatabaseService to use constant for BINARY_DATA and remove obsolete test files

* refactor: update timePasses method in KingsHand

* refactor CandyGame.java

* refactor: simplify boundary checks in CandyGame

* feat: add getters for type and points in Candy class and initialize logger in CandyGame

* remove class and logger initialization in CandyGame

* Modify CandyGame.java

* fix Checkstyle violations

* Remove generic wildcard type in ThreatAwareSystem
  • Loading branch information
SalmaAzeem authored Dec 7, 2024
1 parent b375919 commit 297e429
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public void onEvent(Event e) {

@Override
public void timePasses(Weekday day) {
// This method is intentionally left empty because KingsHand does not handle time-based events directly.
// It serves as a placeholder to fulfill the EventObserver interface contract.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Represents system that is aware of threats that are present in it.
*/
public interface ThreatAwareSystem {
public interface ThreatAwareSystem<T extends Threat> {

/**
* Returns the system id.
Expand All @@ -43,13 +43,13 @@ public interface ThreatAwareSystem {
* Returns list of threats for this system.
* @return list of threats for this system.
*/
List<? extends Threat> threats();
List<T> threats();

/**
* Returns the instance of {@link Filterer} helper interface that allows to covariantly
* specify lower bound for predicate that we want to filter by.
* @return an instance of {@link Filterer} helper interface.
*/
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
Filterer<ThreatAwareSystem<T>, T> filtered();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
*/
package com.iluwatar.gameloop;

import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -46,6 +45,7 @@ void setup() {
gameLoop = new GameLoop() {
@Override
protected void processGameLoop() {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void startupService()
throws SQLException {
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
if (dataTypeDb.equals("BINARY")) {
if (dataTypeDb.equals(BINARY_DATA)) {
statement.execute(CREATE_BINARY_SCHEMA_DDL);
} else {
statement.execute(CREATE_TEXT_SCHEMA_DDL);
Expand Down
13 changes: 7 additions & 6 deletions serialized-lob/src/main/java/com/iluwatar/slob/lob/Forest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
* Creates an object Forest which contains animals and plants as its constituents. Animals may eat
* plants or other animals in the forest.
*/


@Data
@NoArgsConstructor
@AllArgsConstructor
public class Forest implements Serializable {

public static final String HORIZONTAL_DIVIDER = "\n--------------------------\n";
private String name;
private Set<Animal> animals = new HashSet<>();
private Set<Plant> plants = new HashSet<>();
Expand Down Expand Up @@ -105,16 +106,16 @@ public String toString() {
sb.append("Forest Name = ").append(name).append("\n");
sb.append("Animals found in the ").append(name).append(" Forest: \n");
for (Animal animal : animals) {
sb.append("\n--------------------------\n");
sb.append(HORIZONTAL_DIVIDER);
sb.append(animal.toString());
sb.append("\n--------------------------\n");
sb.append(HORIZONTAL_DIVIDER);
}
sb.append("\n");
sb.append("Plants in the ").append(name).append(" Forest: \n");
for (Plant plant : plants) {
sb.append("\n--------------------------\n");
sb.append(HORIZONTAL_DIVIDER);
sb.append(plant.toString());
sb.append("\n--------------------------\n");
sb.append(HORIZONTAL_DIVIDER);
}
return sb.toString();
}
Expand Down
11 changes: 6 additions & 5 deletions type-object/src/main/java/com/iluwatar/typeobject/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
* This project is licensed under the MIT license. Module model-view-viewmodel
* is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
Expand Down Expand Up @@ -27,12 +28,12 @@
import lombok.extern.slf4j.Slf4j;

/**
* <p>Type object pattern is the pattern we use when the OOP concept of creating a base class and
* Type object pattern is the pattern we use when the OOP concept of creating a base class and
* inheriting from it just doesn't work for the case in hand. This happens when we either don't know
* what types we will need upfront, or want to be able to modify or add new types conveniently w/o
* recompiling repeatedly. The pattern provides a solution by allowing flexible creation of required
* objects by creating one class, which has a field which represents the 'type' of the object.</p>
* <p>In this example, we have a mini candy-crush game in action. There are many different candies
* objects by creating one class, which has a field which represents the 'type' of the object.
* In this example, we have a mini candy-crush game in action. There are many different candies
* in the game, which may change over time, as we may want to upgrade the game. To make the object
* creation convenient, we have a class {@link Candy} which has a field name, parent, points and
* Type. We have a json file {@link candy} which contains the details about the candies, and this is
Expand All @@ -41,7 +42,7 @@
* how crushing can be done, how the matrix is to be reconfigured and how points are to be gained.
* The {@link CellPool} class is a pool which reuses the candy cells that have been crushed instead
* of making new ones repeatedly. The {@link CandyGame} class has the rules for the continuation of
* the game and the {@link App} class has the game itself.</p>
* the game and the {@link App} class has the game itself.
*/

@Slf4j
Expand Down
12 changes: 8 additions & 4 deletions type-object/src/main/java/com/iluwatar/typeobject/CandyGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The CandyGame class contains the rules for the continuation of the game and has the game matrix
Expand All @@ -37,7 +39,6 @@
@Slf4j
@SuppressWarnings("java:S3776") //"Cognitive Complexity of methods should not be too high"
public class CandyGame {

Cell[][] cells;
CellPool pool;
int totalPoints;
Expand Down Expand Up @@ -85,17 +86,20 @@ List<Cell> adjacentCells(int y, int x) {
if (x == 0) {
adjacent.add(this.cells[y][1]);
}
if (y == cells.length - 1) {
if (y == cells.length - 1 && cells.length > 1) {
adjacent.add(this.cells[cells.length - 2][x]);
}
if (x == cells.length - 1) {

if (x == cells.length - 1 && cells.length > 1) {
adjacent.add(this.cells[y][cells.length - 2]);
}


if (y > 0 && y < cells.length - 1) {
adjacent.add(this.cells[y - 1][x]);
adjacent.add(this.cells[y + 1][x]);
}
if (x > 0 && x < cells.length - 1) {
if (y >= 0 && y < cells.length && x > 0 && x < cells[y].length - 1) {
adjacent.add(this.cells[y][x - 1]);
adjacent.add(this.cells[y][x + 1]);
}
Expand Down

0 comments on commit 297e429

Please sign in to comment.