Skip to content

Commit

Permalink
Changed Switch statement over class name instead of instance due to i…
Browse files Browse the repository at this point in the history
…nconsistent constructor signatures
  • Loading branch information
TheDavSmasher committed Dec 23, 2024
1 parent bd439c3 commit e40ea6a
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/main/java/edu/byu/cs/dataAccess/sql/ConfigurationSqlDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.byu.cs.dataAccess.sql;

import ch.qos.logback.core.subst.Token;
import com.ctc.wstx.shaded.msv_core.datatype.xsd.TokenType;
import edu.byu.cs.dataAccess.ConfigurationDao;
import edu.byu.cs.dataAccess.DataAccessException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -58,28 +60,28 @@ public <T> T getConfiguration(Configuration key, Class<T> type) throws DataAcces
}
}

private <T> T getValue(Configuration key, String value, Class<T> type) throws ReflectiveOperationException {
Object typedObj = type.getDeclaredConstructor().newInstance();
private <T> T getValue(Configuration key, String value, Class<T> type) {
String className = type.getSimpleName();

if (value.equals(DEFAULT_VALUE)) {
LOGGER.warn("Using default configuration value for key: {} of type {}", key, type);

return type.cast(switch (typedObj) {
case String ignored -> "";
case Integer ignored -> 0;
case Boolean ignored -> false;
case Instant ignored -> Instant.MAX;
case Float ignored -> 0f;
return type.cast(switch (className) {
case "String" -> "";
case "Integer" -> 0;
case "Boolean" -> false;
case "Instant" -> Instant.MAX;
case "Float" -> 0f;
default -> throw new IllegalArgumentException("Unsupported configuration type: " + type);
});
}

return type.cast(switch (typedObj) {
case String ignored -> value;
case Integer ignored -> Integer.parseInt(value);
case Boolean ignored -> Boolean.parseBoolean(value);
case Instant ignored -> Instant.parse(value);
case Float ignored -> Float.parseFloat(value);
return type.cast(switch (className) {
case "String" -> value;
case "Integer" -> Integer.parseInt(value);
case "Boolean" -> Boolean.parseBoolean(value);
case "Instant" -> Instant.parse(value);
case "Float" -> Float.parseFloat(value);
default -> throw new IllegalArgumentException("Unsupported configuration type: " + type);
});
}
Expand Down

0 comments on commit e40ea6a

Please sign in to comment.