Java tutorial
/* * Copyright 2015 dactiv * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.dactiv.fear.service.commons; import com.github.dactiv.fear.commons.Casts; import com.github.dactiv.fear.commons.ValueEnum; import com.github.dactiv.fear.service.service.config.ConfigService; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.collections.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; /** * ?? * * @author maurice */ @Component public class Configs { /** * ?? */ public static final String DEFAULT_VALUE_NAME = "value"; /** * ?? */ public static final String DEFAULT_KEY_NAME = "key"; // ?? private static ConfigService configService; /** * ?? * * @param configService ?? */ @Autowired private void setSystemVariableService(ConfigService configService) { Configs.configService = configService; } /** * {@link ValueEnum} ?? class ??? * * @param enumClass class * * @return key value ? Map ? */ public static List<Map<String, Object>> find(Class<? extends Enum<? extends ValueEnum<?>>> enumClass) { return find(enumClass, Lists.newArrayList()); } /** * {@link ValueEnum} ?? class ??? * * @param enumClass class * @param ignore ? * * @return key value ? Map ? */ public static List<Map<String, Object>> find(Class<? extends Enum<? extends ValueEnum<?>>> enumClass, List ignore) { List<Map<String, Object>> result = Lists.newArrayList(); Enum<? extends ValueEnum<?>>[] values = enumClass.getEnumConstants(); for (Enum<? extends ValueEnum<?>> o : values) { ValueEnum<?> ve = (ValueEnum<?>) o; Object value = ve.getValue(); if (ignore != null && !ignore.contains(value)) { Map<String, Object> dictionary = Maps.newHashMap(); dictionary.put(DEFAULT_VALUE_NAME, value); dictionary.put(DEFAULT_KEY_NAME, ve.getName()); result.add(dictionary); } } return result; } /** * ??? * * @param code ? * * @return key value ? Map ? */ public static List<Map<String, Object>> find(String code) { return find(code, Lists.<String>newArrayList()); } /** * ??? * * @param code ? * @param ignore ? * * @return key value ? Map ? */ public static List<Map<String, Object>> find(String code, List<String> ignore) { List<Map<String, Object>> dataDictionaries = configService.getDataDictionaries(code); for (Map<String, Object> data : dataDictionaries) { String value = Casts.cast(data.get("value"), String.class); if (ignore != null && !ignore.contains(value)) { String type = data.get("type").toString(); data.put("value", Casts.cast(value, type)); } } return dataDictionaries; } /** * ?? * * @param code ? * * @return ? Map */ public static Map<String, Object> get(String code) { Map<String, Object> result = configService.getDataDictionary(code); if (MapUtils.isNotEmpty(result)) { String value = Casts.cast(result.get("value"), String.class); String type = Casts.cast(result.get("type"), String.class); result.put("value", Casts.cast(value, type)); } return result; } /** * ?? * * @param code ? * @param <T> * * @return */ public static <T> T getValue(String code) { Map<String, Object> result = get(code); return Casts.cast(result.get("value")); } }