Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Time.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Time.java
  3. * $Id$
  4. *
  5. *
  6. * Copyright 1999-2003 The Apache Software Foundation.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. *
  21. * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  22. * @version $Revision$ $Name$
  23. */
  24. package org.apache.fop.datatypes;
  25. import org.apache.fop.fo.expr.PropertyException;
  26. import org.apache.fop.fo.properties.Property;
  27. public class Time extends AbstractPropertyValue {
  28. private static final String tag = "$Name$";
  29. private static final String revision = "$Revision$";
  30. /*
  31. * Constant for Unit name
  32. */
  33. public static final int
  34. NOUNIT = 0
  35. ,MSEC = 1
  36. ,SEC = 2
  37. ;
  38. /**
  39. * Array of constant conversion factors from unit to milliseconds,
  40. * indexed by integer unit constant. Keep this array in sync with
  41. * the integer constants or bear the consequences.
  42. */
  43. public static final double[] msPerUnit = {
  44. 0.0
  45. ,1.0
  46. ,1000.0
  47. };
  48. private double time = 0.0;
  49. private int units = 0;
  50. /**
  51. * @param property the <tt>int</tt> index of the property on which
  52. * this value is being defined.
  53. * @param unit the <tt>int</tt> unit name code, as defined in the
  54. * constants of this class.
  55. * @param value the <tt>double</tt> value.
  56. * @exception PropertyException
  57. */
  58. public Time(int property, int unit, double value)
  59. throws PropertyException
  60. {
  61. super(property, PropertyValue.TIME);
  62. units = unit;
  63. time = value * msPerUnit[unit];
  64. }
  65. /**
  66. * @param propertyName the <tt>String</tt< name of the property on which
  67. * this value is being defined.
  68. * @param unit the <tt>int</tt> unit name code, as defined in the
  69. * constants of this class.
  70. * @param value the <tt>double</tt> value.
  71. * @exception PropertyException
  72. */
  73. public Time(String propertyName, int unit, double value)
  74. throws PropertyException
  75. {
  76. super(propertyName, PropertyValue.TIME);
  77. units = unit;
  78. time = value * msPerUnit[unit];
  79. }
  80. /**
  81. * @return <tt>double</tt> time in millisecs
  82. */
  83. public double getTime() {
  84. return time;
  85. }
  86. /**
  87. * @param unit type of unit as per constants defined in this class
  88. * @return <tt>double</tt> time value
  89. */
  90. public double getTime(int unit) {
  91. return time / msPerUnit[unit];
  92. }
  93. /**
  94. * @param unit type of unit as per constants defined in this class
  95. * @param value time in specified units
  96. */
  97. public void setTime(int unit, double value) {
  98. units = unit;
  99. time = value * msPerUnit[unit];
  100. }
  101. /**
  102. * @param time in millisecs
  103. */
  104. public void setTime(double time) {
  105. units = MSEC;
  106. this.time = time;
  107. }
  108. /**
  109. * @param unit an <tt>int</tt> encoding a <i>Time</i> unit.
  110. * @return the <tt>String</tt> name of the unit.
  111. */
  112. public static String getUnitName(int unit) {
  113. switch (unit) {
  114. case MSEC:
  115. return "ms";
  116. case SEC:
  117. return "s";
  118. default:
  119. return "";
  120. }
  121. }
  122. /**
  123. * validate the <i>Time</i> against the associated property.
  124. */
  125. public void validate() throws PropertyException {
  126. super.validate(Property.TIME);
  127. }
  128. public String toString() {
  129. return "" + time + getUnitName(units) + "\n" + super.toString();
  130. }
  131. }