Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cpman; |
| 17 | |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Primitive Metric Value. |
| 22 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 23 | public final class MetricValue { |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 24 | |
| 25 | private final long rate; |
| 26 | private final long load; |
| 27 | private final long count; |
| 28 | |
| 29 | /** |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 30 | * Constructs a metric value using the given rate, load and count. |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 31 | * |
| 32 | * @param rate rate |
| 33 | * @param load load |
| 34 | * @param count count |
| 35 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 36 | private MetricValue(long rate, long load, long count) { |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 37 | this.rate = rate; |
| 38 | this.load = load; |
| 39 | this.count = count; |
| 40 | } |
| 41 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Returns rate value. |
| 44 | * |
| 45 | * @return rate |
| 46 | */ |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 47 | public long getRate() { |
| 48 | return rate; |
| 49 | } |
| 50 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Returns load value. |
| 53 | * |
| 54 | * @return load |
| 55 | */ |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 56 | public long getLoad() { |
| 57 | return load; |
| 58 | } |
| 59 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Returns count value. |
| 62 | * |
| 63 | * @return cound |
| 64 | */ |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 65 | public long getCount() { |
| 66 | return count; |
| 67 | } |
| 68 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 69 | /** |
| 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 Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 120 | @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 Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 148 | return MoreObjects.toStringHelper(getClass()) |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 149 | .add("rate", Long.toHexString(rate)) |
| 150 | .add("load", Long.toHexString(load)) |
| 151 | .add("count", Long.toHexString(count)).toString(); |
| 152 | } |
| 153 | } |