Skip to content

Commit 297e429

Browse files
authored
fix: Reduce High severity issues reported by SonarCloud #2865 (#3103)
* 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
1 parent b375919 commit 297e429

File tree

9 files changed

+29
-21
lines changed

9 files changed

+29
-21
lines changed

event-aggregator/src/main/java/com/iluwatar/event/aggregator/KingsHand.java

+2
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ public void onEvent(Event e) {
4343

4444
@Override
4545
public void timePasses(Weekday day) {
46+
// This method is intentionally left empty because KingsHand does not handle time-based events directly.
47+
// It serves as a placeholder to fulfill the EventObserver interface contract.
4648
}
4749
}

filterer/src/main/java/com/iluwatar/filterer/threat/ThreatAwareSystem.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* Represents system that is aware of threats that are present in it.
3232
*/
33-
public interface ThreatAwareSystem {
33+
public interface ThreatAwareSystem<T extends Threat> {
3434

3535
/**
3636
* Returns the system id.
@@ -43,13 +43,13 @@ public interface ThreatAwareSystem {
4343
* Returns list of threats for this system.
4444
* @return list of threats for this system.
4545
*/
46-
List<? extends Threat> threats();
46+
List<T> threats();
4747

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

5555
}

game-loop/src/test/java/com/iluwatar/gameloop/GameLoopTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
*/
2525
package com.iluwatar.gameloop;
2626

27-
import static org.junit.jupiter.api.Assertions.assertFalse;
28-
2927
import org.junit.jupiter.api.AfterEach;
3028
import org.junit.jupiter.api.Assertions;
29+
import static org.junit.jupiter.api.Assertions.assertFalse;
3130
import org.junit.jupiter.api.BeforeEach;
3231
import org.junit.jupiter.api.Test;
3332

@@ -46,6 +45,7 @@ void setup() {
4645
gameLoop = new GameLoop() {
4746
@Override
4847
protected void processGameLoop() {
48+
throw new UnsupportedOperationException("Not supported yet.");
4949
}
5050
};
5151
}

serialized-lob/src/main/java/com/iluwatar/slob/dbservice/DatabaseService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void startupService()
9292
throws SQLException {
9393
try (var connection = dataSource.getConnection();
9494
var statement = connection.createStatement()) {
95-
if (dataTypeDb.equals("BINARY")) {
95+
if (dataTypeDb.equals(BINARY_DATA)) {
9696
statement.execute(CREATE_BINARY_SCHEMA_DDL);
9797
} else {
9898
statement.execute(CREATE_TEXT_SCHEMA_DDL);

serialized-lob/src/main/java/com/iluwatar/slob/lob/Forest.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@
3737
import org.w3c.dom.Document;
3838
import org.w3c.dom.Element;
3939
import org.w3c.dom.NodeList;
40-
4140
/**
4241
* Creates an object Forest which contains animals and plants as its constituents. Animals may eat
4342
* plants or other animals in the forest.
4443
*/
44+
45+
4546
@Data
4647
@NoArgsConstructor
4748
@AllArgsConstructor
4849
public class Forest implements Serializable {
49-
50+
public static final String HORIZONTAL_DIVIDER = "\n--------------------------\n";
5051
private String name;
5152
private Set<Animal> animals = new HashSet<>();
5253
private Set<Plant> plants = new HashSet<>();
@@ -105,16 +106,16 @@ public String toString() {
105106
sb.append("Forest Name = ").append(name).append("\n");
106107
sb.append("Animals found in the ").append(name).append(" Forest: \n");
107108
for (Animal animal : animals) {
108-
sb.append("\n--------------------------\n");
109+
sb.append(HORIZONTAL_DIVIDER);
109110
sb.append(animal.toString());
110-
sb.append("\n--------------------------\n");
111+
sb.append(HORIZONTAL_DIVIDER);
111112
}
112113
sb.append("\n");
113114
sb.append("Plants in the ").append(name).append(" Forest: \n");
114115
for (Plant plant : plants) {
115-
sb.append("\n--------------------------\n");
116+
sb.append(HORIZONTAL_DIVIDER);
116117
sb.append(plant.toString());
117-
sb.append("\n--------------------------\n");
118+
sb.append(HORIZONTAL_DIVIDER);
118119
}
119120
return sb.toString();
120121
}

type-object/src/main/java/com/iluwatar/typeobject/App.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel
3+
* is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
34
*
45
* The MIT License
56
* Copyright © 2014-2022 Ilkka Seppälä
@@ -27,12 +28,12 @@
2728
import lombok.extern.slf4j.Slf4j;
2829

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

4748
@Slf4j

type-object/src/main/java/com/iluwatar/typeobject/CandyGame.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.ArrayList;
2929
import java.util.List;
3030
import lombok.extern.slf4j.Slf4j;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3133

3234
/**
3335
* The CandyGame class contains the rules for the continuation of the game and has the game matrix
@@ -37,7 +39,6 @@
3739
@Slf4j
3840
@SuppressWarnings("java:S3776") //"Cognitive Complexity of methods should not be too high"
3941
public class CandyGame {
40-
4142
Cell[][] cells;
4243
CellPool pool;
4344
int totalPoints;
@@ -85,17 +86,20 @@ List<Cell> adjacentCells(int y, int x) {
8586
if (x == 0) {
8687
adjacent.add(this.cells[y][1]);
8788
}
88-
if (y == cells.length - 1) {
89+
if (y == cells.length - 1 && cells.length > 1) {
8990
adjacent.add(this.cells[cells.length - 2][x]);
9091
}
91-
if (x == cells.length - 1) {
92+
93+
if (x == cells.length - 1 && cells.length > 1) {
9294
adjacent.add(this.cells[y][cells.length - 2]);
9395
}
96+
97+
9498
if (y > 0 && y < cells.length - 1) {
9599
adjacent.add(this.cells[y - 1][x]);
96100
adjacent.add(this.cells[y + 1][x]);
97101
}
98-
if (x > 0 && x < cells.length - 1) {
102+
if (y >= 0 && y < cells.length && x > 0 && x < cells[y].length - 1) {
99103
adjacent.add(this.cells[y][x - 1]);
100104
adjacent.add(this.cells[y][x + 1]);
101105
}

0 commit comments

Comments
 (0)