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 IntegerRange extends NumberRange { 37 38 41 public final static String TAG_INTEGER_RANGE = "IntegerRange"; 42 43 46 private int m_nMax = -1; 47 48 51 private int m_nMin = -1; 52 53 57 static final private String SEPARATOR = ":"; 58 59 62 public IntegerRange() { 63 super(Integer .class.getName()); 64 } 65 66 69 public boolean isValid(Object obj) { 70 boolean bIsValid = false; 71 72 if (obj instanceof Integer ) { 73 bIsValid = true; 74 int nVal = ((Integer ) obj).intValue(); 75 76 if (hasMin() == true) { 77 int nMin = m_nMin; 78 79 if (m_bMinExclusive == true) { 80 nMin = nMin + 1; 81 } 82 83 if (nVal <= nMin) { 84 bIsValid = false; 85 } 86 } 87 88 if (hasMax() == true && bIsValid == true) { 89 int nMax = m_nMax; 90 91 if (m_bMinExclusive == true) { 92 nMax = nMax - 1; 93 } 94 95 if (nVal >= nMax) { 96 bIsValid = false; 97 } 98 } 99 } 100 101 return bIsValid; 102 } 103 104 111 public void setMax(int nMax, boolean bIsExclusive) { 112 m_nMax = nMax; 113 m_bMaxExclusive = bIsExclusive; 114 hasMax(true); 115 isChanged(true); 116 } 117 118 125 public void setMin(int nMin, boolean bIsExclusive) { 126 m_nMin = nMin; 127 m_bMinExclusive = bIsExclusive; 128 hasMin(true); 129 isChanged(true); 130 } 131 132 137 public int getMin() { 138 return m_nMin; 139 } 140 141 146 public int getMax() { 147 return m_nMax; 148 } 149 150 153 public String getDetails() { 154 155 if (isChanged()) { 156 StringBuffer strbuf = new StringBuffer (); 157 158 if (hasMin() == true) { 159 strbuf.append(m_nMin).append(SEPARATOR).append( 160 this.m_bMinExclusive); 161 } else { 162 strbuf.append(SEPARATOR); 163 } 164 strbuf.append(SEPARATOR); 165 if (hasMax() == true) { 166 strbuf.append(m_nMax).append(SEPARATOR).append( 167 this.m_bMaxExclusive); 168 } else { 169 strbuf.append(SEPARATOR); 170 } 171 super.setDetails(strbuf.toString()); 172 } 173 174 return super.getDetails(); 175 } 176 177 180 public void setDetails(String sDetails) { 181 if (sDetails != null) { 182 StringTokenizer tokenizer = 183 new StringTokenizer (sDetails, SEPARATOR); 184 185 int i = 0; 186 int nTmp = 0; 187 while (tokenizer.hasMoreTokens()) { 188 String token = tokenizer.nextToken(); 189 190 if (token != null && token.length() > 0) { 191 if (i == 0 || i == 2) { 192 nTmp = Integer.parseInt(token); 193 194 if (i == 0) { 195 m_nMin = nTmp; 196 hasMin(true); 197 198 } else if (i == 2) { 199 m_nMax = nTmp; 200 hasMax(true); 201 } 202 } else { 203 if (i == 1) { 204 m_bMinExclusive = Boolean.getBoolean(token); 205 } else if (i == 3) { 206 m_bMaxExclusive = Boolean.getBoolean(token); 207 } 208 } 209 210 } 211 i++; 212 } 213 } 214 215 super.setDetails(sDetails); 216 } 217 218 221 public boolean equals(Object obj) { 222 boolean bResult = false; 223 224 if (obj instanceof IntegerRange) { 225 if (super.equals(obj) == true) { 226 IntegerRange iRange = (IntegerRange) obj; 227 228 if (m_nMin == iRange.getMin() && m_nMax == iRange.getMax()) { 229 bResult = true; 230 } 231 } 232 } 233 234 return bResult; 235 } 236 237 240 public String getTagName() { 241 return TAG_INTEGER_RANGE; 242 } 243 244 247 public void populate(Element xmlElement, State state) 248 throws PopulateException { 249 250 String sTagname = xmlElement.getTagName(); 251 252 if(sTagname.equals(TAG_MAX)) { 253 setMax(getChildIntValue(xmlElement),isExclusive(xmlElement)); 254 } else if (sTagname.equals(TAG_MIN)) { 255 setMin(getChildIntValue(xmlElement),isExclusive(xmlElement)); 256 } else { 257 super.populate(xmlElement, state); 258 } 259 260 } 261 262 269 private int getChildIntValue(Element el) { 270 int nVal = 0; 271 272 String sVal = el.getFirstChild().getNodeValue(); 273 274 if(sVal != null) { 275 nVal = Integer.parseInt(sVal); 276 } 277 278 return nVal; 279 } 280 } 281 | Popular Tags |