KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > smi > TimeTicks


1 /*_############################################################################
2   _##
3   _## SNMP4J - TimeTicks.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23
24
25 package org.snmp4j.smi;
26
27 import java.io.*;
28 import java.text.MessageFormat JavaDoc;
29 import org.snmp4j.asn1.BER;
30 import org.snmp4j.asn1.BERInputStream;
31
32 /**
33  * The <code>TimeTicks</code> class represents the time in 1/100 seconds
34  * since some epoch (which should be have been defined in the
35  * corresponding MIB specification).
36  *
37  * @author Frank Fock
38  * @version 1.7
39  */

40 public class TimeTicks extends UnsignedInteger32 {
41
42   private static final long serialVersionUID = 8663761323061572311L;
43
44   private static final String JavaDoc 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   /**
52    * Copy constructor.
53    * @param other
54    * a TimeTicks instance.
55    * @since 1.7
56    */

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 JavaDoc 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   /**
87    * Returns string with the value of this <code>TimeTicks</code> object as
88    * "[days,]hh:mm:ss.hh".
89    *
90    * @return
91    * a <code>String</code> representation of this object.
92    */

93   public String JavaDoc toString() {
94     return toString(FORMAT_PATTERN);
95   }
96
97   /**
98    * Formats the content of this <code>TimeTicks</code> object according to
99    * a supplied <code>MessageFormat</code> pattern.
100    * @param pattern
101    * a <code>MessageFormat</code> pattern that takes up to five parameters
102    * which are: days, hours, minutes, seconds, and 1/100 seconds.
103    * @return
104    * the formatted string representation.
105    */

106   public String JavaDoc toString(String JavaDoc 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 JavaDoc[] values = new Long JavaDoc[5];
125     values[0] = new Long JavaDoc(days);
126     values[1] = new Long JavaDoc(hours);
127     values[2] = new Long JavaDoc(minutes);
128     values[3] = new Long JavaDoc(seconds);
129     values[4] = new Long JavaDoc(hseconds);
130
131     return MessageFormat.format(pattern, (Object JavaDoc[])values);
132   }
133
134   /**
135    * Returns the timeticks value as milliseconds (instead 1/100 seconds).
136    * @return
137    * <code>getValue()*10</code>.
138    * @since 1.7
139    */

140   public long toMilliseconds() {
141     return value*10;
142   }
143
144   /**
145    * Sets the timeticks value by milliseconds.
146    * @param millis
147    * sets the value as <code>setValue(millis/10)</code>.
148    * @since 1.7
149    */

150   public void fromMilliseconds(long millis) {
151     setValue(millis/10);
152   }
153 }
154
155
Popular Tags