blob: 93de680e2eb3301126006fd4bce77134d0b28c92 [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
141 @Override
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700142 public int compareTo(Frequency other) {
143 return ComparisonChain.start()
144 .compare(this.frequency, other.frequency)
145 .result();
146 }
147
148 @Override
Sho SHIMIZUea560282015-04-27 11:29:56 -0700149 public int hashCode() {
150 return Objects.hash(frequency);
151 }
152
153 @Override
154 public boolean equals(Object obj) {
155 if (this == obj) {
156 return true;
157 }
158
159 if (!(obj instanceof Frequency)) {
160 return false;
161 }
162
163 final Frequency other = (Frequency) obj;
164 return this.frequency == other.frequency;
165 }
166
167 @Override
168 public String toString() {
169 return MoreObjects.toStringHelper(this)
170 .add("frequency", frequency + "Hz")
171 .toString();
172 }
173}