001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.model;
027:
028: import com.sun.perseus.util.SVGConstants;
029:
030: /**
031: * This simple class represents the concept of time in the
032: * context of SMIL timing.
033: *
034: * <p>A <code>Time</code> can be <code>RESOLVED</code>,
035: * <code>UNRESOLVED</code> or <code>INDEFINITE</code>. When
036: * times are sorted, times are compared according to their type
037: * and value. The predefined <code>UNRESOLVED</code> and
038: * <code>INDEFINITE</code> values are used to characterize
039: * the special values and sorting is done with the
040: * <code>greaterThan</code> method.
041: *
042: * @see <a href="http://www.w3.org/TR/smil20/smil-timing.html">
043: * SMIL 2.0 Timing and Synchronization Module"</a>
044: * @version $Id: Time.java,v 1.2 2006/04/21 06:39:16 st125089 Exp $
045: */
046: public final class Time {
047: /**
048: * Un-mutable Time instance used to represent the 'indefinite'
049: * time.
050: */
051: public static final Time INDEFINITE = new Time(-1);
052:
053: /**
054: * Un-mutable Time instance used to represent the 'unresolved'
055: * time.
056: */
057: public static final Time UNRESOLVED = new Time(-2);
058:
059: /**
060: * This Time's value.
061: */
062: long value;
063:
064: /**
065: * Creates a new <code>RESOLVED</code> time with the given value.
066: *
067: * @param value the new time's value.
068: */
069: public Time(final long value) {
070: this .value = value;
071: }
072:
073: /**
074: * Compares the input time with this instance.
075: * The <code>UNRESOLVED</code> value is greater than any
076: * other <code>Time</code> value. The <code>INDEFINITE</code>
077: * value is greater than any resolved value.
078: * A resolved time is greater than another resolved time
079: * if its <code>value</code> is greater.
080: *
081: * @param cmp the time to compare with this instance.
082: * @return true if this <code>Time</code> instance is
083: * greater than cmp.
084: * @throws NullPointerException if cmp is null.
085: */
086: public boolean greaterThan(final Time cmp) {
087: if (this == UNRESOLVED) {
088: return true;
089: } else if (this == INDEFINITE) {
090: if (cmp == UNRESOLVED) {
091: return false;
092: }
093: return true;
094: } else {
095: if (cmp == UNRESOLVED || cmp == INDEFINITE) {
096: return false;
097: }
098: return value >= cmp.value;
099: }
100: }
101:
102: /**
103: * @return true if this time is resolved, i.e., if it is neither
104: * equal to UNRESOLVED nor equal to INDEFINITE
105: */
106: public boolean isResolved() {
107: return this != INDEFINITE && this != UNRESOLVED;
108: }
109:
110: /**
111: * Checks if the input time is the same as this one.
112: *
113: * @param cmp the object to compare to.
114: * @return true if cmp is the same object as this one or if cmp
115: * has the same value as tis object.
116: */
117: public boolean isSameTime(final Time cmp) {
118: if (cmp == null) {
119: return false;
120: }
121:
122: if (cmp == this ) {
123: return true;
124: }
125:
126: if (cmp.value == value) {
127: return true;
128: }
129:
130: return false;
131: }
132:
133: /**
134: * Debug
135: * @return a string describing this TimeInterval
136: */
137: public String toString() {
138: if (this == UNRESOLVED) {
139: return "Time[UNRESOLVED]";
140: } else if (this == INDEFINITE) {
141: return "Time[INDEFINITE]";
142: } else {
143: return "Time[RESOLVED, " + value + "]";
144: }
145: }
146:
147: /**
148: * Converst a Time instance to a String trait value.
149: *
150: * @param t the time instance to convert. If null, the value
151: * 'indefinite' is returned.
152: * @return a string trait value.
153: */
154: protected static String toStringTrait(final Time t) {
155: if (t == null || Time.INDEFINITE == t) {
156: return SVGConstants.SVG_INDEFINITE_VALUE;
157: }
158:
159: // This should never happen because Times converted to traits
160: // are times specified on timed element attributes and should
161: // never be unresolved times.
162: if (Time.UNRESOLVED.isSameTime(t)) {
163: throw new IllegalArgumentException();
164: }
165:
166: // At this point, we know we are dealing with a resolved time.
167: // Returns the value in seconds.
168: return (t.value / 1000f) + "s";
169: }
170:
171: }
|