a7fd580b010a54ef94741bdce3196b5890019393
[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 org.apache.commons.lang3.builder.HashCodeBuilder;
13 import org.apache.commons.lang3.builder.EqualsBuilder;
14
15 import javax.xml.bind.annotation.XmlRootElement;
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlElement;
19
20 /**
21  * @file   Latency.java
22  *
23  * @brief  Class representing Latency
24  *
25  * Describe a latency in picoseconds or multiple of its.
26  */
27 @XmlRootElement
28 public class Latency extends Property {
29     private static final long serialVersionUID = 1L;
30     private long latency;
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.latency = LATENCYUNK;
50     }
51
52     public Latency(long latency) {
53         super(LatencyPropName);
54         this.latency = latency;
55     }
56
57     public Latency(int latency) {
58         super(LatencyPropName);
59         this.latency = (long) latency;
60     }
61
62     public Latency(short latency) {
63         super(LatencyPropName);
64         this.latency = (long) latency;
65     }
66
67     public Latency clone() {
68         return new Latency(this.latency);
69     }
70
71     public long getValue() {
72         return this.latency;
73     }
74
75     @Override
76     public int hashCode() {
77         return HashCodeBuilder.reflectionHashCode(this);
78     }
79
80     @Override
81     public boolean equals(Object obj) {
82         return EqualsBuilder.reflectionEquals(this, obj);
83     }
84
85     @Override
86     public String toString() {
87         StringBuffer sb = new StringBuffer();
88
89         sb.append("Latency[");
90         if (this.latency == 0) {
91             sb.append("UnKnown");
92         } else if (this.latency < LATENCY1ns) {
93             sb.append(this.latency + "psec");
94         } else if (this.latency < LATENCY1us) {
95             sb.append(Long.toString(this.latency / LATENCY1ns) + "nsec");
96         } else if (this.latency < LATENCY1ms) {
97             sb.append(Long.toString(this.latency / LATENCY1us) + "usec");
98         } else if (this.latency < LATENCY1s) {
99             sb.append(Long.toString(this.latency / LATENCY1ms) + "msec");
100         }
101
102         sb.append("]");
103         return sb.toString();
104     }
105 }