Skip to content

Commit b8676ff

Browse files
author
nareshwart
committed
fix
1 parent 1e4c101 commit b8676ff

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

src/main/java/com/devopsdemo/helper/GenericResourceBundle.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public static String getProperties(String source) {
1313
}
1414

1515
public static ResourceBundle getBundle(String baseName, java.util.Locale locale) {
16-
return ResourceBundle.getBundle(baseName, locale);
16+
try {
17+
return ResourceBundle.getBundle(baseName, locale);
18+
} catch (java.util.MissingResourceException e) {
19+
throw new IllegalArgumentException("Resource bundle not found: " + baseName, e);
20+
}
1721
}
1822
}
1923

src/main/java/com/devopsdemo/utilities/CaseInsensitiveComparator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ protected int compareActual(Object v1, Object v2, String returnType) {
8080
if ("java.lang.Object".equals(obj) && v1 != null) {
8181
obj = v1.getClass().getName();
8282
}
83+
if (DATATYPE_STRING.equals(obj)) {
84+
if (v1 == null || v2 == null || ((String)v1).isEmpty() || ((String)v2).isEmpty()) {
85+
throw new StringIndexOutOfBoundsException("Cannot compare empty or null strings");
86+
}
87+
return ((String) v1).compareToIgnoreCase((String) v2) * determinePosition();
88+
}
8389
return switch (obj) {
8490
case DATATYPE_INTEGER -> ((Integer) v1).compareTo((Integer) v2) * determinePosition();
8591
case DATATYPE_LONG -> ((Long) v1).compareTo((Long) v2) * determinePosition();
86-
case DATATYPE_STRING -> ((String) v1).compareToIgnoreCase((String) v2) * determinePosition();
8792
case DATATYPE_DATE -> ((LocalDate) v1).compareTo((LocalDate) v2) * determinePosition();
8893
case DATATYPE_FLOAT -> ((Float) v1).compareTo((Float) v2) * determinePosition();
8994
case DATATYPE_DOUBLE -> ((Double) v1).compareTo((Double) v2) * determinePosition();

src/main/java/com/devopsdemo/utilities/HexAsciiConvertor.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ public class HexAsciiConvertor {
1010
*/
1111
public static String convertHexToASCII(String hexValue) {
1212
if (hexValue == null || hexValue.isEmpty()) {
13-
return null;
13+
throw new IllegalArgumentException("Hex value cannot be null or empty");
14+
}
15+
if (hexValue.length() % 2 != 0 || !hexValue.matches("[0-9A-Fa-f]+")) {
16+
throw new IllegalArgumentException("Invalid hex string");
1417
}
15-
1618
var outputAscii = new StringBuilder();
17-
try {
18-
for (int i = 0; i < hexValue.length(); i += 2) {
19-
var str = hexValue.substring(i, i + 2);
20-
outputAscii.append((char) Integer.parseInt(str, 16));
21-
}
22-
} catch (Exception ex) {
23-
LoggerStackTraceUtil.printErrorMessage(ex);
19+
for (int i = 0; i < hexValue.length(); i += 2) {
20+
var str = hexValue.substring(i, i + 2);
21+
outputAscii.append((char) Integer.parseInt(str, 16));
2422
}
2523
return outputAscii.toString();
2624
}

src/main/java/com/devopsdemo/utilities/LoggerStackTraceUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public class LoggerStackTraceUtil {
1515
* @return the stack trace string
1616
*/
1717
public static String getStackTrace(Exception ex) {
18-
if (ex == null) return "";
18+
if (ex == null) throw new NullPointerException("Exception cannot be null");
1919
StringBuilder sb = new StringBuilder();
2020
for (StackTraceElement elem : ex.getStackTrace()) {
2121
sb.append(elem.toString()).append(System.lineSeparator());
2222
}
23-
return sb.toString();
23+
return sb.length() > 0;
2424
}
2525

2626
private static final Logger LOG = LoggerFactory.getLogger(LoggerStackTraceUtil.class);

src/main/java/com/devopsdemo/utilities/PropertyLoader.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,19 @@ public static Properties loadProperties(String names, ClassLoader loader) {
4848
name = name.endsWith(SUFFIX) ? name.substring(0, name.length() - SUFFIX.length()) : name;
4949

5050
Properties result = new Properties();
51+
boolean loaded = false;
5152

5253
try {
5354
if (LOAD_AS_RESOURCE_BUNDLE) {
5455
name = name.replace('/', '.');
55-
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(),
56-
loader != null ? loader : ClassLoader.getSystemClassLoader());
57-
rb.keySet().forEach(key -> result.put(key, rb.getString(key)));
56+
try {
57+
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(),
58+
loader != null ? loader : ClassLoader.getSystemClassLoader());
59+
rb.keySet().forEach(key -> result.put(key, rb.getString(key)));
60+
loaded = true;
61+
} catch (java.util.MissingResourceException e) {
62+
throw new IllegalArgumentException("Resource bundle not found: " + name, e);
63+
}
5864
} else {
5965
name = name.replace('.', '/');
6066
if (!name.endsWith(SUFFIX)) {
@@ -64,16 +70,16 @@ public static Properties loadProperties(String names, ClassLoader loader) {
6470
try (InputStream in = loader != null ? loader.getResourceAsStream(name) : null) {
6571
if (in != null) {
6672
result.load(in);
73+
loaded = true;
6774
}
6875
}
6976
}
7077
} catch (Exception e) {
7178
LoggerStackTraceUtil.printErrorMessage(e);
7279
}
7380

74-
if (THROW_ON_LOAD_FAILURE && result.isEmpty()) {
75-
LOG.error("Could not load [{}] as {}", name,
76-
LOAD_AS_RESOURCE_BUNDLE ? "a resource bundle" : "a classloader resource");
81+
if (!loaded) {
82+
throw new IllegalArgumentException("Could not load properties: " + name);
7783
}
7884

7985
return result;

0 commit comments

Comments
 (0)