1 28 29 package com.idaremedia.antx.helpers; 30 31 import org.apache.tools.ant.types.EnumeratedAttribute; 32 33 42 43 public final class Setting extends EnumeratedAttribute 44 { 45 46 public static final int OFF_INDEX= 0; 47 48 public static final int ON_INDEX= 1; 49 50 public static final int DEFAULT_INDEX= 2; 51 52 public static final int INHERITED_INDEX= 3; 53 54 public static final int UNDEFINED_INDEX= 4; 55 56 57 public static final Setting OFF = new Setting("off",OFF_INDEX); 58 59 public static final Setting ON = new Setting("on",ON_INDEX); 60 61 public static final Setting DEFAULT = new Setting("default",DEFAULT_INDEX); 62 63 public static final Setting INHERITED = new Setting("inherited",INHERITED_INDEX); 64 65 public static final Setting UNDEFINED = new Setting("undefined",UNDEFINED_INDEX); 66 67 68 69 72 public Setting() 73 { 74 super(); 75 } 76 77 78 82 private Setting(String v, int i) 83 { 84 super(); 85 Tk.initEnum(this,v); 86 this.index = getIndex(); } 88 89 90 95 public String [] getValues() 96 { 97 return new String [] {"off","on","default","inherited","undefined"}; 98 }; 99 100 101 102 106 public static Setting from(int i) 107 { 108 if (i==ON.index) { return ON; } 109 if (i==OFF.index) { return OFF; } 110 if (i==DEFAULT.index) { return DEFAULT; } 111 if (i==INHERITED.index) { return INHERITED; } 112 if (i==UNDEFINED.index) { return UNDEFINED; } 113 return null; 114 } 115 116 117 124 public static Setting from(int i, Setting dflt) 125 { 126 Setting st= from(i); 127 return (st==null) ? dflt : st; 128 } 129 130 131 138 public static Setting from(String s) 139 { 140 if (s!=null && s.length()>1) { 141 s = s.toLowerCase(); 142 if (Character.isDigit(s.charAt(0))) { 143 try { return from(Integer.parseInt(s)); } 144 catch(Exception nfx) {} 145 } else { 146 if ("yes".equals(s)) { return ON; } 147 if ("true".equals(s)) { return ON; } 148 if (ON.value.equals(s)) { return ON; } 149 150 if ("no".equals(s)) { return OFF; } 151 if ("false".equals(s)) { return OFF; } 152 if (OFF.value.equals(s)) { return OFF; } 153 154 if (Strings.INHERIT.equals(s)) { return INHERITED; } 155 if (INHERITED.value.equals(s)) { return INHERITED; } 156 157 if (DEFAULT.value.equals(s)) { return DEFAULT; } 158 if (UNDEFINED.value.equals(s)) { return UNDEFINED; } 159 } 160 } 161 return null; 162 } 163 164 165 172 public static Setting from(String s, Setting dflt) 173 { 174 Setting st= from(s); 175 return (st==null) ? dflt : st; 176 } 177 178 179 183 public boolean equals(Object o) 184 { 185 if (o==this) { return true; } 186 if (o==null) { return false; } 187 if (o.getClass()==getClass()) { 188 return ((Setting)o).getIndex()==this.getIndex(); 189 } 190 return false; 191 } 192 193 194 195 199 public int hashCode() 200 { 201 return this.value.hashCode(); 202 } 203 204 private int index; 205 } 206 207 208 | Popular Tags |