Move adsal into its own subdirectory.
[controller.git] / opendaylight / adsal / 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.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlRootElement;
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 @XmlAccessorType(XmlAccessType.NONE)
26 public class Latency extends Property {
27     private static final long serialVersionUID = 1L;
28
29     @XmlElement(name="value")
30     private long latencyValue;
31
32     public static final long LATENCYUNK = 0;
33     public static final long LATENCY1ns = (long) Math.pow(10, 3);
34     public static final long LATENCY10ns = (long) Math.pow(10, 4);
35     public static final long LATENCY100ns = (long) Math.pow(10, 5);
36     public static final long LATENCY1us = (long) Math.pow(10, 6);
37     public static final long LATENCY10us = (long) Math.pow(10, 7);
38     public static final long LATENCY100us = (long) Math.pow(10, 8);
39     public static final long LATENCY1ms = (long) Math.pow(10, 9);
40     public static final long LATENCY1s = (long) Math.pow(10, 12);
41
42     public static final String LatencyPropName = "latency";
43
44     /*
45      * Private constructor used for JAXB mapping
46      */
47     private Latency() {
48         super(LatencyPropName);
49         this.latencyValue = LATENCYUNK;
50     }
51
52     public Latency(long latency) {
53         super(LatencyPropName);
54         this.latencyValue = latency;
55     }
56
57     public Latency(int latency) {
58         super(LatencyPropName);
59         this.latencyValue = (long) latency;
60     }
61
62     public Latency(short latency) {
63         super(LatencyPropName);
64         this.latencyValue = (long) latency;
65     }
66
67     @Override
68     public Latency clone() {
69         return new Latency(this.latencyValue);
70     }
71
72     public long getValue() {
73         return this.latencyValue;
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = super.hashCode();
80         result = prime * result + (int) (latencyValue ^ (latencyValue >>> 32));
81         return result;
82     }
83
84     @Override
85     public boolean equals(Object obj) {
86         if (this == obj)
87             return true;
88         if (!super.equals(obj))
89             return false;
90         if (getClass() != obj.getClass())
91             return false;
92         Latency other = (Latency) obj;
93         if (latencyValue != other.latencyValue)
94             return false;
95         return true;
96     }
97
98     @Override
99     public String toString() {
100         StringBuffer sb = new StringBuffer();
101
102         sb.append("Latency[");
103         if (this.latencyValue == 0) {
104             sb.append("UnKnown");
105         } else if (this.latencyValue < LATENCY1ns) {
106             sb.append(this.latencyValue).append("psec");
107         } else if (this.latencyValue < LATENCY1us) {
108             sb.append(Long.toString(this.latencyValue / LATENCY1ns)).append("nsec");
109         } else if (this.latencyValue < LATENCY1ms) {
110             sb.append(Long.toString(this.latencyValue / LATENCY1us)).append("usec");
111         } else if (this.latencyValue < LATENCY1s) {
112             sb.append(Long.toString(this.latencyValue / LATENCY1ms)).append("msec");
113         }
114
115         sb.append("]");
116         return sb.toString();
117     }
118
119     @Override
120     public String getStringValue() {
121         if (this.latencyValue == 0) {
122             return("UnKnown");
123         } else if (this.latencyValue < LATENCY1ns) {
124             return(this.latencyValue + "psec");
125         } else if (this.latencyValue < LATENCY1us) {
126             return(Long.toString(this.latencyValue / LATENCY1ns) + "nsec");
127         } else if (this.latencyValue < LATENCY1ms) {
128             return(Long.toString(this.latencyValue / LATENCY1us) + "usec");
129         } else if (this.latencyValue < LATENCY1s) {
130             return(Long.toString(this.latencyValue / LATENCY1ms) + "msec");
131         } else {
132             return Long.toString(this.latencyValue) + "sec";
133         }
134     }
135 }