DynamicEnum.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package cn.com.qmth.examcloud.commons.helpers;
  2. import java.io.Serializable;
  3. import org.apache.commons.lang3.StringUtils;
  4. import cn.com.qmth.examcloud.commons.util.StringUtil;
  5. /**
  6. * 动态枚举
  7. *
  8. * @author WANGWEI
  9. * @date 2018年8月23日
  10. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  11. */
  12. public final class DynamicEnum implements Serializable {
  13. private static final long serialVersionUID = -694709140246715214L;
  14. /**
  15. * ID
  16. */
  17. private Long id;
  18. /**
  19. * name
  20. */
  21. private String name;
  22. /**
  23. * 描述
  24. */
  25. private String desc;
  26. /**
  27. * 值类型
  28. */
  29. private String valueType;
  30. public Long getId() {
  31. return id;
  32. }
  33. public void setId(Long id) {
  34. this.id = id;
  35. }
  36. public String getName() {
  37. return name;
  38. }
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42. public String getDesc() {
  43. return desc;
  44. }
  45. public void setDesc(String desc) {
  46. this.desc = desc;
  47. }
  48. public String getValueType() {
  49. return valueType;
  50. }
  51. public void setValueType(String valueType) {
  52. this.valueType = valueType;
  53. }
  54. /**
  55. * 是否合法
  56. *
  57. * @author WANGWEI
  58. * @param value
  59. * @return
  60. */
  61. public Boolean isLegal(String value) {
  62. if (StringUtils.isNotBlank(value) && null != valueType) {
  63. BasicDataType dataType = BasicDataType.valueOf(valueType);
  64. if (dataType.equals(BasicDataType.BOOLEAN)) {
  65. try {
  66. StringUtil.toBoolean(value);
  67. return true;
  68. } catch (Exception e) {
  69. return false;
  70. }
  71. } else if (dataType.equals(BasicDataType.INTEGER)) {
  72. try {
  73. StringUtil.toInteger(value);
  74. return true;
  75. } catch (Exception e) {
  76. return false;
  77. }
  78. } else if (dataType.equals(BasicDataType.LONG)) {
  79. try {
  80. StringUtil.toLong(value);
  81. return true;
  82. } catch (Exception e) {
  83. return false;
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. }