61e3872669b67f6a01f955bf2d15780635e0c84a
[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 /**
15  * @file   Latency.java
16  *
17  * @brief  Class representing Latency
18  *
19  * Describe a latency in picoseconds or multiple of its.
20  */
21 @XmlRootElement
22 public class Latency extends Property {
23     private static final long serialVersionUID = 1L;
24     private long latency;
25
26     public static final long LATENCYUNK = 0;
27     public static final long LATENCY1ns = (long) Math.pow(10, 3);
28     public static final long LATENCY10ns = (long) Math.pow(10, 4);
29     public static final long LATENCY100ns = (long) Math.pow(10, 5);
30     public static final long LATENCY1us = (long) Math.pow(10, 6);
31     public static final long LATENCY10us = (long) Math.pow(10, 7);
32     public static final long LATENCY100us = (long) Math.pow(10, 8);
33     public static final long LATENCY1ms = (long) Math.pow(10, 9);
34     public static final long LATENCY1s = (long) Math.pow(10, 12);
35
36     public static final String LatencyPropName = "latency";
37
38     /*
39      * Private constructor used for JAXB mapping
40      */
41     private Latency() {
42         super(LatencyPropName);
43         this.latency = LATENCYUNK;
44     }
45
46     public Latency(long latency) {
47         super(LatencyPropName);
48         this.latency = latency;
49     }
50
51     public Latency(int latency) {
52         super(LatencyPropName);
53         this.latency = (long) latency;
54     }
55
56     public Latency(short latency) {
57         super(LatencyPropName);
58         this.latency = (long) latency;
59     }
60
61     public Latency clone() {
62         return new Latency(this.latency);
63     }
64
65     public long getValue() {
66         return this.latency;
67     }
68
69     @Override
70     public int hashCode() {
71         final int prime = 31;
72         int result = super.hashCode();
73         result = prime * result + (int) (latency ^ (latency >>> 32));
74         return result;
75     }
76
77     @Override
78     public boolean equals(Object obj) {
79         if (this == obj)
80             return true;
81         if (!super.equals(obj))
82             return false;
83         if (getClass() != obj.getClass())
84             return false;
85         Latency other = (Latency) obj;
86         if (latency != other.latency)
87             return false;
88         return true;
89     }
90
91     @Override
92     public String toString() {
93         StringBuffer sb = new StringBuffer();
94
95         sb.append("Latency[");
96         if (this.latency == 0) {
97             sb.append("UnKnown");
98         } else if (this.latency < LATENCY1ns) {
99             sb.append(this.latency + "psec");
100         } else if (this.latency < LATENCY1us) {
101             sb.append(Long.toString(this.latency / LATENCY1ns) + "nsec");
102         } else if (this.latency < LATENCY1ms) {
103             sb.append(Long.toString(this.latency / LATENCY1us) + "usec");
104         } else if (this.latency < LATENCY1s) {
105             sb.append(Long.toString(this.latency / LATENCY1ms) + "msec");
106         }
107
108         sb.append("]");
109         return sb.toString();
110     }
111 }