使用 interface嵌套interface的 方式,能够层级更为清楚的使用一些静态常量
Eclipse快捷键:
Ctrl+Shift +X 变大写
Ctrl+Shift +Y 变小写
package constants;
public interface Constants {
/**
* 月份
*/
public interface Month{
public static final String JANUARY="01";
public static final String FEBRUARY="02";
public static final String MARCH="03";
public static final String APRIL="04";
public static final String MAY="05";
public static final String JUNE="06";
public static final String JULY="07";
public static final String AUGUST="08";
public static final String SEPTEMBER="09";
public static final String OCTOBER="10";
public static final String NOVEMBER="11";
public static final String DECEMBER="12";
}
/**
* 生肖
*/
public interface TheChineseZodiac{
}
}
通过枚举配置的方式,相对"优雅"的将一些使用多次且繁杂的判断逻辑,通过交并集的方式处理
package util;
import org.apache.commons.lang3.StringUtils;
/**
* 判断月份所属季节和气候工具
*/
public class SeasonlUtil {
/**
* 温暖季节: 春、夏、秋
* @param month
* @return
*/
public static boolean chcekWarm(String month) {
return checkSrping(month)||checkSummer(month)||checkAutumn(month);
}
/**
* 凉爽季节:秋、冬、春
* @param month
* @return
*/
public static boolean chcekCool(String month) {
return checkSrping(month)||checkAutumn(month)||checkWinter(month);
}
/**
* 春天
* @param month
* @return
*/
public static boolean checkSrping(String month) {
boolean flag=false;
if(StringUtils.isNotBlank(month)) {
for (SpringEnum spring : SpringEnum.values()) {
if(spring.value.equals(month)){
flag=true;
}
}
}
return flag;
}
/**
* 夏天
* @param month
* @return
*/
public static boolean checkSummer(String month) {
boolean flag=false;
if(StringUtils.isNotBlank(month)) {
for (SummerEnum spring : SummerEnum.values()) {
if(spring.value.equals(month)){
flag=true;
}
}
}
return flag;
}
/**
* 秋天
* @param month
* @return
*/
public static boolean checkAutumn(String month) {
boolean flag=false;
if(StringUtils.isNotBlank(month)) {
for (AutumnEnum spring : AutumnEnum.values()) {
if(spring.value.equals(month)){
flag=true;
}
}
}
return flag;
}
/**
* 冬天
* @param month
* @return
*/
public static boolean checkWinter(String month) {
boolean flag=false;
if(StringUtils.isNotBlank(month)) {
for (WinterEnum spring : WinterEnum.values()) {
if(spring.value.equals(month)){
flag=true;
}
}
}
return flag;
}
//--------------------------------↓↓配置枚举↓↓-------------------------------------------
/**
* 春季 2、3、4 月
*/
private enum SpringEnum {
FEBRUARY(Constants.Month.FEBRUARY),
MARCH(Constants.Month.MARCH),
APRIL(Constants.Month.APRIL);
private String value;
SpringEnum(String value) {
this.value = value;
}
}
/**
* 夏季 5、6、7 月
*/
private enum SummerEnum {
MAY(Constants.Month.MAY),
JUNE(Constants.Month.JUNE),
JULY(Constants.Month.JULY);
private String value;
SummerEnum(String value) {
this.value = value;
}
}
/**
* 秋季 8、9、10 月
*/
private enum AutumnEnum {
AUGUST(Constants.Month.AUGUST),
SEPTEMBER(Constants.Month.SEPTEMBER),
OCTOBER(Constants.Month.OCTOBER);
private String value;
AutumnEnum(String value) {
this.value = value;
}
}
/**
* 冬季 11、12、1 月
*/
private enum WinterEnum {
NOVEMBER(Constants.Month.NOVEMBER),
DECEMBER(Constants.Month.DECEMBER),
JANUARY(Constants.Month.JANUARY);
private String value;
WinterEnum(String value) {
this.value = value;
}
}
//--------------------------------↑↑配置枚举↑↑-------------------------------------------
}