Skip to content

Commit

Permalink
feat: add setDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
Metaphoriker committed Sep 27, 2024
1 parent 0f05552 commit c4c6654
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.godcipher</groupId>
<artifactId>gutil</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/de/godcipher/config/BaseConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public abstract class BaseConfiguration {

private final Map<String, ConfigurationOption<?>> configOptions = new LinkedHashMap<>();
private final Properties properties = new Properties();
private final File file;

private File file;

/** Constructor for BaseConfiguration, uses the file name from the @Configuration annotation. */
public BaseConfiguration() {
Configuration configAnnotation = this.getClass().getAnnotation(Configuration.class);
if (configAnnotation == null || configAnnotation.fileName().isEmpty()) {
throw new IllegalStateException("Missing or empty @Configuration annotation with fileName.");
}

Configuration configAnnotation = retrieveConfigurationAnnotation();
this.file = new File(configAnnotation.fileName());
createDirectoryIfNotExists(file.getParentFile());
}
Expand All @@ -38,6 +35,29 @@ public void initialize() {
saveConfiguration();
}

/**
* Set the directory where the configuration file should be saved.
*
* @param directory The directory path as a String or File
*/
public void setDirectory(File directory) {
if (directory == null) {
throw new IllegalArgumentException("The directory must not be null");
}

Configuration configAnnotation = retrieveConfigurationAnnotation();
this.file = new File(directory, configAnnotation.fileName());
createDirectoryIfNotExists(directory);
}

private Configuration retrieveConfigurationAnnotation() {
Configuration configAnnotation = this.getClass().getAnnotation(Configuration.class);
if (configAnnotation == null || configAnnotation.fileName().isEmpty()) {
throw new IllegalStateException("Missing or empty @Configuration annotation with fileName.");
}
return configAnnotation;
}

/**
* Adds a configuration option to the internal map.
*
Expand Down

0 comments on commit c4c6654

Please sign in to comment.