由于枚举类只能实现接口,不能继承,导致枚举本身如果想要更多的功能,必须在枚举类内部实现接口的每一个方法。同一个枚举内还可以。但当需要写多个枚举实现相同的接口时,必须在每个枚举内进行方法实现。同样的代码存在多份。现在有了default关键字,该问题不再存在。在接口用default关键字把抽象方法进行实现。枚举实现该接口。达到一种伪继承的效果。该代码组件可以提高代码复用率,减少硬编码的存在,使代码更优雅等。比如:
角色相关操作:
java
public interface RoleBee extends Matcher {
String roleKey();
@Override
default Object statusCode() {
return roleKey();
}
default List<String> findRoleIdsByKey() {
return ZYRoleHelper.build().findRoleIdsByKey(this);
}
default String findRoleIdByKey() {
return findRoleIdByKey(ZYUserHelper.getLoginAreaId());
}
default String findRoleIdByKey(String areaId) {
Role role = ZYRoleHelper.build().areaId(areaId).try2getRoleByKey(this);
return null != role ? role.getId() : null;
}
default List<String> findUserIdsByRoleKey() {
return ZYRoleHelper.build().findUserIdsByRoleKey(roleKey(), ZYUserHelper.getLoginAreaId());
}
default List<String> findUserIdsByRoleKey(String areaId) {
return ZYRoleHelper.build().findUserIdsByRoleKey(roleKey(), areaId);
}
default boolean hasRole(String userId) {
return ZYRoleHelper.build().hasRole(userId, roleKey());
}
default boolean hasRole(String userId, String areaId) {
return ZYRoleHelper.build().hasRole(userId, roleKey(), areaId);
}
default <T> void completeIsRole(List<T> ts, Function<T, String> userIdFun, BiConsumer<T, Boolean> setIsMemberFun) {
RoleFiller.completeIsMember(this, ts, userIdFun, setIsMemberFun);
}
default void join(String userId) {
ZYRoleHelper.build().userId(userId).roleKeys(roleKey()).join();
}
default void clear(String userId) {
ZYRoleHelper.build().userId(userId).roleKeys(roleKey()).clear();
}
default void clearAll() {
ZYRoleHelper.build().roleKeys(roleKey()).clearAll();
}
default <T extends User> void setWrapper(LambdaQueryWrapper<T> wrapper) {
ZYRoleHelper.build().areaId(ZYUserHelper.getLoginAreaId()).setWrapper(this, wrapper);
}
default <T extends User> void setWrapper(QueryWrapper<T> wrapper) {
ZYRoleHelper.build().areaId(ZYUserHelper.getLoginAreaId()).setWrapper(this, wrapper);
}
}
public enum SecurityUserRole implements RoleBee {
WORKER("工作人员"),
ADMIN("管理员"),
PARTY_MEMBER("党员"),
DECLARER_MEMBER("人事任免申报员"),
ANONYMOUS("游客");
private String roleName;
SecurityUserRole(String roleName) {
this.roleName = roleName;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
@Override
public String roleKey() {
return this.name().toLowerCase();
}
}
业务代码中的使用场景
List<String> npcMemberIds = SecurityUserRole.NPC_MEMBER.findUserIdsByRoleKey();
字典:
java
public interface DictBee {
String dictCode();
default Object defaultValue() {
return null;
}
default DictColumn defaultDict() {
return defaultDict(defaultValue());
}
default DictColumn defaultYes() {
return defaultDict(YES);
}
default DictColumn defaultNo() {
return defaultDict(NO);
}
default DictColumn defaultDict(Object value) {
value = ZYStrUtils.isNotNull(value) ? value : defaultValue();
DictColumn dictColumn = ZYStrUtils.isNotNull(value) ? new DictColumn(String.valueOf(value)) : new DictColumn();
dictColumn.setDictCode(dictCode());
return dictColumn;
}
default String buildKeywordSql(String fieldName, String keyword) {
if (ZYStrUtils.isNull(keyword)) {
return "";
}
List<Dictionary> dicts = dicts();
if (null == dicts || dicts.isEmpty()) {
return "";
}
List<String> values = new ArrayList<>();
for (Dictionary dictionary : dicts) {
String label = dictionary.getLabel();
if (label.contains(keyword)) {
values.add("'" + dictionary.getValue() + "'");
}
}
if (values.isEmpty()) {
return "";
}
return "(" + fieldName + " IN (" + ZYStrUtils.join(values) + "))";
}
default List<String> dictValues(String labelKeyword) {
if (ZYStrUtils.isNull(labelKeyword)) {
return Collections.emptyList();
}
List<Dictionary> dicts = dicts();
if (null == dicts || dicts.isEmpty()) {
return Collections.emptyList();
}
List<String> values = new ArrayList<>();
for (Dictionary dictionary : dicts) {
String label = dictionary.getLabel();
if (label.contains(labelKeyword)) {
values.add("'" + dictionary.getValue() + "'");
}
}
return values;
}
// 设置默认值
default <T> void setDefaultDict(T t, String dictName, Function<T, DictColumn> getDict, BiConsumer<T, DictColumn> setDefault) {
if (null == t) {
return;
}
DictColumn apply = getDict.apply(t);
if (null == apply) {
return;
}
String value = apply.getValue();
if (ZYStrUtils.isNotNull(value)) {
return;
}
String dictValue = this.dictValue(dictName);
if (ZYStrUtils.isNotNull(dictValue)) {
setDefault.accept(t, new DictColumn(dictValue));
}
}
// 分组
default <T> Map<String, List<T>> group(List<T> ts, Function<T, DictColumn> getDict) {
return ZYListUtils.groupList(ts, t -> {
DictColumn moduleCode = getDict.apply(t);
if (null != moduleCode) {
return moduleCode.getValue();
}
return null;
});
}
default String dictName(String dictValue) {
return ZYDictHelper.dictName(this.dictCode(), dictValue);
}
default String dictName(Integer dictValue) {
return ZYDictHelper.dictName(this.dictCode(), dictValue);
}
default String dictValue(String dictName) {
return ZYDictHelper.dictValue(this.dictCode(), dictName);
}
default Integer dictIntValue(String dictName) {
return ZYDictHelper.dictIntValue(this.dictCode(), dictName);
}
default Selectors selectors() {
return ZYDictHelper.selectors(this.dictCode());
}
default List<Dictionary> dicts() {
return ZYDictHelper.dicts(this.dictCode());
}
default Integer getSort(String dictValue) {
return ZYDictHelper.getSort(this.dictCode(), dictValue);
}
default Dictionary dict(String dictValue) {
return ZYDictHelper.dict(this.dictCode(), dictValue);
}
default Map<String, String> dictContainer() {
return ZYDictHelper.dictContainer(this.dictCode());
}
}
public enum DependenceDict implements DictBee {
MODULE_CODE("模块名称");
private String desc;
DependenceDict(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
@Override
public String dictCode() {
return this.name().toLowerCase();
}
}
public enum ModuleUserDictBee implements DictBee {
SECTOR_TYPE("界别"),
SECTOR_TYPE_NATION("全国委员界别"),
PARTY("党派"),
SEX("性别"),
NATION("民族"),
EDUCATION("学历"),
COMMITTEE("专委会"),
SPECIALTY("所学专业"),
REPRESENTER_ELEMENT("组成结构");
private String dictName;
@Override
public String dictCode() {
return this.name().toLowerCase();
}
}
// 业务代码中的使用场景
String sectorTypeName = ModuleUserDictBee.SECTOR_TYPE.dictName(sectorType);
配置读取
java
public interface DBConfigBee {
String configCode();
default Object defaultValue() {
return null;
}
default String strValue() {
Object defaultValueObj = defaultValue();
boolean isStr = defaultValueObj instanceof String;
return strValue(isStr ? (String) defaultValueObj : null);
}
default String strValue(String defaultValue) {
return ZYDBConfigHelper.strValue(configCode(), defaultValue);
}
default Integer intValue() {
Object defaultValueObj = defaultValue();
boolean isInt = defaultValueObj instanceof Integer;
return intValue(isInt ? (Integer) defaultValueObj : null);
}
default Integer intValue(Integer defaultValue) {
return ZYDBConfigHelper.intValue(configCode(), defaultValue);
}
default Long longValue() {
Object defaultValueObj = defaultValue();
boolean isLong = defaultValueObj instanceof Long;
return longValue(isLong ? (Long) defaultValueObj : null);
}
default Long longValue(Long defaultValue) {
return ZYDBConfigHelper.longValue(configCode(), defaultValue);
}
default Boolean booleanValue() {
Object defaultValueObj = defaultValue();
boolean isBoolean = defaultValueObj instanceof Boolean;
return booleanValue(isBoolean ? (Boolean) defaultValueObj : null);
}
default Boolean booleanValue(Boolean defaultValue) {
return ZYDBConfigHelper.booleanValue(configCode(), defaultValue);
}
}
public enum DependenceDBConfigBee implements DBConfigBee {
TEXT_MESSAGE_LIMIT("短信限额", 100000),
ENABLE_SEND_TEXT_MESSAGE("全局是否开启短信发送", true);
private String desc;
private Object defaultValue;
@Override
public String configCode() {
return this.name().toLowerCase();
}
@Override
public Object defaultValue() {
return defaultValue;
}
}
// 业务代码中的使用场景
boolean isEnable = DependenceDBConfigBee.ENABLE_SEND_TEXT_MESSAGE.booleanValue();