1 20 21 22 23 24 25 package org.snmp4j.smi; 26 27 import java.io.*; 28 import java.text.MessageFormat ; 29 import org.snmp4j.asn1.BER; 30 import org.snmp4j.asn1.BERInputStream; 31 32 40 public class TimeTicks extends UnsignedInteger32 { 41 42 private static final long serialVersionUID = 8663761323061572311L; 43 44 private static final String FORMAT_PATTERN = 45 "{0,choice,0#|1#1 day, |1<{0,number,integer} days, }"+ 46 "{1,number,integer}:{2,number,00}:{3,number,00}.{4,number,00}"; 47 48 public TimeTicks() { 49 } 50 51 57 public TimeTicks(TimeTicks other) { 58 this.value = other.value; 59 } 60 61 public TimeTicks(long value) { 62 super(value); 63 } 64 65 public Object clone() { 66 return new TimeTicks(value); 67 } 68 69 public int getSyntax() { 70 return SMIConstants.SYNTAX_TIMETICKS; 71 } 72 73 public void encodeBER(OutputStream os) throws IOException { 74 BER.encodeUnsignedInteger(os, BER.TIMETICKS, super.getValue()); 75 } 76 77 public void decodeBER(BERInputStream inputStream) throws IOException { 78 BER.MutableByte type = new BER.MutableByte(); 79 long newValue = BER.decodeUnsignedInteger(inputStream, type); 80 if (type.getValue() != BER.TIMETICKS) { 81 throw new IOException("Wrong type encountered when decoding TimeTicks: "+type.getValue()); 82 } 83 setValue(newValue); 84 } 85 86 93 public String toString() { 94 return toString(FORMAT_PATTERN); 95 } 96 97 106 public String toString(String pattern) { 107 long hseconds, seconds, minutes, hours, days; 108 long tt = getValue(); 109 110 days = tt / 8640000; 111 tt %= 8640000; 112 113 hours = tt / 360000; 114 tt %= 360000; 115 116 minutes = tt / 6000; 117 tt %= 6000; 118 119 seconds = tt / 100; 120 tt %= 100; 121 122 hseconds = tt; 123 124 Long [] values = new Long [5]; 125 values[0] = new Long (days); 126 values[1] = new Long (hours); 127 values[2] = new Long (minutes); 128 values[3] = new Long (seconds); 129 values[4] = new Long (hseconds); 130 131 return MessageFormat.format(pattern, (Object [])values); 132 } 133 134 140 public long toMilliseconds() { 141 return value*10; 142 } 143 144 150 public void fromMilliseconds(long millis) { 151 setValue(millis/10); 152 } 153 } 154 155 | Popular Tags |