001/** 002 * Copyright (C) 2015 Typesafe Inc. <http://typesafe.com> 003 */ 004package com.typesafe.config; 005 006/** 007 * An immutable class representing an amount of memory. Use 008 * static factory methods such as {@link 009 * ConfigMemorySize#ofBytes(long)} to create instances. 010 * 011 * @since 1.3.0 012 */ 013public final class ConfigMemorySize { 014 private final long bytes; 015 016 private ConfigMemorySize(long bytes) { 017 if (bytes < 0) 018 throw new IllegalArgumentException("Attempt to construct ConfigMemorySize with negative number: " + bytes); 019 this.bytes = bytes; 020 } 021 022 /** 023 * Constructs a ConfigMemorySize representing the given 024 * number of bytes. 025 * @since 1.3.0 026 * @param bytes a number of bytes 027 * @return an instance representing the number of bytes 028 */ 029 public static ConfigMemorySize ofBytes(long bytes) { 030 return new ConfigMemorySize(bytes); 031 } 032 033 /** 034 * Gets the size in bytes. 035 * @since 1.3.0 036 * @return how many bytes 037 */ 038 public long toBytes() { 039 return bytes; 040 } 041 042 @Override 043 public String toString() { 044 return "ConfigMemorySize(" + bytes + ")"; 045 } 046 047 @Override 048 public boolean equals(Object other) { 049 if (other instanceof ConfigMemorySize) { 050 return ((ConfigMemorySize)other).bytes == this.bytes; 051 } else { 052 return false; 053 } 054 } 055 056 @Override 057 public int hashCode() { 058 // in Java 8 this can become Long.hashCode(bytes) 059 return Long.valueOf(bytes).hashCode(); 060 } 061 062} 063