mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
Add ConfigMemorySize type
Not used at all yet, and with no unit conversions supported.
This commit is contained in:
parent
1f50c19b59
commit
287e927129
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (C) 2015 Typesafe Inc. <http://typesafe.com>
|
||||
*/
|
||||
package com.typesafe.config;
|
||||
|
||||
/**
|
||||
* An immutable class representing an amount of memory. Use
|
||||
* static factory methods such as {@link
|
||||
* ConfigMemorySize#ofBytes(long)} to create instances.
|
||||
*/
|
||||
public final class ConfigMemorySize {
|
||||
private final long bytes;
|
||||
|
||||
private ConfigMemorySize(long bytes) {
|
||||
if (bytes < 0)
|
||||
throw new IllegalArgumentException("Attempt to construct ConfigMemorySize with negative number: " + bytes);
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
/** Constructs a ConfigMemorySize representing the given
|
||||
* number of bytes.
|
||||
*/
|
||||
public static ConfigMemorySize ofBytes(long bytes) {
|
||||
return new ConfigMemorySize(bytes);
|
||||
}
|
||||
|
||||
/** Gets the size in bytes.
|
||||
*/
|
||||
public long toBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other instanceof ConfigMemorySize) {
|
||||
return ((ConfigMemorySize)other).bytes == this.bytes;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// in Java 8 this can become Long.hashCode(bytes)
|
||||
return Long.valueOf(bytes).hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (C) 2015 Typesafe Inc. <http://typesafe.com>
|
||||
*/
|
||||
package com.typesafe.config.impl
|
||||
|
||||
import org.junit.Assert._
|
||||
import org.junit._
|
||||
import com.typesafe.config.ConfigMemorySize
|
||||
|
||||
class ConfigMemorySizeTest extends TestUtils {
|
||||
|
||||
@Test
|
||||
def testEquals() {
|
||||
assertTrue("Equal ConfigMemorySize are equal",
|
||||
ConfigMemorySize.ofBytes(10).equals(ConfigMemorySize.ofBytes(10)))
|
||||
assertTrue("Different ConfigMemorySize are not equal",
|
||||
!ConfigMemorySize.ofBytes(10).equals(ConfigMemorySize.ofBytes(11)))
|
||||
}
|
||||
|
||||
@Test
|
||||
def testToUnits() {
|
||||
val kilobyte = ConfigMemorySize.ofBytes(1024)
|
||||
assertEquals(1024, kilobyte.toBytes)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user