blob: 54986d85ac75fb0bdb15a568fb6f690639311612 [file] [log] [blame]
Jian Li60804322015-12-02 14:46:31 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li60804322015-12-02 14:46:31 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cpman;
17
Jian Li6b86a762016-01-29 09:30:40 -080018import com.google.common.base.MoreObjects;
Jian Li60804322015-12-02 14:46:31 -080019
20/**
21 * Primitive Metric Value.
22 */
Jian Lic132c112016-01-28 20:27:34 -080023public final class MetricValue {
Jian Li60804322015-12-02 14:46:31 -080024
25 private final long rate;
26 private final long load;
27 private final long count;
28
29 /**
Jian Lic5cb4a12016-02-03 23:24:42 -080030 * Constructs a metric value using the given rate, load and count.
Jian Li60804322015-12-02 14:46:31 -080031 *
32 * @param rate rate
33 * @param load load
34 * @param count count
35 */
Jian Lic132c112016-01-28 20:27:34 -080036 private MetricValue(long rate, long load, long count) {
Jian Li60804322015-12-02 14:46:31 -080037 this.rate = rate;
38 this.load = load;
39 this.count = count;
40 }
41
Jian Lic132c112016-01-28 20:27:34 -080042 /**
43 * Returns rate value.
44 *
45 * @return rate
46 */
Jian Li60804322015-12-02 14:46:31 -080047 public long getRate() {
48 return rate;
49 }
50
Jian Lic132c112016-01-28 20:27:34 -080051 /**
52 * Returns load value.
53 *
54 * @return load
55 */
Jian Li60804322015-12-02 14:46:31 -080056 public long getLoad() {
57 return load;
58 }
59
Jian Lic132c112016-01-28 20:27:34 -080060 /**
61 * Returns count value.
62 *
63 * @return cound
64 */
Jian Li60804322015-12-02 14:46:31 -080065 public long getCount() {
66 return count;
67 }
68
Jian Lic132c112016-01-28 20:27:34 -080069 /**
70 * MetricValue builder class.
71 */
72 public static final class Builder {
73 private long rate;
74 private long load;
75 private long count;
76
77 /**
78 * Sets rate value.
79 *
80 * @param rate rate value
81 * @return Builder object
82 */
83 public Builder rate(long rate) {
84 this.rate = rate;
85 return this;
86 }
87
88 /**
89 * Sets load value.
90 *
91 * @param load load value
92 * @return Builder object
93 */
94 public Builder load(long load) {
95 this.load = load;
96 return this;
97 }
98
99 /**
100 * Sets count value.
101 *
102 * @param count count value
103 * @return Builder object
104 */
105 public Builder count(long count) {
106 this.count = count;
107 return this;
108 }
109
110 /**
111 * Builds a MetricValue object.
112 *
113 * @return MetricValue object
114 */
115 public MetricValue add() {
116 return new MetricValue(rate, load, count);
117 }
118 }
119
Jian Li60804322015-12-02 14:46:31 -0800120 @Override
121 public boolean equals(Object obj) {
122 if (this == obj) {
123 return true;
124 }
125 if (obj instanceof MetricValue) {
126 MetricValue other = (MetricValue) obj;
127 if (this.rate == other.rate &&
128 this.load == other.load &&
129 this.count == other.count) {
130 return true;
131 }
132 }
133 return false;
134 }
135
136 @Override
137 public int hashCode() {
138 final int prime = 1004;
139 int result = super.hashCode();
140 result = prime * result + (int) this.rate;
141 result = prime * result + (int) this.load;
142 result = prime * result + (int) this.count;
143 return result;
144 }
145
146 @Override
147 public String toString() {
Jian Li6b86a762016-01-29 09:30:40 -0800148 return MoreObjects.toStringHelper(getClass())
Jian Li60804322015-12-02 14:46:31 -0800149 .add("rate", Long.toHexString(rate))
150 .add("load", Long.toHexString(load))
151 .add("count", Long.toHexString(count)).toString();
152 }
153}