blob: 1ef71130f49008f4d582e9cc19c3b79d1b078704 [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
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 */
75 public static Frequency ofKHz(double value) {
76 return new Frequency((long) (value * KHZ));
77 }
78
79 /**
80 * Returns an instance representing the specified value in MHz.
81 *
82 * @param value frequency in MHz
83 * @return instance representing the given frequency
84 */
85 public static Frequency ofMHz(double value) {
86 return new Frequency((long) (value * MHZ));
87 }
88
89 /**
90 * Returns an instance representing the specified value in GHz.
91 *
92 * @param value frequency in GHz
93 * @return instance representing the given frequency
94 */
95 public static Frequency ofGHz(double value) {
96 return new Frequency((long) (value * GHZ));
97 }
98
99 /**
100 * Returns an instance representing the specified value in THz.
101 *
102 * @param value frequency in THz
103 * @return instance representing the given frequency
104 */
105 public static Frequency ofTHz(double value) {
106 return new Frequency((long) (value * THZ));
107 }
108
109 /**
110 * Returns a Frequency whose value is (this + value).
111 *
112 * @param value value to be added to this Frequency
113 * @return this + value
114 */
115 public Frequency add(Frequency value) {
116 return new Frequency(this.frequency + value.frequency);
117 }
118
119 /**
120 * Returns a Frequency whose value is (this - value).
121 *
122 * @param value value to be subtracted from this Frequency
123 * @return this - value
124 */
125 public Frequency subtract(Frequency value) {
126 return new Frequency(this.frequency - value.frequency);
127 }
128
129 /**
130 * Returns a Frequency whose value is (this * value).
131 *
132 * @param value value to be multiplied by this Frequency
133 * @return this * value
134 */
135 public Frequency multiply(long value) {
136 return new Frequency(this.frequency * value);
137 }
138
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700139 /**
140 * Returns a Frequency whose value is Math.floorDiv(this, value).
141 *
Thomas Vachuska5d410a22015-05-19 17:52:37 -0700142 * @param value value to be divided by this Frequency
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700143 * @return Math.floorDiv(this, value)
144 */
145 public Frequency floorDivision(long value) {
146 return new Frequency(Math.floorDiv(this.frequency, value));
147 }
148
Sho SHIMIZUea560282015-04-27 11:29:56 -0700149 @Override
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700150 public int compareTo(Frequency other) {
151 return ComparisonChain.start()
152 .compare(this.frequency, other.frequency)
153 .result();
154 }
155
156 @Override
Sho SHIMIZUea560282015-04-27 11:29:56 -0700157 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700158 return Long.hashCode(frequency);
Sho SHIMIZUea560282015-04-27 11:29:56 -0700159 }
160
161 @Override
162 public boolean equals(Object obj) {
163 if (this == obj) {
164 return true;
165 }
166
167 if (!(obj instanceof Frequency)) {
168 return false;
169 }
170
171 final Frequency other = (Frequency) obj;
172 return this.frequency == other.frequency;
173 }
174
175 @Override
176 public String toString() {
177 return MoreObjects.toStringHelper(this)
178 .add("frequency", frequency + "Hz")
179 .toString();
180 }
181}