1 19 package org.openharmonise.rm.resources.metadata.properties.ranges; 20 21 import java.util.StringTokenizer ; 22 23 import org.openharmonise.rm.PopulateException; 24 import org.openharmonise.rm.publishing.State; 25 import org.w3c.dom.Element ; 26 27 28 36 public class FloatRange extends NumberRange { 37 38 41 public final static String TAG_FLOAT_RANGE = "FloatRange"; 42 43 46 private float m_fMax = -1; 47 48 51 private float m_fMin = -1; 52 53 57 static final private String SEPARATOR = ":"; 58 59 62 public FloatRange() { 63 super(Float .class.getName()); 64 } 65 66 69 public boolean equals(Object obj) { 70 boolean bResult = false; 71 72 if (obj instanceof NumberRange) { 73 if (super.equals(obj) == true) { 74 NumberRange nRange = (NumberRange) obj; 75 76 if (hasMin() == nRange.hasMin() 77 && hasMax() == nRange.hasMax()) { 78 bResult = true; 79 } 80 } 81 } 82 83 return bResult; 84 } 85 86 89 public boolean isValid(Object obj) { 90 boolean bIsValid = false; 91 92 if (obj instanceof Float ) { 93 bIsValid = true; 94 float fVal = ((Float ) obj).floatValue(); 95 96 if (hasMin() == true) { 97 float fMin = m_fMin; 98 99 if (m_bMinExclusive == true) { 100 fMin = fMin + 1; 101 } 102 103 if (fVal <= fMin) { 104 bIsValid = false; 105 } 106 } 107 108 if (hasMax() == true && bIsValid == true) { 109 float fMax = m_fMax; 110 111 if (m_bMinExclusive == true) { 112 fMax = fMax - 1; 113 } 114 115 if (fVal >= fMax) { 116 bIsValid = false; 117 } 118 } 119 } 120 121 return bIsValid; 122 } 123 124 131 public void setMax(float fMax, boolean bIsExclusive) { 132 m_fMax = fMax; 133 m_bMaxExclusive = bIsExclusive; 134 hasMax(true); 135 isChanged(true); 136 } 137 138 145 public void setMin(float fMin, boolean bIsExclusive) { 146 m_fMin = fMin; 147 m_bMinExclusive = bIsExclusive; 148 hasMin(true); 149 isChanged(true); 150 } 151 152 157 public float getMin() { 158 return m_fMin; 159 } 160 161 162 167 public float getMax() { 168 return m_fMax; 169 } 170 171 174 public String getDetails() { 175 176 if (isChanged()) { 177 StringBuffer strbuf = new StringBuffer (); 178 179 if (hasMin() == true) { 180 strbuf.append(m_fMin).append(SEPARATOR).append( 181 this.m_bMinExclusive); 182 } else { 183 strbuf.append(SEPARATOR); 184 } 185 strbuf.append(SEPARATOR); 186 if (hasMax() == true) { 187 strbuf.append(m_fMax).append(SEPARATOR).append( 188 this.m_bMaxExclusive); 189 } else { 190 strbuf.append(SEPARATOR); 191 } 192 super.setDetails(strbuf.toString()); 193 } 194 195 return super.getDetails(); 196 } 197 198 201 public void setDetails(String sDetails) { 202 if (sDetails != null) { 203 StringTokenizer tokenizer = 204 new StringTokenizer (sDetails, SEPARATOR); 205 206 int i = 0; 207 float fTmp = 0; 208 while (tokenizer.hasMoreTokens()) { 209 String token = tokenizer.nextToken(); 210 211 if (token != null && token.length() > 0) { 212 if (i == 0 || i == 2) { 213 fTmp = Float.parseFloat(token); 214 215 if (i == 0) { 216 m_fMin = fTmp; 217 hasMin(true); 218 219 } else if (i == 2) { 220 m_fMax = fTmp; 221 hasMax(true); 222 } 223 } else { 224 if (i == 1) { 225 m_bMinExclusive = Boolean.getBoolean(token); 226 } else if (i == 3) { 227 m_bMaxExclusive = Boolean.getBoolean(token); 228 } 229 } 230 231 } 232 i++; 233 } 234 } 235 236 super.setDetails(sDetails); 237 } 238 239 242 public String getTagName() { 243 return TAG_FLOAT_RANGE; 244 } 245 246 249 public void populate(Element xmlElement, State state) 250 throws PopulateException { 251 252 String sTagname = xmlElement.getTagName(); 253 254 if(sTagname.equals(TAG_MAX)) { 255 setMax(getChildFloatValue(xmlElement),isExclusive(xmlElement)); 256 } else if (sTagname.equals(TAG_MIN)) { 257 setMin(getChildFloatValue(xmlElement),isExclusive(xmlElement)); 258 } else { 259 super.populate(xmlElement, state); 260 } 261 262 } 263 264 271 private float getChildFloatValue(Element el) { 272 float fVal = 0; 273 274 String sVal = el.getFirstChild().getNodeValue(); 275 276 if(sVal != null) { 277 fVal = Float.parseFloat(sVal); 278 } 279 280 return fVal; 281 } 282 283 } 284 | Popular Tags |