You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jaxme-dev@ws.apache.org by jo...@apache.org on 2004/01/24 23:16:10 UTC

cvs commit: ws-jaxme/src/jaxme/org/apache/ws/jaxme/util Duration.java

jochen      2004/01/24 14:16:10

  Modified:    src/jaxme/org/apache/ws/jaxme/generator/types
                        DurationSG.java
               src/jaxme/org/apache/ws/jaxme/util Duration.java
  Log:
  Seconds in Duration are now splitted into an integer part (seconds) and a long part (millis)
  
  Revision  Changes    Path
  1.2       +1 -18     ws-jaxme/src/jaxme/org/apache/ws/jaxme/generator/types/DurationSG.java
  
  Index: DurationSG.java
  ===================================================================
  RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/generator/types/DurationSG.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DurationSG.java	23 Sep 2003 12:34:47 -0000	1.1
  +++ DurationSG.java	24 Jan 2004 22:16:10 -0000	1.2
  @@ -48,8 +48,6 @@
    */
   package org.apache.ws.jaxme.generator.types;
   
  -import java.text.ParseException;
  -
   import org.apache.ws.jaxme.generator.sg.SGFactory;
   import org.apache.ws.jaxme.generator.sg.SGlet;
   import org.apache.ws.jaxme.generator.sg.SchemaSG;
  @@ -80,22 +78,7 @@
     public JavaQName getRuntimeType(SimpleTypeSG pController) { return DURATION_TYPE; }
   
     public Object getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, Object pData) {
  -    DirectAccessible v;
  -    if (pValue instanceof DirectAccessible) {
  -      v = (DirectAccessible) pValue;
  -    } else {
  -      LocalJavaField f = pMethod.newJavaField(String.class);
  -      f.addLine(pValue);
  -      v = f;
  -    }
  -    LocalJavaField f = pMethod.newJavaField(DURATION_TYPE);
  -    pMethod.addTry();
  -    pMethod.addLine(f, " = ", DURATION_TYPE, ".valueOf(", pValue, ");");
  -    pMethod.addCatch(ParseException.class);
  -    pMethod.addThrowNew(IllegalArgumentException.class, v);
  -    pMethod.addEndTry();
  -    
  -    return f;
  +      return new Object[]{DURATION_TYPE, ".valueOf(", pValue, ")"};
     }
   
     public Object getCastFromString(SimpleTypeSG pController, String pValue) throws SAXException {
  
  
  
  1.2       +273 -101  ws-jaxme/src/jaxme/org/apache/ws/jaxme/util/Duration.java
  
  Index: Duration.java
  ===================================================================
  RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/util/Duration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Duration.java	23 Sep 2003 12:34:50 -0000	1.1
  +++ Duration.java	24 Jan 2004 22:16:10 -0000	1.2
  @@ -49,9 +49,6 @@
   package org.apache.ws.jaxme.util;
   
   import java.io.Serializable;
  -import java.text.MessageFormat;
  -import java.text.NumberFormat;
  -import java.text.ParseException;
   
   
   /** <p>Implementation of xs:duration.</p>
  @@ -59,103 +56,278 @@
    * @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
    */
   public class Duration implements Serializable, Comparable {
  -  private final int years, months, days, hours, minutes;
  -  private final double seconds;
  -
  -  public Duration(int pYears, int pMonths, int pDays, int pHours, int pMinutes, double pSeconds) {
  -    years = pYears;
  -    months = pMonths;
  -    days = pDays;
  -    hours = pHours;
  -    minutes = pMinutes;
  -    seconds = pSeconds;
  -  }
  +    private final boolean isNegative;
  +    private final int years, months, days, hours, minutes, seconds;
  +    private final long millis;
  +
  +    public Duration(boolean pNegative, int pYears, int pMonths, int pDays, int pHours, int pMinutes, int pSeconds, long pMillis) {
  +        isNegative = pNegative;
  +        years = pYears;
  +        months = pMonths;
  +        days = pDays;
  +        hours = pHours;
  +        minutes = pMinutes;
  +        seconds = pSeconds;
  +        millis = pMillis;
  +    }
     
  -  /** <p>Returns the number of years.</p>
  -   */
  -  public int getYears() {
  -    return years;
  -  }
  -
  -  /** <p>Returns the number of months.</p>
  -   */
  -  public int getMonths() {
  -    return months;
  -  }
  -
  -  /** <p>Returns the number of days.</p>
  -   */
  -  public int getDays() {
  -    return days;
  -  }
  -
  -  /** <p>Returns the number of hours.</p>
  -   */
  -  public int getHours() {
  -    return hours;
  -  }
  -
  -  /** <p>Returns the number of minutes.</p>
  -   */
  -  public int getMinutes() {
  -    return minutes;
  -  }
  -
  -  /** <p>Returns the number of seconds.</p>
  -   */
  -  public double getSeconds() {
  -    return seconds;
  -  }
  -  /** <p>Returns a string representation of this Duration.</p>
  -   */
  -  public String toString() {
  -    return "P" + getYears() + "Y" + getMonths() + "M" + getDays() + "DT" +
  -      getHours() + "H" + getMinutes() + "M" +
  -      NumberFormat.getInstance().format(getSeconds()) + "S";
  -  }
  -
  -  /** <p>Converts the given String representation into an instance of
  -   * Duration.</p>
  -   * @throws ParseException The String could not be parsed.
  -   */
  -  public static Duration valueOf(String pValue) throws ParseException {
  -    MessageFormat format = new MessageFormat("P{0,number,integer}Y{1,number,integer}M{2,number,integer}DT{3,number,integer}H{4,number,integer}M{5,number}S");
  -    Object[] numbers = format.parse(pValue);
  -    return new Duration(((Number) numbers[0]).intValue(),
  -                         ((Number) numbers[1]).intValue(),
  -                         ((Number) numbers[2]).intValue(),
  -                         ((Number) numbers[3]).intValue(),
  -                         ((Number) numbers[4]).intValue(),
  -                         ((Number) numbers[5]).doubleValue());
  -  }
  -
  -  public boolean equals(Object o) {
  -    if (o == null  ||  !(o instanceof Duration)) {
  -      return false;
  -    }
  -    return compareTo((Duration) o) == 0;
  -  }
  -
  -  public int compareTo(Object o) {
  -    return compareTo((Duration) o);
  -  }
  -
  -  public int compareTo(Duration d) {
  -    if (years != d.years) { return years - d.years; }
  -    if (months != d.months) { return months - d.months; }
  -    if (days != d.days) { return days - d.days; }
  -    if (hours != d.hours) { return hours - d.hours; }
  -    if (minutes != d.minutes) { return minutes - d.minutes; }
  -    if (seconds > d.seconds) {
  -      return 1;
  -    } else if (seconds < d.seconds) {
  -      return -1;
  -    } else {
  -      return 0;
  -    }
  -  }
  -
  -  public int hashCode() {
  -    return years + months + days + hours + minutes + (int) seconds;
  -  }
  +    /** <p>Returns the number of years.</p>
  +     */
  +    public int getYears() {
  +        return years;
  +    }
  +
  +    /** <p>Returns the number of months.</p>
  +     */
  +    public int getMonths() {
  +        return months;
  +    }
  +
  +    /** <p>Returns the number of days.</p>
  +     */
  +    public int getDays() {
  +        return days;
  +    }
  +
  +    /** <p>Returns the number of hours.</p>
  +     */
  +    public int getHours() {
  +        return hours;
  +    }
  +
  +    /** <p>Returns the number of minutes.</p>
  +     */
  +    public int getMinutes() {
  +        return minutes;
  +    }
  +
  +    /** <p>Returns the number of seconds.</p>
  +     */
  +    public int getSeconds() {
  +        return seconds;
  +    }
  +
  +    /** <p>Returns the number of milliseconds.</p>
  +     */
  +    public long getMillis() {
  +        return millis;
  +    }
  +
  +    /** <p>Returns a string representation of this Duration.</p>
  +     */
  +    public String toString() {
  +        StringBuffer sb = new StringBuffer();
  +        sb.append('P');
  +        sb.append(getYears());
  +        sb.append('Y');
  +        sb.append(getMonths());
  +        sb.append('M');
  +        sb.append(getDays());
  +        sb.append("DT");
  +        sb.append(getHours());
  +        sb.append('H');
  +        sb.append(getMinutes());
  +        sb.append('M');
  +        sb.append(getSeconds());
  +        long m = getMillis();
  +        if (m != 0) {
  +            sb.append('.');
  +            sb.append(m);
  +        }
  +        sb.append('S');
  +        return sb.toString();
  +    }
  +
  +    /** <p>Converts the given String representation into an instance of
  +     * Duration.</p>
  +     * @throws IllegalArgumentException The String could not be parsed.
  +     */
  +    public static Duration valueOf(String pValue) {
  +        if (pValue == null) {
  +            throw new NullPointerException("The duration value must not be null.");
  +        }
  +        int len = pValue.length();
  +        int offset = 0;
  +        boolean isNegative;
  +        if (len > 0) {
  +            char c = pValue.charAt(0);
  +            if (c == '-') {
  +                isNegative = true;
  +                ++offset;
  +            } else if (c == '+') {
  +                isNegative = false;
  +                ++offset;
  +            } else {
  +                isNegative = false;
  +            }
  +        } else {
  +            throw new IllegalArgumentException("Invalid duration: Empty string");
  +        }
  +        
  +        if (len == 0  ||  pValue.charAt(offset) != 'P') {
  +            throw new IllegalArgumentException("Invalid duration: " + pValue + " (must start with P, +P, or -P)");
  +        } else {
  +            ++offset;
  +        }
  +        
  +        int years = -1, months = -1, daysOfMonth = -1, hours = -1, minutes = -1, seconds = -1;
  +        long millis = -1;
  +        int preDecimalPoint = -1;
  +        boolean separatorSeen = false;
  +        StringBuffer digits = new StringBuffer();
  +        while (offset < len) {
  +            char c = pValue.charAt(offset);
  +            if (Character.isDigit(c)) {
  +                digits.append(c);
  +            } else if (c == 'T') {
  +                if (separatorSeen) {
  +                    throw new IllegalArgumentException("Invalid duration: " + pValue
  +                            + " (date/time separator 'T' used twice)");
  +                } else {
  +                    separatorSeen = true;
  +                }
  +            } else {
  +                long l;
  +                if (digits.length() == 0) {
  +                    l = 0;
  +                } else {
  +                    try {
  +                        l = Long.parseLong(digits.toString());
  +                    } catch (NumberFormatException e) {
  +                        throw new IllegalArgumentException("Invalid duration: "  + pValue
  +                                + " (max long value exceeded by " + digits + ")");
  +                    }
  +                    digits.setLength(0);
  +                }
  +                if (preDecimalPoint >= 0) {
  +                    if (c == 'S') {
  +                        if (!separatorSeen) {
  +                            throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                    + "(seconds specified before date/time separator 'T' seen)");
  +                        }
  +                        if (seconds != -1) {
  +                            throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                    + " (seconds specified twice)");
  +                        }
  +                        seconds = preDecimalPoint;
  +                        millis = l;
  +                        preDecimalPoint = -1;
  +                    } else {
  +                        throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                + " (decimal point not allowed here: "
  +                                + preDecimalPoint + "." + digits + c + ")");
  +                    }
  +                } else if (l > Integer.MAX_VALUE) {
  +                    throw new IllegalArgumentException("Invalid duration: " + pValue
  +                            + " (max integer value exceeded by " + digits + ")");
  +                } else {
  +                    int i = (int) l;
  +                    if (c == '.') {
  +                        preDecimalPoint = i;
  +                    } else if (separatorSeen) {
  +                        if (c == 'Y'  ||  c == 'D') {
  +                            throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                    + " (years or days of month specified after date/time separator 'T' seen)");
  +                        } else if (c == 'S') {
  +                            if (seconds != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (seconds specified twice)");
  +                            }
  +                            seconds = i;
  +                            millis = 0;
  +                        } else if (c == 'M') {
  +                            if (minutes != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (minutes specified twice)");
  +                            } else if (seconds != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (minutes specified after seconds)");
  +                            }
  +                            minutes = i;
  +                        } else if (c == 'H') {
  +                            if (hours != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (hours specified twice)");
  +                           } else if (minutes != -1) {
  +                               throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                       + " (hours specified after minutes)");
  +                           } else if (seconds != -1) {
  +                               throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                       + " (seconds specified after minutes)");
  +                           }
  +                           hours = i;
  +                        }
  +                    } else {
  +                        if (c == 'H'  ||  c == 'S') {
  +                            throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                    + " (hours or seconds specified before date/time separator 'T' seen)");
  +                        } else if (c == 'Y') {
  +                            if (years != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (years specified twice)");
  +                            } else if (months != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (years specified after months)");
  +                            } else if (daysOfMonth != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (years specified after days of month)");
  +                            }
  +                            years = i;
  +                        } else if (c == 'M') {
  +                            if (months != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (months specified twice)");
  +                            } else if (daysOfMonth != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                        + " (days of month specified after months)");
  +                            }
  +                            months = i;
  +                        } else if (c == 'D') {
  +                            if (daysOfMonth != -1) {
  +                                throw new IllegalArgumentException("Invalid duration: " + pValue
  +                                                                   + " (days of month specified twice)");
  +                            }
  +                            daysOfMonth = i;
  +                        }
  +                    }
  +                }
  +            }
  +            ++offset;
  +        }
  +        return new Duration(isNegative, years, months, daysOfMonth, hours, minutes, seconds, millis);
  +    }
  +
  +    public boolean equals(Object o) {
  +        if (o == null  ||  !(o instanceof Duration)) {
  +            return false;
  +        }
  +        return compareTo((Duration) o) == 0;
  +    }
  +
  +    public int compareTo(Object o) {
  +        return compareTo((Duration) o);
  +    }
  +
  +    public int compareTo(Duration d) {
  +        if (isNegative != d.isNegative) {
  +            return isNegative ? -1 : 1;
  +        }
  +        if (years != d.years) { return years - d.years; }
  +        if (months != d.months) { return months - d.months; }
  +        if (days != d.days) { return days - d.days; }
  +        if (hours != d.hours) { return hours - d.hours; }
  +        if (minutes != d.minutes) { return minutes - d.minutes; }
  +        if (seconds != d.seconds) { return seconds - d.seconds; }
  +        if (millis > d.millis) {
  +            return 1;
  +        } else if (millis < d.millis) {
  +            return -1;
  +        } else {
  +            return 0;
  +        }
  +    }
  +
  +    public int hashCode() {
  +        return isNegative ? 1 : 0 + years + months + days + hours + minutes + seconds + (int) millis;
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org