0e464253512a61f60f22db3ff46dc78b732926ef
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / memory / MemorySize.java
1 /*
2  * Copyright 2018-present Open Networking Foundation
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  */
16 package io.atomix.utils.memory;
17
18 import static com.google.common.base.MoreObjects.toStringHelper;
19
20 /**
21  * Memory size.
22  */
23 public class MemorySize {
24
25   /**
26    * Creates a memory size from the given bytes.
27    *
28    * @param bytes the number of bytes
29    * @return the memory size
30    */
31   public static MemorySize from(long bytes) {
32     return new MemorySize(bytes);
33   }
34
35   private final long bytes;
36
37   public MemorySize(long bytes) {
38     this.bytes = bytes;
39   }
40
41   /**
42    * Returns the number of bytes.
43    *
44    * @return the number of bytes
45    */
46   public long bytes() {
47     return bytes;
48   }
49
50   @Override
51   public int hashCode() {
52     return Long.valueOf(bytes).hashCode();
53   }
54
55   @Override
56   public boolean equals(Object object) {
57     return object instanceof MemorySize && ((MemorySize) object).bytes == bytes;
58   }
59
60   @Override
61   public String toString() {
62     return toStringHelper(this)
63         .addValue(bytes)
64         .toString();
65   }
66 }