基于枚举实现枚举的组合方式使用
一、第一种枚举类型
package com.enums;
public enum RefugeType {
/**
* 室内
*/
INDOOR(1, "室内"),
/**
* 室外
*/
OUTDOOR(2, "室外"),
;
private final Integer value;
private final String desc;
RefugeType(int value, String desc) {
this.value = value;
this.desc = desc;
}
public static RefugeType getRefugeType(int value) {
RefugeType refugeType = INDOOR;
for (RefugeType item : RefugeType.values()) {
if (value == item.getValue()) {
return item;
}
}
return null;
}
public Integer getValue() {
return value;
}
public String getDesc() {
return desc;
}
}
二、第二种枚举类型
package com.enums;
public enum RefugeType {
/**
* 室内
*/
INDOOR(1, "室内"),
/**
* 室外
*/
OUTDOOR(2, "室外"),
;
private final Integer value;
private final String desc;
RefugeType(int value, String desc) {
this.value = value;
this.desc = desc;
}
public static RefugeType getRefugeType(int value) {
RefugeType refugeType = INDOOR;
for (RefugeType item : RefugeType.values()) {
if (value == item.getValue()) {
return item;
}
}
return null;
}
public Integer getValue() {
return value;
}
public String getDesc() {
return desc;
}
}
三、将两种枚举类型组合
package com.enums;
import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Getter
public enum StatisticsType {
/**
* 室内的明细统计
*/
SEARCH_GOODSS_DETAIL(RefugeType.INDOOR, InspectType.ZERO){
@Override
public String getStatisticsTypeByParam(InspecStatisticsSearchDto dto) {
if(ObjectUtil.isEmpty(dto.getAreaId()) && ObjectUtil.isEmpty(dto.getStreetId()) ){
return RefugeType.INDOOR.name().concat("_").concat(InspectType.ZERO.getDesc());
}
if(ObjectUtil.isEmpty(dto.getStreetId())){
return RefugeType.INDOOR.name().concat("_").concat(InspectType.ZERO.getDesc()).concat("_").concat("Street");
}
return RefugeType.INDOOR.name().concat("_").concat(InspectType.ZERO.getDesc()).concat("_").concat("Area");
}
},
/**
* 室内的统计
*/
SEARCH_GOODSS_STATISTICS(RefugeType.INDOOR, InspectType.ONE) {
@Override
public String getStatisticsTypeByParam(InspecStatisticsSearchDto dto) {
if(ObjectUtil.isEmpty(dto.getAreaId()) && ObjectUtil.isEmpty(dto.getStreetId()) ){
return RefugeType.INDOOR.name().concat("_").concat(InspectType.ONE.getDesc());
}
if(ObjectUtil.isEmpty(dto.getStreetId())){
return RefugeType.INDOOR.name().concat("_").concat(InspectType.ONE.getDesc()).concat("_").concat("Street");
}
return RefugeType.INDOOR.name().concat("_").concat(InspectType.ONE.getDesc()).concat("_").concat("Area");
}
},
/**
* 室外的明细统计
*/
SEARCH_DEVICE_DETAIL(RefugeType.OUTDOOR, InspectType.ZERO){
@Override
public String getStatisticsTypeByParam(InspecStatisticsSearchDto dto) {
if(ObjectUtil.isEmpty(dto.getAreaId()) && ObjectUtil.isEmpty(dto.getStreetId()) ){
return RefugeType.OUTDOOR.name().concat("_").concat(InspectType.ZERO.getDesc());
}
if(ObjectUtil.isEmpty(dto.getStreetId())){
return RefugeType.OUTDOOR.name().concat("_").concat(InspectType.ZERO.getDesc()).concat("_").concat("Street");
}
return RefugeType.OUTDOOR.name().concat("_").concat(InspectType.ZERO.getDesc()).concat("_").concat("Area");
}
},
/**
* 室外的统计
*/
SEARCH_DEVICE_STATISTICS(RefugeType.OUTDOOR, InspectType.ONE){
@Override
public String getStatisticsTypeByParam(InspecStatisticsSearchDto dto) {
if(ObjectUtil.isEmpty(dto.getAreaId()) && ObjectUtil.isEmpty(dto.getStreetId()) ){
return RefugeType.OUTDOOR.name().concat("_").concat(InspectType.ONE.getDesc());
}
if(ObjectUtil.isEmpty(dto.getStreetId())){
return RefugeType.OUTDOOR.name().concat("_").concat(InspectType.ONE.getDesc()).concat("_").concat("Street");
}
return RefugeType.OUTDOOR.name().concat("_").concat(InspectType.ONE.getDesc()).concat("_").concat("Area");
}
},
;
StatisticsType(RefugeType refugeType, InspectType inspectType) {
this.refugeType = refugeType;
this.inspectType = inspectType;
}
private RefugeType refugeType;
private InspectType inspectType;
public static List<StatisticsType> getStatisticsTypeList(int value) {
return Arrays.stream(StatisticsType.values()).collect(Collectors.toList());
}
public static StatisticsType getStatisticsType(RefugeType refugeType, InspectType inspectType) {
return Arrays.stream(StatisticsType.values())
.filter(item->item.refugeType.equals(refugeType) && item.inspectType.equals(inspectType)
).findFirst().orElseGet(()->null);
}
public abstract String getStatisticsTypeByParam(InspecStatisticsSearchDto dto);
public static void main(String[] args) {
StatisticsType statisticsType = getStatisticsType(RefugeType.INDOOR, InspectType.ZERO);
System.out.println(statisticsType);
// InspecStatisticsSearchDto dto2 = new InspecStatisticsSearchDto();
// Arrays.stream(StatisticsType.values()).forEach(item-> System.out.println(item.getStatisticsTypeByParam(dto2)));
//
// System.out.println();
// InspecStatisticsSearchDto dto = new InspecStatisticsSearchDto();
// dto.setStreetId(1L);
// Arrays.stream(StatisticsType.values()).forEach(item-> System.out.println(item.getStatisticsTypeByParam(dto)));
//
// System.out.println();
// InspecStatisticsSearchDto dto1 = new InspecStatisticsSearchDto();
// dto1.setAreaId(1L);
// Arrays.stream(StatisticsType.values()).forEach(item-> System.out.println(item.getStatisticsTypeByParam(dto1)));
}
}
四、应用于函数式编程的策略模式
// 查询的分类
Map<StatisticsType, Function<InspecStatisticsSearchDto,RD<?>>> searchMap = CollUtil.newHashMap();
@PostConstruct
public void searchInit(){
// 室内明细
searchMap.put(StatisticsType.SEARCH_GOODSS_DETAIL, this::searchGoodsSByDetail);
// 室内按单位统计
searchMap.put(StatisticsType.SEARCH_GOODSS_STATISTICS, this::searchGoodsSByStatistics);
// 室外明细
searchMap.put(StatisticsType.SEARCH_DEVICE_DETAIL, this::searchInDeviceByDetail);
// 室外按单位统计
searchMap.put(StatisticsType.SEARCH_DEVICE_STATISTICS, this::searchInDeviceByStatistics);
}