Java 枚举活用
2013-09-22 22:39 TONY|小四 阅读(719) 评论(0) 编辑 收藏 举报1 /** 2 * 联盟神兽、神兽资源枚举 3 * 4 * @author TonyCody 5 * @author 13-9-22 6 * @time 下午5:35 7 */ 8 public enum LegionDogzType { 9 10 FOOD(1) { 11 @Override 12 public int getResource(LegionDogzVO dogz) { 13 return dogz.getFood(); 14 } 15 16 @Override 17 public boolean isCDUp(UserLimitVO ul) { 18 return cdUp(this, ul); 19 } 20 21 @Override 22 public int getCDTime(UserLimitVO ul) { 23 return ul.getDogzFoodSec(); 24 } 25 26 @Override 27 public void increateCDTime(UserLimitVO ul, int sec) { 28 ul.setDogzFoodSec(sec); 29 } 30 31 @Override 32 public void increateResource(LegionDogzVO dogz, int value) { 33 dogz.setFood(dogz.getFood() + value); 34 } 35 }, 36 WOOD(2) { 37 @Override 38 public int getResource(LegionDogzVO dogz) { 39 return dogz.getWood(); 40 } 41 42 @Override 43 public int getCDTime(UserLimitVO ul) { 44 return ul.getDogzWoodSec(); 45 } 46 47 @Override 48 public boolean isCDUp(UserLimitVO ul) { 49 return cdUp(this, ul); 50 } 51 52 @Override 53 public void increateCDTime(UserLimitVO ul, int sec) { 54 ul.setDogzWoodSec(sec); 55 } 56 57 @Override 58 public void increateResource(LegionDogzVO dogz, int value) { 59 dogz.setWood(dogz.getWood() + value); 60 } 61 }, 62 STONE(3) { 63 @Override 64 public int getResource(LegionDogzVO dogz) { 65 return dogz.getStone(); 66 } 67 68 @Override 69 public int getCDTime(UserLimitVO ul) { 70 return ul.getDogzStoneSec(); 71 } 72 73 @Override 74 public boolean isCDUp(UserLimitVO ul) { 75 return cdUp(this, ul); 76 } 77 78 @Override 79 public void increateCDTime(UserLimitVO ul, int sec) { 80 ul.setDogzStoneSec(sec); 81 } 82 83 @Override 84 public void increateResource(LegionDogzVO dogz, int value) { 85 dogz.setStone(dogz.getStone() + value); 86 } 87 }, 88 BRONZE(4) { 89 @Override 90 public int getResource(LegionDogzVO dogz) { 91 return dogz.getBronze(); 92 } 93 94 @Override 95 public int getCDTime(UserLimitVO ul) { 96 return ul.getDogzBronzeSec(); 97 } 98 99 @Override 100 public boolean isCDUp(UserLimitVO ul) { 101 return cdUp(this, ul); 102 } 103 104 @Override 105 public void increateCDTime(UserLimitVO ul, int sec) { 106 ul.setDogzBronzeSec(sec); 107 } 108 109 @Override 110 public void increateResource(LegionDogzVO dogz, int value) { 111 dogz.setBronze(dogz.getBronze() + value); 112 } 113 }, 114 COPPERS(5) { 115 @Override 116 public int getResource(LegionDogzVO dogz) { 117 return dogz.getCoppers(); 118 } 119 120 @Override 121 public int getCDTime(UserLimitVO ul) { 122 return ul.getDogzCoppersSec(); 123 } 124 125 @Override 126 public boolean isCDUp(UserLimitVO ul) { 127 return cdUp(this, ul); 128 } 129 130 @Override 131 public void increateCDTime(UserLimitVO ul, int sec) { 132 ul.setDogzCoppersSec(sec); 133 } 134 135 @Override 136 public void increateResource(LegionDogzVO dogz, int value) { 137 dogz.setCoppers(dogz.getCoppers() + value); 138 } 139 }; 140 141 public static LegionDogzType getLegionDogzType(int type) { 142 for (LegionDogzType e : LegionDogzType.values()) { 143 if (e.getType() == type) { 144 return e; 145 } 146 } 147 return null; 148 } 149 150 /** 151 * 增加资源 152 * 153 * @param dogz 154 * @param value 155 */ 156 public abstract void increateResource(LegionDogzVO dogz, int value); 157 158 /** 159 * 获取资源 160 * 161 * @param dogz 162 */ 163 public abstract int getResource(LegionDogzVO dogz); 164 165 /** 166 * 设置 CD 时间 167 * 168 * @param ul 169 * @param sec 170 */ 171 public abstract void increateCDTime(UserLimitVO ul, int sec); 172 173 /** 174 * 获取 CD 时间 175 * 176 * @param ul 177 * 178 * @return 179 */ 180 public abstract int getCDTime(UserLimitVO ul); 181 182 /** 183 * 是否到 CD 时间 184 * 185 * @param ul 186 * 187 * @return 188 */ 189 public abstract boolean isCDUp(UserLimitVO ul); 190 191 private byte type; 192 193 protected byte getType() { 194 return type; 195 } 196 197 protected void setType(byte type) { 198 this.type = type; 199 } 200 201 private LegionDogzType(int type) { 202 this.type = (byte) type; 203 } 204 205 protected boolean cdUp(LegionDogzType type, UserLimitVO ul) { 206 if (type.getCDTime(ul) > 0) { 207 if (type.getCDTime(ul) - DateUtil.getNowSec() > 0) { 208 return false; 209 } else { 210 type.increateCDTime(ul, 0); 211 } 212 } 213 return true; 214 } 215 }