com.apus.hades.admin.cache.ConfigCache.java Source code

Java tutorial

Introduction

Here is the source code for com.apus.hades.admin.cache.ConfigCache.java

Source

/*
 * Copyright 2015 ireader.com All right reserved. This software is the
 * confidential and proprietary information of ireader.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with ireader.com.
 */
package com.apus.hades.admin.cache;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;

import com.apus.hades.core.CoreConstant;

/**
 * @Descriptions ?.
 * @date 201539
 * @author wangliping
 */
public enum ConfigCache {
    PIC_SIZE("pic_size", ','), LIMIT_PARTNER_IDS("limit_partner_ids", ','), REAL_AD_UP_DOWN("real_ad_up_down",
            'N'), REC_TAG_PAY_AMOUNT("rec_tag_pay_amount",
                    null), RESULT_PUBLISH_VERSION(CoreConstant.PUBLISH_VERSION_KEY, null),
    /** ??:??+?? */
    DEFAULT_MAILTO("default_mailto", null), CHANNEL_WHITE_LIST("channel_white_list",
            ','), PIC_PROPORTION("pic_proportion", ',');

    private final String key;

    private final Character separator;

    private ConfigCache(String key, Character separator) {
        this.key = key;
        this.separator = separator;
    }

    public String getKey() {
        return key;
    }

    public Character getSeparator() {
        return separator;
    }

    public String[] getValues() {
        String value = getStr(this.key);
        return StringUtils.isBlank(value) ? null : value.split(this.separator.toString());
    }

    public String getValue() {
        return getStr(this.key);
    }

    public double getDoubleValue() {
        String v = caches.get(this.getKey());
        String regex = "\\d+(.\\d+)?";
        if (null != v && v.matches(regex)) {
            return Double.parseDouble(v);
        }
        return 0d;
    }

    public List<String> getValueList() {
        String[] values = this.getValues();
        return null == values ? null : Arrays.asList(values);
    }

    private static Map<String, String> caches = new ConcurrentHashMap<String, String>();

    public static void put(String key, String value) {
        if (StringUtils.isNotBlank(key)) {
            caches.put(key, value);
        }
    }

    public static boolean retainAll(List<String> keys) {
        return caches.keySet().retainAll(keys);
    }

    /**
     * ?.
     * 
     * @param key 
     * @return
     */
    public static String getStr(String key) {
        return caches.get(key);
    }

    /**
     * ?.
     * 
     * @param key 
     * @param defaultValue 
     * @return
     */
    public static String getStr(String key, String defaultValue) {
        String value = getStr(key);
        return StringUtils.isBlank(value) ? defaultValue : value;
    }

}