395863591437d2bec527cd3144724abf5593f646
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / Latency.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.core;
11
12 import javax.xml.bind.annotation.XmlRootElement;
13
14 import org.apache.commons.lang3.builder.EqualsBuilder;
15 import org.apache.commons.lang3.builder.HashCodeBuilder;
16
17 /**
18  * @file   Latency.java
19  *
20  * @brief  Class representing Latency
21  *
22  * Describe a latency in picoseconds or multiple of its.
23  */
24 @XmlRootElement
25 public class Latency extends Property {
26     private static final long serialVersionUID = 1L;
27     private long latency;
28
29     public static final long LATENCYUNK = 0;
30     public static final long LATENCY1ns = (long) Math.pow(10, 3);
31     public static final long LATENCY10ns = (long) Math.pow(10, 4);
32     public static final long LATENCY100ns = (long) Math.pow(10, 5);
33     public static final long LATENCY1us = (long) Math.pow(10, 6);
34     public static final long LATENCY10us = (long) Math.pow(10, 7);
35     public static final long LATENCY100us = (long) Math.pow(10, 8);
36     public static final long LATENCY1ms = (long) Math.pow(10, 9);
37     public static final long LATENCY1s = (long) Math.pow(10, 12);
38
39     public static final String LatencyPropName = "latency";
40
41     /*
42      * Private constructor used for JAXB mapping
43      */
44     private Latency() {
45         super(LatencyPropName);
46         this.latency = LATENCYUNK;
47     }
48
49     public Latency(long latency) {
50         super(LatencyPropName);
51         this.latency = latency;
52     }
53
54     public Latency(int latency) {
55         super(LatencyPropName);
56         this.latency = (long) latency;
57     }
58
59     public Latency(short latency) {
60         super(LatencyPropName);
61         this.latency = (long) latency;
62     }
63
64     public Latency clone() {
65         return new Latency(this.latency);
66     }
67
68     public long getValue() {
69         return this.latency;
70     }
71
72     @Override
73     public int hashCode() {
74         return HashCodeBuilder.reflectionHashCode(this);
75     }
76
77     @Override
78     public boolean equals(Object obj) {
79         return EqualsBuilder.reflectionEquals(this, obj);
80     }
81
82     @Override
83     public String toString() {
84         StringBuffer sb = new StringBuffer();
85
86         sb.append("Latency[");
87         if (this.latency == 0) {
88             sb.append("UnKnown");
89         } else if (this.latency < LATENCY1ns) {
90             sb.append(this.latency + "psec");
91         } else if (this.latency < LATENCY1us) {
92             sb.append(Long.toString(this.latency / LATENCY1ns) + "nsec");
93         } else if (this.latency < LATENCY1ms) {
94             sb.append(Long.toString(this.latency / LATENCY1us) + "usec");
95         } else if (this.latency < LATENCY1s) {
96             sb.append(Long.toString(this.latency / LATENCY1ms) + "msec");
97         }
98
99         sb.append("]");
100         return sb.toString();
101     }
102 }