簡體   English   中英

枚舉中的java重復代碼

[英]java duplicated code in enum

我在我的程序中有重復的代碼,我有枚舉從屬性文件加載值,我想讓我的代碼更清潔。

也許一個Interface可以作為解決方案,但我不能聲明一個非最終變量。

這是一個例子:

public enum AlertMessageEnum{
    //
    OUTPUT_FOLDER_EXISTS, 
    ...
    CONFIG_FILE_IS_MISSING;
    // the file path to load properties
    private static final String PATH= "/i18n/alertDialogText.properties";
    private static Properties   properties;
    private String value;

    public void init() {
        if (properties == null) {
            properties = new Properties();
            try {
                properties.load(AlertMessageEnum.class.getResourceAsStream(PATH));
            }
            catch (Exception e) {
                throw new RthosRuntimeException(e);
            }
        }
        value = (String) properties.get(this.toString());
    }

    public String getValue() {
        if (value == null) {
            init();
        }
        return value;
    }
}

public enum ConverterErrorEnum{
    INVALID_EXTRACTION_PATH,
    ...
    PATIAL_DATA_GENERATED;

    private static final String PATH= "/i18n/converterErrorText.properties";
    private static Properties   properties;
    private String value;

    ...

}

使用普通的java代碼從屬性文件生成枚舉是不可能的。 您需要一個解決方法,例如:

  1. 使用一個發布這些常量的類是不可變的值
  2. 從屬性文件生成java源代碼
  3. 用反射生成java代碼

我建議選項1.例如單身:

package com.example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class Props {

    private static Props INSTANCE;

    public synchronized Props getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Props();
        }
        return INSTANCE;
    }

    private static final String PATH = "/i18n/converterErrorText.properties";
    private Properties properties;
    private List<String> keys;

    public Props() {
        properties = new Properties();
        keys = new ArrayList<>();
        try {
            properties.load(getClass().getResourceAsStream(PATH));
            for (Object key : properties.keySet()) {
                keys.add(key.toString());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public Enumeration<Object> getKeys() {
        return properties.keys();
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

}

委托另一個持有所有enum Properties類:

public class PropertyProvider {
    private static Map<Class<?>, Properties> pMap = new HashMap<>();

    public static String getValue(Enum<?> enumValue, final String path) {
        Properties properties = pMap.get(enumValue.getClass());
        if (properties == null) {
            properties = new Properties();
            try {
                properties.load(PropertyProvider.class.getResourceAsStream(path));
            }
            catch (Exception e) {
                throw new RthosRuntimeException(e);
            }
            pMap.put(enumValue.getClass(), properties);
        }
        return (String) properties.get(enumValue.toString());
    }
}

public enum ConverterErrorEnum{
    INVALID_EXTRACTION_PATH,
    ...
    PATIAL_DATA_GENERATED;

    private static final String PATH= "/i18n/converterErrorText.properties";
    private String value;
    ...

    public String getValue() {
        if (value == null) {
            value = PropertyProvider.getValue(this, PATH);
        }
        return value;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM