Skip to content

Commit 5a644f1

Browse files
sims-keshriohbus
andauthored
refactoring: Critical Sonar Issues (iluwatar#1833)
* Resolve Sonar Code Smell: Define a constant instead of duplicating this literal 'Space rocket <' 4 times. * Resolve Sonar Critical Code Smell: Define a constant instead of duplicating this literal 'Error connecting to MongoDB' 4 times. * Fix checkstyle violation. * Resolve Sonar Critical Code Smell: Define a constant instead of duplicating this literal 'LITERAL 0' 4 times. Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
1 parent cab9048 commit 5a644f1

File tree

3 files changed

+32
-20
lines changed
  • async-method-invocation/src/main/java/com/iluwatar/async/method/invocation
  • bytecode/src/main/java/com/iluwatar/bytecode
  • caching/src/main/java/com/iluwatar/caching

3 files changed

+32
-20
lines changed

async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/App.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@
5959
@Slf4j
6060
public class App {
6161

62+
private static final String ROCKET_LAUNCH_LOG_PATTERN = "Space rocket <%s> launched successfully";
63+
6264
/**
6365
* Program entry point.
6466
*/
67+
6568
public static void main(String[] args) throws Exception {
6669
// construct a new executor that will run async tasks
6770
var executor = new ThreadAsyncExecutor();
@@ -87,9 +90,9 @@ public static void main(String[] args) throws Exception {
8790
asyncResult5.await();
8891

8992
// log the results of the tasks, callbacks log immediately when complete
90-
log("Space rocket <" + result1 + "> launch complete");
91-
log("Space rocket <" + result2 + "> launch complete");
92-
log("Space rocket <" + result3 + "> launch complete");
93+
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, result1));
94+
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, result2));
95+
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, result3));
9396
}
9497

9598
/**
@@ -102,7 +105,7 @@ public static void main(String[] args) throws Exception {
102105
private static <T> Callable<T> lazyval(T value, long delayMillis) {
103106
return () -> {
104107
Thread.sleep(delayMillis);
105-
log("Space rocket <" + value + "> launched successfully");
108+
log(String.format(ROCKET_LAUNCH_LOG_PATTERN, value));
106109
return value;
107110
};
108111
}

bytecode/src/main/java/com/iluwatar/bytecode/App.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
@Slf4j
4343
public class App {
4444

45+
private static final String LITERAL_0 = "LITERAL 0";
46+
private static final String HEALTH_PATTERN = "%s_HEALTH";
47+
private static final String GET_AGILITY = "GET_AGILITY";
48+
private static final String GET_WISDOM = "GET_WISDOM";
49+
private static final String ADD = "ADD";
50+
private static final String LITERAL_2 = "LITERAL 2";
51+
private static final String DIVIDE = "DIVIDE";
52+
4553
/**
4654
* Main app method.
4755
*
@@ -53,17 +61,17 @@ public static void main(String[] args) {
5361
new Wizard(45, 7, 11, 0, 0),
5462
new Wizard(36, 18, 8, 0, 0));
5563

56-
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
57-
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
58-
vm.execute(InstructionConverterUtil.convertToByteCode("GET_HEALTH"));
59-
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
60-
vm.execute(InstructionConverterUtil.convertToByteCode("GET_AGILITY"));
61-
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
62-
vm.execute(InstructionConverterUtil.convertToByteCode("GET_WISDOM"));
63-
vm.execute(InstructionConverterUtil.convertToByteCode("ADD"));
64-
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 2"));
65-
vm.execute(InstructionConverterUtil.convertToByteCode("DIVIDE"));
66-
vm.execute(InstructionConverterUtil.convertToByteCode("ADD"));
67-
vm.execute(InstructionConverterUtil.convertToByteCode("SET_HEALTH"));
64+
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_0));
65+
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_0));
66+
vm.execute(InstructionConverterUtil.convertToByteCode(String.format(HEALTH_PATTERN, "GET")));
67+
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_0));
68+
vm.execute(InstructionConverterUtil.convertToByteCode(GET_AGILITY));
69+
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_0));
70+
vm.execute(InstructionConverterUtil.convertToByteCode(GET_WISDOM));
71+
vm.execute(InstructionConverterUtil.convertToByteCode(ADD));
72+
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_2));
73+
vm.execute(InstructionConverterUtil.convertToByteCode(DIVIDE));
74+
vm.execute(InstructionConverterUtil.convertToByteCode(ADD));
75+
vm.execute(InstructionConverterUtil.convertToByteCode(String.format(HEALTH_PATTERN, "SET")));
6876
}
6977
}

caching/src/main/java/com/iluwatar/caching/DbManager.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public final class DbManager {
5050
private static boolean useMongoDB;
5151

5252
private static Map<String, UserAccount> virtualDB;
53+
private static final String ERROR_MESSAGE_LOG = "Error connecting to MongoDB";
5354

5455
private DbManager() {
5556
}
@@ -85,7 +86,7 @@ public static UserAccount readFromDb(String userId) {
8586
try {
8687
connect();
8788
} catch (ParseException e) {
88-
LOGGER.error("Error connecting to MongoDB", e);
89+
LOGGER.error(ERROR_MESSAGE_LOG, e);
8990
}
9091
}
9192
var iterable = db
@@ -112,7 +113,7 @@ public static void writeToDb(UserAccount userAccount) {
112113
try {
113114
connect();
114115
} catch (ParseException e) {
115-
LOGGER.error("Error connecting to MongoDB", e);
116+
LOGGER.error(ERROR_MESSAGE_LOG, e);
116117
}
117118
}
118119
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne(
@@ -134,7 +135,7 @@ public static void updateDb(UserAccount userAccount) {
134135
try {
135136
connect();
136137
} catch (ParseException e) {
137-
LOGGER.error("Error connecting to MongoDB", e);
138+
LOGGER.error(ERROR_MESSAGE_LOG, e);
138139
}
139140
}
140141
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
@@ -155,7 +156,7 @@ public static void upsertDb(UserAccount userAccount) {
155156
try {
156157
connect();
157158
} catch (ParseException e) {
158-
LOGGER.error("Error connecting to MongoDB", e);
159+
LOGGER.error(ERROR_MESSAGE_LOG, e);
159160
}
160161
}
161162
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(

0 commit comments

Comments
 (0)