Add ConfigMemorySize type

Not used at all yet, and with no unit conversions supported.
This commit is contained in:
Havoc Pennington 2015-02-24 17:45:52 -05:00
parent 1f50c19b59
commit 287e927129
2 changed files with 74 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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)
}
}