blob: 3a6001528fe56f1a70f25678f0ea12e7485f2a56 [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 /**
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 SHIMIZU00762ee2016-01-05 16:32:24 -080075 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 SHIMIZUea560282015-04-27 11:29:56 -070085 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 SHIMIZU00762ee2016-01-05 16:32:24 -080095 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 SHIMIZUea560282015-04-27 11:29:56 -0700105 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 SHIMIZU00762ee2016-01-05 16:32:24 -0800115 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 SHIMIZUea560282015-04-27 11:29:56 -0700125 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 SHIMIZU00762ee2016-01-05 16:32:24 -0800135 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 SHIMIZUea560282015-04-27 11:29:56 -0700145 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 Leenheer1afa2a02015-05-13 09:18:07 -0700179 /**
180 * Returns a Frequency whose value is Math.floorDiv(this, value).
181 *
Thomas Vachuska5d410a22015-05-19 17:52:37 -0700182 * @param value value to be divided by this Frequency
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700183 * @return Math.floorDiv(this, value)
184 */
185 public Frequency floorDivision(long value) {
186 return new Frequency(Math.floorDiv(this.frequency, value));
187 }
188
Sho SHIMIZUea560282015-04-27 11:29:56 -0700189 @Override
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700190 public int compareTo(Frequency other) {
191 return ComparisonChain.start()
192 .compare(this.frequency, other.frequency)
193 .result();
194 }
195
196 @Override
Sho SHIMIZUea560282015-04-27 11:29:56 -0700197 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700198 return Long.hashCode(frequency);
Sho SHIMIZUea560282015-04-27 11:29:56 -0700199 }
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}