001: /*_############################################################################
002: _##
003: _## SNMP4J - TimeTicks.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.smi;
022:
023: import java.io.*;
024: import java.text.MessageFormat;
025: import org.snmp4j.asn1.BER;
026: import org.snmp4j.asn1.BERInputStream;
027:
028: /**
029: * The <code>TimeTicks</code> class represents the time in 1/100 seconds
030: * since some epoch (which should be have been defined in the
031: * corresponding MIB specification).
032: *
033: * @author Frank Fock
034: * @version 1.7
035: */
036: public class TimeTicks extends UnsignedInteger32 {
037:
038: private static final long serialVersionUID = 8663761323061572311L;
039:
040: private static final String FORMAT_PATTERN = "{0,choice,0#|1#1 day, |1<{0,number,integer} days, }"
041: + "{1,number,integer}:{2,number,00}:{3,number,00}.{4,number,00}";
042:
043: public TimeTicks() {
044: }
045:
046: /**
047: * Copy constructor.
048: * @param other
049: * a TimeTicks instance.
050: * @since 1.7
051: */
052: public TimeTicks(TimeTicks other) {
053: this .value = other.value;
054: }
055:
056: public TimeTicks(long value) {
057: super (value);
058: }
059:
060: public Object clone() {
061: return new TimeTicks(value);
062: }
063:
064: public int getSyntax() {
065: return SMIConstants.SYNTAX_TIMETICKS;
066: }
067:
068: public void encodeBER(OutputStream os) throws IOException {
069: BER.encodeUnsignedInteger(os, BER.TIMETICKS, super .getValue());
070: }
071:
072: public void decodeBER(BERInputStream inputStream)
073: throws IOException {
074: BER.MutableByte type = new BER.MutableByte();
075: long newValue = BER.decodeUnsignedInteger(inputStream, type);
076: if (type.getValue() != BER.TIMETICKS) {
077: throw new IOException(
078: "Wrong type encountered when decoding TimeTicks: "
079: + type.getValue());
080: }
081: setValue(newValue);
082: }
083:
084: /**
085: * Returns string with the value of this <code>TimeTicks</code> object as
086: * "[days,]hh:mm:ss.hh".
087: *
088: * @return
089: * a <code>String</code> representation of this object.
090: */
091: public String toString() {
092: return toString(FORMAT_PATTERN);
093: }
094:
095: /**
096: * Formats the content of this <code>TimeTicks</code> object according to
097: * a supplied <code>MessageFormat</code> pattern.
098: * @param pattern
099: * a <code>MessageFormat</code> pattern that takes up to five parameters
100: * which are: days, hours, minutes, seconds, and 1/100 seconds.
101: * @return
102: * the formatted string representation.
103: */
104: public String toString(String pattern) {
105: long hseconds, seconds, minutes, hours, days;
106: long tt = getValue();
107:
108: days = tt / 8640000;
109: tt %= 8640000;
110:
111: hours = tt / 360000;
112: tt %= 360000;
113:
114: minutes = tt / 6000;
115: tt %= 6000;
116:
117: seconds = tt / 100;
118: tt %= 100;
119:
120: hseconds = tt;
121:
122: Long[] values = new Long[5];
123: values[0] = new Long(days);
124: values[1] = new Long(hours);
125: values[2] = new Long(minutes);
126: values[3] = new Long(seconds);
127: values[4] = new Long(hseconds);
128:
129: return MessageFormat.format(pattern, (Object[]) values);
130: }
131:
132: /**
133: * Returns the timeticks value as milliseconds (instead 1/100 seconds).
134: * @return
135: * <code>getValue()*10</code>.
136: * @since 1.7
137: */
138: public long toMilliseconds() {
139: return value * 10;
140: }
141:
142: /**
143: * Sets the timeticks value by milliseconds.
144: * @param millis
145: * sets the value as <code>setValue(millis/10)</code>.
146: * @since 1.7
147: */
148: public void fromMilliseconds(long millis) {
149: setValue(millis / 10);
150: }
151: }
|