1. /*
  2. * @(#)file SnmpTimeticks.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.11
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. // Copyright (c) 1995-96 by Cisco Systems, Inc.
  12. package com.sun.jmx.snmp;
  13. /**
  14. * Contains an <CODE>SnmpTimeTick</CODE> value which
  15. * has units of 1/100th of a second.
  16. *
  17. * <p><b>This API is a Sun Microsystems internal API and is subject
  18. * to change without notice.</b></p>
  19. * @version 4.11 12/19/03
  20. * @author Sun Microsystems, Inc
  21. * @author Cisco Systems, Inc.
  22. */
  23. public class SnmpTimeticks extends SnmpUnsignedInt {
  24. // CONSTRUCTORS
  25. //-------------
  26. /**
  27. * Constructs a new <CODE>SnmpTimeticks</CODE> from the specified
  28. * integer value.
  29. * @param v The initialization value.
  30. * @exception IllegalArgumentException The specified value is negative.
  31. */
  32. public SnmpTimeticks(int v) throws IllegalArgumentException {
  33. super(v) ;
  34. }
  35. /**
  36. * Constructs a new <CODE>SnmpTimeticks</CODE> from the specified
  37. * <CODE>Integer</CODE> value.
  38. * @param v The initialization value.
  39. * @exception IllegalArgumentException The specified value is negative.
  40. */
  41. public SnmpTimeticks(Integer v) throws IllegalArgumentException {
  42. super(v) ;
  43. }
  44. /**
  45. * Constructs a new <CODE>SnmpTimeticks</CODE> from the specified long
  46. * value.
  47. * <p>If the specified value is greater than {@link
  48. * SnmpUnsignedInt#MAX_VALUE SnmpUnsignedInt.MAX_VALUE}, the SnmpTimeTicks
  49. * will be initialized with <code>v%(SnmpUnsignedInt.MAX_VALUE+1)</code>.
  50. * @param v The initialization value.
  51. * @exception IllegalArgumentException if the specified value is negative.
  52. */
  53. public SnmpTimeticks(long v) throws IllegalArgumentException {
  54. super(((v>0)?v&SnmpUnsignedInt.MAX_VALUE:v)) ;
  55. }
  56. /**
  57. * Constructs a new <CODE>SnmpTimeticks</CODE> from the specified
  58. * <CODE>Long</CODE> value.
  59. * <p>If the specified value is greater than {@link
  60. * SnmpUnsignedInt#MAX_VALUE SnmpUnsignedInt.MAX_VALUE}, the SnmpTimeTicks
  61. * will be initialized with <code>v%(SnmpUnsignedInt.MAX_VALUE+1)</code>.
  62. * @param v The initialization value.
  63. * @exception IllegalArgumentException if the specified value is negative.
  64. */
  65. public SnmpTimeticks(Long v) throws IllegalArgumentException {
  66. this(v.longValue()) ;
  67. }
  68. // PUBLIC METHODS
  69. //---------------
  70. /**
  71. * Parses the specified long value with time units and
  72. * returns a <CODE>String</CODE> of the form <CODE>d days hh:mm:ss</CODE>.
  73. * @param timeticks The value to be parsed.
  74. * @return The <CODE>String</CODE> representation of the value.
  75. */
  76. final static public String printTimeTicks(long timeticks) {
  77. int seconds, minutes, hours, days;
  78. StringBuffer buf = new StringBuffer() ;
  79. timeticks /= 100;
  80. days = (int)(timeticks / (60 * 60 * 24));
  81. timeticks %= (60 * 60 * 24);
  82. hours = (int)(timeticks / (60 * 60)) ;
  83. timeticks %= (60 * 60);
  84. minutes = (int)(timeticks / 60) ;
  85. seconds = (int)(timeticks % 60) ;
  86. if (days == 0) {
  87. buf.append(hours + ":" + minutes + ":" + seconds) ;
  88. return buf.toString() ;
  89. }
  90. if (days == 1) {
  91. buf.append("1 day ") ;
  92. } else {
  93. buf.append(days + " days ") ;
  94. }
  95. buf.append(hours + ":" + minutes + ":" + seconds) ;
  96. return buf.toString() ;
  97. }
  98. /**
  99. * Converts the timeticks value to its <CODE>String</CODE> form.
  100. * The format of the returned <CODE>String</CODE> is <CODE>d days hh:mm:ss</CODE>.
  101. * <BR>Note: this method simply calls the {@link #printTimeTicks printTimeTicks} method.
  102. * @return The <CODE>String</CODE> representation of the value.
  103. */
  104. final public String toString() {
  105. return printTimeTicks((long)value) ;
  106. }
  107. /**
  108. * Returns a textual description of the type object.
  109. * @return ASN.1 textual description.
  110. */
  111. final public String getTypeName() {
  112. return name;
  113. }
  114. // VARIABLES
  115. //----------
  116. /**
  117. * Name of the type.
  118. */
  119. final static String name = "TimeTicks" ;
  120. static final private long serialVersionUID = -5486435222360030630L;
  121. }