⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daytimedurationattribute.java

📁 sunxacml源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @return the long value for that groupNumber
     * @throws NumberFormatException if the string value for that
     * groupNumber is not a valid long
     */
    private static long parseGroup(Matcher matcher, int groupNumber)
        throws NumberFormatException {
        long groupLong = 0;

        if (matcher.start(groupNumber) != -1) {
            String groupString = matcher.group(groupNumber);
            groupLong = Long.parseLong(groupString);
        }
        return groupLong;
    }

    /**
     * Returns a new <code>DayTimeDurationAttribute</code> that represents
     * the xf:dayTimeDuration value indicated by the string provided.
     *
     * @param value a string representing the desired value
     * @return a new <code>DayTimeDurationAttribute</code> representing the
     *         desired value (null if there is a parsing error)
     */
    public static DayTimeDurationAttribute getInstance(String value)
        throws ParsingException, NumberFormatException
    {
        boolean negative = false;
        long days = 0;
        long hours = 0;
        long minutes = 0;
        long seconds = 0;
        int nanoseconds = 0;

        // Compile the pattern, if not already done.
        // No thread-safety problem here. The worst that can
        // happen is that we initialize pattern several times.
        if (pattern == null) {
            try {
                pattern = Pattern.compile(patternString);
            } catch (PatternSyntaxException e) {
                // This should never happen
                throw new ParsingException("unexpected pattern match error");
            }
        }

        // See if the value matches the pattern.
        Matcher matcher = pattern.matcher(value);
        boolean matches = matcher.matches();

        // If not, syntax error!
        if (!matches) {
            throw new ParsingException("Syntax error in dayTimeDuration");
        }

        // If the negative group matched, the value is negative.
        if (matcher.start(GROUP_SIGN) != -1)
            negative = true;

        try {
            // If the days group matched, parse that value.
            days = parseGroup(matcher, GROUP_DAYS);

            // If the hours group matched, parse that value.
            hours = parseGroup(matcher, GROUP_HOURS);

            // If the minutes group matched, parse that value.
            minutes = parseGroup(matcher, GROUP_MINUTES);

            // If the seconds group matched, parse that value.
            seconds = parseGroup(matcher, GROUP_SECONDS);

            // Special handling for fractional seconds, since
            // they can have any resolution.
            if (matcher.start(GROUP_NANOSECONDS) != -1) {
                String nanosecondString = matcher.group(GROUP_NANOSECONDS);

                // If there are less than 9 digits in the fractional seconds,
                // pad with zeros on the right so it's nanoseconds.
                while (nanosecondString.length() < 9)
                    nanosecondString += "0";

                // If there are more than 9 digits in the fractional seconds,
                // drop the least significant digits.
                if (nanosecondString.length() > 9) {
                    nanosecondString = nanosecondString.substring(0, 9);
                }

                nanoseconds = Integer.parseInt(nanosecondString);
            }
        } catch (NumberFormatException e) {
            // If we run into a number that's too big to be a long
            // that's an error. Really, it's a processing error,
            // since one can argue that we should handle that.
            throw e;
        }

        // Here's a requirement that's not checked for in the pattern.
        // The designator 'T' must be absent if all the time
        // items are absent. So the string can't end in 'T'.
        // Note that we don't have to worry about a zero length
        // string, since the pattern won't allow that.
        if (value.charAt(value.length()-1) == 'T')
            throw new ParsingException("'T' must be absent if all" +
                                       "time items are absent");

        // If parsing went OK, create a new DayTimeDurationAttribute object and
        // return it.
        return new DayTimeDurationAttribute(negative, days, hours, minutes,
                                            seconds, nanoseconds);
    }

    /**
     * Returns true if the duration is negative.
     *
     * @return true if the duration is negative, false otherwise
     */
    public boolean isNegative() {
        return negative;
    }

    /**
     * Gets the number of days.
     *
     * @return the number of days
     */
    public long getDays() {
        return days;
    }

    /**
     * Gets the number of hours.
     *
     * @return the number of hours
     */
    public long getHours() {
        return hours;
    }

    /**
     * Gets the number of minutes.
     *
     * @return the number of minutes
     */
    public long getMinutes() {
        return minutes;
    }

    /**
     * Gets the number of seconds.
     *
     * @return the number of seconds
     */
    public long getSeconds() {
        return seconds;
    }

    /**
     * Gets the number of nanoseconds.
     *
     * @return the number of nanoseconds
     */
    public int getNanoseconds() {
        return nanoseconds;
    }

    /**
     * Gets the total number of round seconds (in milliseconds).
     *
     * @return the total number of seconds (in milliseconds)
     */
    public long getTotalSeconds() {
        return totalMillis;
    }

    /**
     * Returns true if the input is an instance of this class and if its
     * value equals the value contained in this class.
     *
     * @param o the object to compare
     *
     * @return true if this object and the input represent the same value
     */
    public boolean equals(Object o) {
        if (! (o instanceof DayTimeDurationAttribute))
            return false;

        DayTimeDurationAttribute other = (DayTimeDurationAttribute)o;

        return ((totalMillis == other.totalMillis) &&
                (nanoseconds == other.nanoseconds) &&
                (negative == other.negative));
    }

    /**
     * Returns the hashcode value used to index and compare this object with
     * others of the same type. Typically this is the hashcode of the backing
     * data object.
     *
     * @return the object's hashcode value
     */
    public int hashCode() {
        // The totalMillis, nanoseconds, and negative fields are all considered
        // by the equals method, so it's best if the hashCode is derived
        // from all of those fields.
        int hashCode = (int) totalMillis ^ (int) (totalMillis >> 32);
        hashCode = 31*hashCode + nanoseconds;
        if (negative)
            hashCode = -hashCode;
        return hashCode;
    }

    /**
     * Converts to a String representation.
     *
     * @return the String representation
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("DayTimeDurationAttribute: [\n");
        sb.append("  Negative: " + negative);
        sb.append("  Days: " + days);
        sb.append("  Hours: " + hours);
        sb.append("  Minutes: " + minutes);
        sb.append("  Seconds: " + seconds);
        sb.append("  Nanoseconds: " + nanoseconds);
        sb.append("  TotalSeconds: " + totalMillis);
        sb.append("]");

        return sb.toString();
    }

    /**
     * Encodes the value in a form suitable for including in XML data like
     * a request or an obligation. This must return a value that could in
     * turn be used by the factory to create a new instance with the same
     * value.
     *
     * @return a <code>String</code> form of the value
     */
    public String encode() {
        if (encodedValue != null)
            return encodedValue;

        // Length is quite variable
        StringBuffer buf = new StringBuffer(10);

        if (negative)
            buf.append('-');
        buf.append('P');
        if (days != 0) {
            buf.append(Long.toString(days));
            buf.append('D');
        }
        if ((hours != 0) || (minutes != 0)
            || (seconds != 0) || (nanoseconds != 0)) {
            // Only include the T if there are some time fields
            buf.append('T');
        } else {
            // Make sure that there's always at least one field specified
            if (days == 0)
                buf.append("0D");
        }
        if (hours != 0) {
            buf.append(Long.toString(hours));
            buf.append('H');
        }
        if (minutes != 0) {
            buf.append(Long.toString(minutes));
            buf.append('M');
        }
        if ((seconds != 0) || (nanoseconds != 0)) {
            buf.append(Long.toString(seconds));
            if (nanoseconds != 0) {
                buf.append('.');
                buf.append(DateAttribute.zeroPadInt(nanoseconds, 9));
            }
            buf.append('S');
        }

        encodedValue = buf.toString();

        return encodedValue;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -