Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 195   Methods: 12
NCLOC: 166   Classes: 1
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
DurationData.java 63,9% 78,8% 58,3% 73,5%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 package org.jbind.xml.instance.data;
 11   
 
 12   
 import java.math.BigDecimal;
 13   
 
 14   
 import org.jbind.util.other.StrBuffer;
 15   
 import org.jbind.xml.core.data.CompareResult;
 16   
 import org.jbind.xml.core.data.IAnyTypeData;
 17   
 import org.jbind.xml.core.data.IDuration;
 18   
 import org.jbind.xml.core.data.IDurationData;
 19   
 import org.jbind.xml.core.data.IHasOrder;
 20   
 import org.jbind.xml.msg.IConstraintViolations;
 21   
 import org.jbind.xml.msg.XmlException;
 22   
 import org.jbind.xml.msg.XmlMessages;
 23   
 
 24   
 public class DurationData extends AbstractDateOrTimeData implements IDurationData {
 25   
 
 26   
   private IDuration myValue = null;
 27   
 
 28  8
   public DurationData() {}
 29   
 
 30  8
   protected void doAccept(String aString) throws XmlException {
 31  8
     super.doAccept(aString);
 32  8
     int idx = 0;
 33  8
     ensureChar(aString, idx);
 34  8
     int sign = 1;
 35  8
     if (aString.charAt(0) == '-') {
 36  0
       sign = -1;
 37  0
       idx++;
 38   
     }
 39  8
     parseChar(aString, idx, 'P');
 40  8
     idx++;
 41  8
     int years = 0;
 42  8
     int months = 0;
 43  8
     int days = 0;
 44  8
     int hours = 0;
 45  8
     int minutes = 0;
 46  8
     int seconds = 0;
 47  8
     BigDecimal fraction = null;
 48   
 
 49  8
     boolean parsedTimeDesignator = false;
 50  8
     boolean parsedItem = false;
 51   
 
 52  8
     for (StrBuffer symbols = new StrBuffer("YMDTHMS");
 53  44
             (symbols.length() > 0) && (idx < aString.length()); ) {
 54  36
       int symbolIdx = symbolIdx(aString, idx);
 55  36
       if (symbolIdx < 0) {
 56  0
         throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
 57   
       }
 58  36
       char symbol = aString.charAt(symbolIdx);
 59  36
       if (!validSymbol(symbol, symbols)) {
 60  0
         throw new XmlException(XmlMessages.invalidDurationDesignator(aString, new Integer(symbolIdx), new Character(symbol), getImpl_()));
 61   
       }
 62  36
       switch (symbol) {
 63   
         case 'Y':
 64  4
           years = parseInt(aString, idx);
 65  4
           idx = advanceInt(aString, idx);
 66  4
           parsedItem = true;
 67  4
           break;
 68   
         case 'M':
 69  9
           if (parsedTimeDesignator) {
 70  4
             minutes = parseInt(aString, idx);
 71  4
             idx = advanceInt(aString, idx);
 72  4
             parsedItem = true;
 73   
           } else {
 74  5
             months = parseInt(aString, idx);
 75  5
             idx = advanceInt(aString, idx);
 76  5
             parsedItem = true;
 77   
           }
 78  9
           break;
 79   
         case 'D':
 80  5
           days = parseInt(aString, idx);
 81  5
           idx = advanceInt(aString, idx);
 82  5
           parsedItem = true;
 83  5
           break;
 84   
         case 'T':
 85  7
           if (idx != symbolIdx) {
 86  0
             throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
 87   
           }
 88  7
           parsedTimeDesignator = true;
 89  7
           break;
 90   
         case 'H':
 91  4
           if (!parsedTimeDesignator) {
 92  0
             throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
 93   
           }
 94  4
           hours = parseInt(aString, idx);
 95  4
           idx = advanceInt(aString, idx);
 96  4
           parsedItem = true;
 97  4
           break;
 98   
         case 'S':
 99  7
           if (!parsedTimeDesignator) {
 100  0
             throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
 101   
           }
 102  7
           seconds = parseInt(aString, idx);
 103  7
           idx = advanceInt(aString, idx);
 104  7
           parsedItem = true;
 105  7
           if ((idx < aString.length()) && (aString.charAt(idx) == '.')) {
 106  4
             int begin = idx;
 107  4
             idx++;
 108  4
             while ((idx < aString.length()) && Character.isDigit(aString.charAt(idx))) {
 109  13
               idx++;
 110   
             }
 111  4
             if (begin == idx - 1) {
 112  0
               throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
 113   
             }
 114  4
             fraction = new BigDecimal("0" + aString.substring(begin, idx));
 115   
           }
 116  7
           break;
 117   
         default:
 118   
           assert false;
 119   
       }
 120  36
       idx++;// advance over symbol
 121   
     }
 122  8
     if (!parsedItem) {
 123  0
       throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
 124   
     }
 125  8
     if (idx < aString.length()) {
 126  0
       throw new XmlException(XmlMessages.unexpectedCharactersAfterDuration(aString, getImpl_()));
 127   
     }
 128  8
     myValue = new Duration(sign, years, months, days, hours, minutes, seconds, fraction);
 129   
   }
 130   
 
 131  36
   private boolean validSymbol(char aSymbol, StrBuffer aSymbols) {
 132  36
     while (true) {
 133  51
       char c = aSymbols.charAt(0);
 134  51
       aSymbols.replace(0, 1, "");
 135  51
       if (c == aSymbol) {
 136  36
         return true;
 137   
       }
 138  15
       if (aSymbols.length() == 0) {
 139  0
         return false;
 140   
       }
 141   
     }
 142   
   }
 143   
 
 144  36
   private int symbolIdx(String aString, int anIndex) {
 145  36
     int res = -1;
 146  87
     for (int i = anIndex; i < aString.length(); i++) {
 147  87
       char c = aString.charAt(i);
 148  87
       if (!Character.isDigit(c) && (c != '.')) {
 149  36
         res = i;
 150  36
         break;
 151   
       }
 152   
     }
 153  36
     return res;
 154   
   }
 155   
 
 156  0
   public String getCanonicalForm_() {
 157  0
     return String.valueOf(myValue);
 158   
   }
 159   
 
 160  5
   public IDuration getDuration() {
 161  5
     return myValue;
 162   
   }
 163   
 
 164  0
   public void setDuration(IDuration aNewValue) throws XmlException {
 165  0
     IDuration oldValue = myValue;
 166  0
     myValue = aNewValue;
 167  0
     IConstraintViolations violations = completeSimpleStorageAssignment_();
 168  0
     if (!violations.isEmpty()) {
 169  0
       myValue = oldValue;
 170  0
       throw new XmlException(violations);
 171   
     }
 172   
   }
 173   
 
 174  4
   public boolean simpleStorageValueEquals(IAnyTypeData aData) {
 175  4
     return (aData instanceof IDurationData) && myValue.equals(((IDurationData)aData).getDuration());
 176   
   }
 177   
 
 178  7
   public int simpleStorageValueHashCode() {
 179  7
     return myValue.hashCode();
 180   
   }
 181   
 
 182  0
   public Object getSimpleStorageObject() {
 183  0
     return myValue;
 184   
   }
 185   
 
 186  0
   public void setSimpleStorageObject(Object aValue) {
 187  0
     myValue = (IDuration)aValue;
 188   
   }
 189   
 
 190  0
   protected CompareResult doGetCompareResult(IHasOrder aData) {
 191  0
     return myValue.compareTo(((IDurationData)aData).getDuration());
 192   
   }
 193   
 
 194   
 }
 195