public interface EnumMatchString
使用例子:
public enum SimType implements EnumIntegerCode, EnumMatchString
{
// 北京联通VPDN卡。
CU_VPDN_BJ
{
public int getCode()
{
return 1;
}
public boolean isMatch(String val)
{
return "1".equals(val);
}
},
// 无锡联通APN卡。
CU_APN_WX
{
public int getCode()
{
return 2;
}
public boolean isMatch(String val)
{
return "2".equals(val);
}
},
// 未知类型。
UNKNOWN
{
public int getCode()
{
return 0;
}
public boolean isMatch(String val)
{
return val == null || val.length() == 0;
}
};
// 通过整型值获取指定类型。
public static SimType get(int code)
{
for (SimType val : SimType.values())
{
if (val.getCode() == code)
{
return val;
}
}
return UNKNOWN;
}
// 通过字符串获取指定类型。
public static SimType get(String strVal)
{
for (SimType val : SimType.values())
{
if (val.isMatch(strVal))
{
return val;
}
}
return UNKNOWN;
}
}
可用于多值匹配的场景
public enum SimType implements EnumStringCode, EnumMatchString
{
// 北京联通VPDN卡。
CU_VPDN_BJ
{
public String getCode()
{
return "a";
}
public boolean isMatch(String val)
{
return "1".equals(val) || (val != null && val.length() >= 1 && "a".equalsIgnoreCase(val));
}
},
// 无锡联通APN卡。
CU_APN_WX
{
public String getCode()
{
return "b";
}
public boolean isMatch(String val)
{
return "2".equals(val);
}
},
// 未知类型。
UNKNOWN
{
public int getCode()
{
return 0;
}
public boolean isMatch(String val)
{
return val == null || val.length() == 0;
}
};
// 通过字符串获取指定类型。
public static SimType get(String strVal)
{
for (SimType val : SimType.values())
{
if (val.isMatch(strVal))
{
return val;
}
}
return UNKNOWN;
}
}
可用于多值匹配的场景
public enum SimType implements EnumStringCode, EnumMatchString
{
// 北京联通VPDN卡。
CU_VPDN_BJ
{
public String getCode()
{
return "a";
}
public boolean isMatch(String val)
{
return "1".equals(val) || (val != null && val.length() >= 1 && "a".equalsIgnoreCase(val));
}
},
// 无锡联通APN卡。
CU_APN_WX
{
public String getCode()
{
return "b";
}
public boolean isMatch(String val)
{
return "2".equals(val);
}
},
// 未知类型。
UNKNOWN
{
public int getCode()
{
return 0;
}
public boolean isMatch(String val)
{
return val == null || val.length() == 0;
}
};
// 通过字符串获取指定类型。
public static SimType get(String strVal)
{
for (SimType val : SimType.values())
{
if (val.isMatch(strVal))
{
return val;
}
}
return UNKNOWN;
}
}
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
isMatch(java.lang.String val)
值匹配时返回true。
|
Copyright © 2001-2014 hynnet.com