blob: c6e262fcd8cda868d347df79eafb5b00b2a69b2a [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;
19
20import java.util.Objects;
21
22/**
23 * Class representing frequency. This class is intended to be used for a value whose unit is Hz
24 * and its family (KHz, MHz, etc.).
25 *
26 * <p>
27 * Note: this class is mainly intended to be used for lambda, which
28 * represents THz order. Long has enough space to represent over THz frequency as Hz,
29 * and the underlying value is long as Hz. This means this class can't represent
30 * sub-Hz accuracy.
31 * </p>
32 */
33public final class Frequency {
34
35 private static final long KHZ = 1_000L;
36 private static final long MHZ = 1_000_000L;
37 private static final long GHZ = 1_000_000_000L;
38 private static final long THZ = 1_000_000_000_000L;
39
40 private final long frequency; // frequency in Hz
41
42 /**
43 * Creates an instance representing the specified frequency in Hz.
44 *
45 * @param frequency frequency in Hz
46 */
47 private Frequency(long frequency) {
48 this.frequency = frequency;
49 }
50
51 /**
52 * Return the value this instance represents as Hz.
53 *
54 * @return frequency in Hz
55 */
56 public long asHz() {
57 return frequency;
58 }
59
60 /**
61 * Returns an instance representing the specified value in Hz.
62 *
63 * @param value frequency in Hz
64 * @return instance representing the given frequency
65 */
66 public static Frequency ofHz(long value) {
67 return new Frequency(value);
68 }
69
70 /**
71 * Returns an instance representing the specified value in KHz.
72 *
73 * @param value frequency in KHz
74 * @return instance representing the given frequency
75 */
76 public static Frequency ofKHz(double value) {
77 return new Frequency((long) (value * KHZ));
78 }
79
80 /**
81 * Returns an instance representing the specified value in MHz.
82 *
83 * @param value frequency in MHz
84 * @return instance representing the given frequency
85 */
86 public static Frequency ofMHz(double value) {
87 return new Frequency((long) (value * MHZ));
88 }
89
90 /**
91 * Returns an instance representing the specified value in GHz.
92 *
93 * @param value frequency in GHz
94 * @return instance representing the given frequency
95 */
96 public static Frequency ofGHz(double value) {
97 return new Frequency((long) (value * GHZ));
98 }
99
100 /**
101 * Returns an instance representing the specified value in THz.
102 *
103 * @param value frequency in THz
104 * @return instance representing the given frequency
105 */
106 public static Frequency ofTHz(double value) {
107 return new Frequency((long) (value * THZ));
108 }
109
110 /**
111 * Returns a Frequency whose value is (this + value).
112 *
113 * @param value value to be added to this Frequency
114 * @return this + value
115 */
116 public Frequency add(Frequency value) {
117 return new Frequency(this.frequency + value.frequency);
118 }
119
120 /**
121 * Returns a Frequency whose value is (this - value).
122 *
123 * @param value value to be subtracted from this Frequency
124 * @return this - value
125 */
126 public Frequency subtract(Frequency value) {
127 return new Frequency(this.frequency - value.frequency);
128 }
129
130 /**
131 * Returns a Frequency whose value is (this * value).
132 *
133 * @param value value to be multiplied by this Frequency
134 * @return this * value
135 */
136 public Frequency multiply(long value) {
137 return new Frequency(this.frequency * value);
138 }
139
140 @Override
141 public int hashCode() {
142 return Objects.hash(frequency);
143 }
144
145 @Override
146 public boolean equals(Object obj) {
147 if (this == obj) {
148 return true;
149 }
150
151 if (!(obj instanceof Frequency)) {
152 return false;
153 }
154
155 final Frequency other = (Frequency) obj;
156 return this.frequency == other.frequency;
157 }
158
159 @Override
160 public String toString() {
161 return MoreObjects.toStringHelper(this)
162 .add("frequency", frequency + "Hz")
163 .toString();
164 }
165}