blob: 5669abdc897df569da14a5c461a2687abbcf608f [file] [log] [blame]
Sho SHIMIZUea560282015-04-27 11:29:56 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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
21import java.util.Objects;
22
23/**
24 * Class representing frequency. This class is intended to be used for a value whose unit is Hz
25 * and its family (KHz, MHz, etc.).
26 *
27 * <p>
28 * Note: this class is mainly intended to be used for lambda, which
29 * represents THz order. Long has enough space to represent over THz frequency as Hz,
30 * and the underlying value is long as Hz. This means this class can't represent
31 * sub-Hz accuracy.
32 * </p>
33 */
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -070034public final class Frequency implements RichComparable<Frequency> {
Sho SHIMIZUea560282015-04-27 11:29:56 -070035
36 private static final long KHZ = 1_000L;
37 private static final long MHZ = 1_000_000L;
38 private static final long GHZ = 1_000_000_000L;
39 private static final long THZ = 1_000_000_000_000L;
40
41 private final long frequency; // frequency in Hz
42
43 /**
44 * Creates an instance representing the specified frequency in Hz.
45 *
46 * @param frequency frequency in Hz
47 */
48 private Frequency(long frequency) {
49 this.frequency = frequency;
50 }
51
52 /**
53 * Return the value this instance represents as Hz.
54 *
55 * @return frequency in Hz
56 */
57 public long asHz() {
58 return frequency;
59 }
60
61 /**
62 * Returns an instance representing the specified value in Hz.
63 *
64 * @param value frequency in Hz
65 * @return instance representing the given frequency
66 */
67 public static Frequency ofHz(long value) {
68 return new Frequency(value);
69 }
70
71 /**
72 * Returns an instance representing the specified value in KHz.
73 *
74 * @param value frequency in KHz
75 * @return instance representing the given frequency
76 */
77 public static Frequency ofKHz(double value) {
78 return new Frequency((long) (value * KHZ));
79 }
80
81 /**
82 * Returns an instance representing the specified value in MHz.
83 *
84 * @param value frequency in MHz
85 * @return instance representing the given frequency
86 */
87 public static Frequency ofMHz(double value) {
88 return new Frequency((long) (value * MHZ));
89 }
90
91 /**
92 * Returns an instance representing the specified value in GHz.
93 *
94 * @param value frequency in GHz
95 * @return instance representing the given frequency
96 */
97 public static Frequency ofGHz(double value) {
98 return new Frequency((long) (value * GHZ));
99 }
100
101 /**
102 * Returns an instance representing the specified value in THz.
103 *
104 * @param value frequency in THz
105 * @return instance representing the given frequency
106 */
107 public static Frequency ofTHz(double value) {
108 return new Frequency((long) (value * THZ));
109 }
110
111 /**
112 * Returns a Frequency whose value is (this + value).
113 *
114 * @param value value to be added to this Frequency
115 * @return this + value
116 */
117 public Frequency add(Frequency value) {
118 return new Frequency(this.frequency + value.frequency);
119 }
120
121 /**
122 * Returns a Frequency whose value is (this - value).
123 *
124 * @param value value to be subtracted from this Frequency
125 * @return this - value
126 */
127 public Frequency subtract(Frequency value) {
128 return new Frequency(this.frequency - value.frequency);
129 }
130
131 /**
132 * Returns a Frequency whose value is (this * value).
133 *
134 * @param value value to be multiplied by this Frequency
135 * @return this * value
136 */
137 public Frequency multiply(long value) {
138 return new Frequency(this.frequency * value);
139 }
140
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700141 /**
142 * Returns a Frequency whose value is Math.floorDiv(this, value).
143 *
Thomas Vachuska5d410a22015-05-19 17:52:37 -0700144 * @param value value to be divided by this Frequency
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700145 * @return Math.floorDiv(this, value)
146 */
147 public Frequency floorDivision(long value) {
148 return new Frequency(Math.floorDiv(this.frequency, value));
149 }
150
Sho SHIMIZUea560282015-04-27 11:29:56 -0700151 @Override
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700152 public int compareTo(Frequency other) {
153 return ComparisonChain.start()
154 .compare(this.frequency, other.frequency)
155 .result();
156 }
157
158 @Override
Sho SHIMIZUea560282015-04-27 11:29:56 -0700159 public int hashCode() {
160 return Objects.hash(frequency);
161 }
162
163 @Override
164 public boolean equals(Object obj) {
165 if (this == obj) {
166 return true;
167 }
168
169 if (!(obj instanceof Frequency)) {
170 return false;
171 }
172
173 final Frequency other = (Frequency) obj;
174 return this.frequency == other.frequency;
175 }
176
177 @Override
178 public String toString() {
179 return MoreObjects.toStringHelper(this)
180 .add("frequency", frequency + "Hz")
181 .toString();
182 }
183}