Простая система конфигураций

Начинающий
Начинающий
Статус
Оффлайн
Регистрация
1 Ноя 2023
Сообщения
60
Реакции
3
Config:
Expand Collapse Copy
public class Config {

    private static String configExtension = ".config";
    private static Path configPath = Paths.get(".");
    private String configName;
    private File configFile;
    private Map<String, Map<String, Object>> configMap = new HashMap<>();

    public Config(String file) {
        this("", file);
    }

    public Config(String path, String file) {
        load(path, file);
    }

    public void addProperty(String item, String property, Object value) {
        configMap.computeIfAbsent(item, _ -> new HashMap<>()).put(property, value);
    }

    public Object getProperty(String item, String property) {
        return configMap.getOrDefault(item, new HashMap<>()).get(property);
    }

    public void removeProperty(String item, String property) {
        Map<String, Object> properties = configMap.get(item);
        if (properties != null) {
            properties.remove(property);
            if (properties.isEmpty()) {
                removeItem(item);
            }
        }
    }

    public void removeItem(String item) {
        configMap.remove(item);
    }

    public void load(String file) {
        load("", file);
    }

    public void load(String path, String file) {
        try {
            configFile = configPath.resolve(path + file + configExtension).toFile();
            if (!configFile.exists() || !configFile.isFile()) {
                configFile.createNewFile();
            }
            configName = configFile.getName();
            if (configFile.length() > 0) {
                try (var objectInputStream = new ObjectInputStream(new FileInputStream(configFile))) {
                    configMap = (Map<String, Map<String, Object>>) objectInputStream.readObject();
                }
            }
            // Autosave after JVM shutdown
            Runtime.getRuntime().addShutdownHook(new Thread(this::save));
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException("Failed to load configuration", e);
        }
    }

    public void save() {
        try (var objectOutputStream = new ObjectOutputStream(new FileOutputStream(configFile))) {
            objectOutputStream.writeObject(configMap);
        } catch (IOException e) {
            throw new RuntimeException("Failed to save configuration", e);
        }
    }

    public static void setConfigExtension(String extension) {
        Config.configExtension = "." + extension;
    }

    public static void setConfigPath(String path) {
        Config.configPath = Paths.get(path);
    }

    public Map<String, Map<String, Object>> getConfigMap() {
        return new HashMap<>(configMap);
    }

    public String getConfigName() {
        return configName;
    }
}
 
Последнее редактирование:
1723246536111.png

доброе утро, это или постфикс или суффикс
 
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
норм
 
это нихуя не просто даун
 
Config:
Expand Collapse Copy
public class Config {

    private static String configExtension = ".config";
    private static Path configPath = Paths.get(".");
    private String configName;
    private File configFile;
    private Map<String, Map<String, Object>> configMap = new HashMap<>();

    public Config(String file) {
        this("", file);
    }

    public Config(String path, String file) {
        load(path, file);
    }

    public void addProperty(String item, String property, Object value) {
        configMap.computeIfAbsent(item, _ -> new HashMap<>()).put(property, value);
    }

    public Object getProperty(String item, String property) {
        return configMap.getOrDefault(item, new HashMap<>()).get(property);
    }

    public void removeProperty(String item, String property) {
        Map<String, Object> properties = configMap.get(item);
        if (properties != null) {
            properties.remove(property);
            if (properties.isEmpty()) {
                removeItem(item);
            }
        }
    }

    public void removeItem(String item) {
        configMap.remove(item);
    }

    public void load(String file) {
        load("", file);
    }

    public void load(String path, String file) {
        try {
            configFile = configPath.resolve(path + file + configExtension).toFile();
            if (!configFile.exists() || !configFile.isFile()) {
                configFile.createNewFile();
            }
            configName = configFile.getName();
            if (configFile.length() > 0) {
                try (var objectInputStream = new ObjectInputStream(new FileInputStream(configFile))) {
                    configMap = (Map<String, Map<String, Object>>) objectInputStream.readObject();
                }
            }
            // Autosave after JVM shutdown
            Runtime.getRuntime().addShutdownHook(new Thread(this::save));
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException("Failed to load configuration", e);
        }
    }

    public void save() {
        try (var objectOutputStream = new ObjectOutputStream(new FileOutputStream(configFile))) {
            objectOutputStream.writeObject(configMap);
        } catch (IOException e) {
            throw new RuntimeException("Failed to save configuration", e);
        }
    }

    public static void setConfigExtension(String extension) {
        Config.configExtension = "." + extension;
    }

    public static void setConfigPath(String path) {
        Config.configPath = Paths.get(path);
    }

    public Map<String, Map<String, Object>> getConfigMap() {
        return new HashMap<>(configMap);
    }

    public String getConfigName() {
        return configName;
    }
}
годно
 
Для таких вещей есть kaml :roflanPominki:
 
chatgpt solutions
 
Назад
Сверху Снизу