blob: 9fd296e65818dbf06c5a4532cc8d1745bb18464d [file] [log] [blame]
Sho SHIMIZUea560282015-04-27 11:29:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUea560282015-04-27 11:29:56 -07003 *
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.onlab.util;
17
18import com.google.common.base.MoreObjects;
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -070019import com.google.common.collect.ComparisonChain;
Sho SHIMIZUea560282015-04-27 11:29:56 -070020
Sho SHIMIZUea560282015-04-27 11:29:56 -070021/**
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 SHIMIZU2908d9e2015-05-08 16:40:06 -070032public final class Frequency implements RichComparable<Frequency> {
Sho SHIMIZUea560282015-04-27 11:29:56 -070033
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 /**
Marc De Leenheer40a544b2017-04-25 14:16:02 -070060 * Return the value this instance represents as KHz.
61 *
62 * @return frequency in kHz
63 */
64 public double asKHz() {
65 return (double) frequency / KHZ;
66 }
67
68 /**
69 * Return the value this instance represents as MHz.
70 *
71 * @return frequency in MHz
72 */
73 public double asMHz() {
74 return (double) frequency / MHZ;
75 }
76
77 /**
78 * Return the value this instance represents as GHz.
79 *
80 * @return frequency in GHz
81 */
82 public double asGHz() {
83 return (double) frequency / GHZ;
84 }
85
86 /**
87 * Return the value this instance represents as THz.
88 *
89 * @return frequency in THz
90 */
91 public double asTHz() {
92 return (double) frequency / THZ;
93 }
94
95 /**
Sho SHIMIZUea560282015-04-27 11:29:56 -070096 * Returns an instance representing the specified value in Hz.
97 *
98 * @param value frequency in Hz
99 * @return instance representing the given frequency
100 */
101 public static Frequency ofHz(long value) {
102 return new Frequency(value);
103 }
104
105 /**
106 * Returns an instance representing the specified value in KHz.
107 *
108 * @param value frequency in KHz
109 * @return instance representing the given frequency
110 */
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800111 public static Frequency ofKHz(long value) {
112 return new Frequency(value * KHZ);
113 }
114
115 /**
116 * Returns an instance representing the specified value in KHz.
117 *
118 * @param value frequency in KHz
119 * @return instance representing the given frequency
120 */
Sho SHIMIZUea560282015-04-27 11:29:56 -0700121 public static Frequency ofKHz(double value) {
122 return new Frequency((long) (value * KHZ));
123 }
124
125 /**
126 * Returns an instance representing the specified value in MHz.
127 *
128 * @param value frequency in MHz
129 * @return instance representing the given frequency
130 */
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800131 public static Frequency ofMHz(long value) {
132 return new Frequency(value * MHZ);
133 }
134
135 /**
136 * Returns an instance representing the specified value in MHz.
137 *
138 * @param value frequency in MHz
139 * @return instance representing the given frequency
140 */
Sho SHIMIZUea560282015-04-27 11:29:56 -0700141 public static Frequency ofMHz(double value) {
142 return new Frequency((long) (value * MHZ));
143 }
144
145 /**
146 * Returns an instance representing the specified value in GHz.
147 *
148 * @param value frequency in GHz
149 * @return instance representing the given frequency
150 */
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800151 public static Frequency ofGHz(long value) {
152 return new Frequency(value * GHZ);
153 }
154
155 /**
156 * Returns an instance representing the specified value in GHz.
157 *
158 * @param value frequency in GHz
159 * @return instance representing the given frequency
160 */
Sho SHIMIZUea560282015-04-27 11:29:56 -0700161 public static Frequency ofGHz(double value) {
162 return new Frequency((long) (value * GHZ));
163 }
164
165 /**
166 * Returns an instance representing the specified value in THz.
167 *
168 * @param value frequency in THz
169 * @return instance representing the given frequency
170 */
Sho SHIMIZU00762ee2016-01-05 16:32:24 -0800171 public static Frequency ofTHz(long value) {
172 return new Frequency(value * THZ);
173 }
174
175 /**
176 * Returns an instance representing the specified value in THz.
177 *
178 * @param value frequency in THz
179 * @return instance representing the given frequency
180 */
Sho SHIMIZUea560282015-04-27 11:29:56 -0700181 public static Frequency ofTHz(double value) {
182 return new Frequency((long) (value * THZ));
183 }
184
185 /**
186 * Returns a Frequency whose value is (this + value).
187 *
188 * @param value value to be added to this Frequency
189 * @return this + value
190 */
191 public Frequency add(Frequency value) {
192 return new Frequency(this.frequency + value.frequency);
193 }
194
195 /**
196 * Returns a Frequency whose value is (this - value).
197 *
198 * @param value value to be subtracted from this Frequency
199 * @return this - value
200 */
201 public Frequency subtract(Frequency value) {
202 return new Frequency(this.frequency - value.frequency);
203 }
204
205 /**
206 * Returns a Frequency whose value is (this * value).
207 *
208 * @param value value to be multiplied by this Frequency
209 * @return this * value
210 */
211 public Frequency multiply(long value) {
212 return new Frequency(this.frequency * value);
213 }
214
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700215 /**
216 * Returns a Frequency whose value is Math.floorDiv(this, value).
217 *
Thomas Vachuska5d410a22015-05-19 17:52:37 -0700218 * @param value value to be divided by this Frequency
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700219 * @return Math.floorDiv(this, value)
220 */
221 public Frequency floorDivision(long value) {
222 return new Frequency(Math.floorDiv(this.frequency, value));
223 }
224
Sho SHIMIZUea560282015-04-27 11:29:56 -0700225 @Override
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700226 public int compareTo(Frequency other) {
227 return ComparisonChain.start()
228 .compare(this.frequency, other.frequency)
229 .result();
230 }
231
232 @Override
Sho SHIMIZUea560282015-04-27 11:29:56 -0700233 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700234 return Long.hashCode(frequency);
Sho SHIMIZUea560282015-04-27 11:29:56 -0700235 }
236
237 @Override
238 public boolean equals(Object obj) {
239 if (this == obj) {
240 return true;
241 }
242
243 if (!(obj instanceof Frequency)) {
244 return false;
245 }
246
247 final Frequency other = (Frequency) obj;
248 return this.frequency == other.frequency;
249 }
250
251 @Override
252 public String toString() {
253 return MoreObjects.toStringHelper(this)
254 .add("frequency", frequency + "Hz")
255 .toString();
256 }
257}