Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [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.onlab.util; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ComparisonChain; |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 20 | |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Class representing frequency. This class is intended to be used for a value whose unit is Hz |
| 23 | * and its family (KHz, MHz, etc.). |
| 24 | * |
| 25 | * <p> |
| 26 | * Note: this class is mainly intended to be used for lambda, which |
| 27 | * represents THz order. Long has enough space to represent over THz frequency as Hz, |
| 28 | * and the underlying value is long as Hz. This means this class can't represent |
| 29 | * sub-Hz accuracy. |
| 30 | * </p> |
| 31 | */ |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 32 | public final class Frequency implements RichComparable<Frequency> { |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 33 | |
| 34 | private static final long KHZ = 1_000L; |
| 35 | private static final long MHZ = 1_000_000L; |
| 36 | private static final long GHZ = 1_000_000_000L; |
| 37 | private static final long THZ = 1_000_000_000_000L; |
| 38 | |
| 39 | private final long frequency; // frequency in Hz |
| 40 | |
| 41 | /** |
| 42 | * Creates an instance representing the specified frequency in Hz. |
| 43 | * |
| 44 | * @param frequency frequency in Hz |
| 45 | */ |
| 46 | private Frequency(long frequency) { |
| 47 | this.frequency = frequency; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Return the value this instance represents as Hz. |
| 52 | * |
| 53 | * @return frequency in Hz |
| 54 | */ |
| 55 | public long asHz() { |
| 56 | return frequency; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns an instance representing the specified value in Hz. |
| 61 | * |
| 62 | * @param value frequency in Hz |
| 63 | * @return instance representing the given frequency |
| 64 | */ |
| 65 | public static Frequency ofHz(long value) { |
| 66 | return new Frequency(value); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns an instance representing the specified value in KHz. |
| 71 | * |
| 72 | * @param value frequency in KHz |
| 73 | * @return instance representing the given frequency |
| 74 | */ |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 75 | public static Frequency ofKHz(long value) { |
| 76 | return new Frequency(value * KHZ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns an instance representing the specified value in KHz. |
| 81 | * |
| 82 | * @param value frequency in KHz |
| 83 | * @return instance representing the given frequency |
| 84 | */ |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 85 | public static Frequency ofKHz(double value) { |
| 86 | return new Frequency((long) (value * KHZ)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns an instance representing the specified value in MHz. |
| 91 | * |
| 92 | * @param value frequency in MHz |
| 93 | * @return instance representing the given frequency |
| 94 | */ |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 95 | public static Frequency ofMHz(long value) { |
| 96 | return new Frequency(value * MHZ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns an instance representing the specified value in MHz. |
| 101 | * |
| 102 | * @param value frequency in MHz |
| 103 | * @return instance representing the given frequency |
| 104 | */ |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 105 | public static Frequency ofMHz(double value) { |
| 106 | return new Frequency((long) (value * MHZ)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns an instance representing the specified value in GHz. |
| 111 | * |
| 112 | * @param value frequency in GHz |
| 113 | * @return instance representing the given frequency |
| 114 | */ |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 115 | public static Frequency ofGHz(long value) { |
| 116 | return new Frequency(value * GHZ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns an instance representing the specified value in GHz. |
| 121 | * |
| 122 | * @param value frequency in GHz |
| 123 | * @return instance representing the given frequency |
| 124 | */ |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 125 | public static Frequency ofGHz(double value) { |
| 126 | return new Frequency((long) (value * GHZ)); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns an instance representing the specified value in THz. |
| 131 | * |
| 132 | * @param value frequency in THz |
| 133 | * @return instance representing the given frequency |
| 134 | */ |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 135 | public static Frequency ofTHz(long value) { |
| 136 | return new Frequency(value * THZ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns an instance representing the specified value in THz. |
| 141 | * |
| 142 | * @param value frequency in THz |
| 143 | * @return instance representing the given frequency |
| 144 | */ |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 145 | public static Frequency ofTHz(double value) { |
| 146 | return new Frequency((long) (value * THZ)); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns a Frequency whose value is (this + value). |
| 151 | * |
| 152 | * @param value value to be added to this Frequency |
| 153 | * @return this + value |
| 154 | */ |
| 155 | public Frequency add(Frequency value) { |
| 156 | return new Frequency(this.frequency + value.frequency); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns a Frequency whose value is (this - value). |
| 161 | * |
| 162 | * @param value value to be subtracted from this Frequency |
| 163 | * @return this - value |
| 164 | */ |
| 165 | public Frequency subtract(Frequency value) { |
| 166 | return new Frequency(this.frequency - value.frequency); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Returns a Frequency whose value is (this * value). |
| 171 | * |
| 172 | * @param value value to be multiplied by this Frequency |
| 173 | * @return this * value |
| 174 | */ |
| 175 | public Frequency multiply(long value) { |
| 176 | return new Frequency(this.frequency * value); |
| 177 | } |
| 178 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Returns a Frequency whose value is Math.floorDiv(this, value). |
| 181 | * |
Thomas Vachuska | 5d410a2 | 2015-05-19 17:52:37 -0700 | [diff] [blame] | 182 | * @param value value to be divided by this Frequency |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 183 | * @return Math.floorDiv(this, value) |
| 184 | */ |
| 185 | public Frequency floorDivision(long value) { |
| 186 | return new Frequency(Math.floorDiv(this.frequency, value)); |
| 187 | } |
| 188 | |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 189 | @Override |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 190 | public int compareTo(Frequency other) { |
| 191 | return ComparisonChain.start() |
| 192 | .compare(this.frequency, other.frequency) |
| 193 | .result(); |
| 194 | } |
| 195 | |
| 196 | @Override |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 197 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 198 | return Long.hashCode(frequency); |
Sho SHIMIZU | ea56028 | 2015-04-27 11:29:56 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public boolean equals(Object obj) { |
| 203 | if (this == obj) { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | if (!(obj instanceof Frequency)) { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | final Frequency other = (Frequency) obj; |
| 212 | return this.frequency == other.frequency; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public String toString() { |
| 217 | return MoreObjects.toStringHelper(this) |
| 218 | .add("frequency", frequency + "Hz") |
| 219 | .toString(); |
| 220 | } |
| 221 | } |