a0ba47b29d2458aadcffa90e115f0c1a218602e6
[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.XmlElement;
13 import javax.xml.bind.annotation.XmlRootElement;
14
15 /**
16  * @file   Latency.java
17  *
18  * @brief  Class representing Latency
19  *
20  * Describe a latency in picoseconds or multiple of its.
21  */
22 @XmlRootElement
23 public class Latency extends Property {
24     private static final long serialVersionUID = 1L;
25
26     @XmlElement(name="value")
27     private long latencyValue;
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.latencyValue = LATENCYUNK;
47     }
48
49     public Latency(long latency) {
50         super(LatencyPropName);
51         this.latencyValue = latency;
52     }
53
54     public Latency(int latency) {
55         super(LatencyPropName);
56         this.latencyValue = (long) latency;
57     }
58
59     public Latency(short latency) {
60         super(LatencyPropName);
61         this.latencyValue = (long) latency;
62     }
63
64     @Override
65     public Latency clone() {
66         return new Latency(this.latencyValue);
67     }
68
69     public long getValue() {
70         return this.latencyValue;
71     }
72
73     @Override
74     public int hashCode() {
75         final int prime = 31;
76         int result = super.hashCode();
77         result = prime * result + (int) (latencyValue ^ (latencyValue >>> 32));
78         return result;
79     }
80
81     @Override
82     public boolean equals(Object obj) {
83         if (this == obj)
84             return true;
85         if (!super.equals(obj))
86             return false;
87         if (getClass() != obj.getClass())
88             return false;
89         Latency other = (Latency) obj;
90         if (latencyValue != other.latencyValue)
91             return false;
92         return true;
93     }
94
95     @Override
96     public String toString() {
97         StringBuffer sb = new StringBuffer();
98
99         sb.append("Latency[");
100         if (this.latencyValue == 0) {
101             sb.append("UnKnown");
102         } else if (this.latencyValue < LATENCY1ns) {
103             sb.append(this.latencyValue + "psec");
104         } else if (this.latencyValue < LATENCY1us) {
105             sb.append(Long.toString(this.latencyValue / LATENCY1ns) + "nsec");
106         } else if (this.latencyValue < LATENCY1ms) {
107             sb.append(Long.toString(this.latencyValue / LATENCY1us) + "usec");
108         } else if (this.latencyValue < LATENCY1s) {
109             sb.append(Long.toString(this.latencyValue / LATENCY1ms) + "msec");
110         }
111
112         sb.append("]");
113         return sb.toString();
114     }
115 }