com.hazelcast.memory.MemorySize Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.memory;
/**
* MemorySize represents a memory size with given value and {@link MemoryUnit}
.
*
* @see MemoryUnit
* @see Capacity
* @since 3.4
*
* @deprecated Since 5.1, {@link Capacity} should be used instead.
*/
public final class MemorySize extends Capacity {
public MemorySize(long value) {
super(value, MemoryUnit.BYTES);
}
public MemorySize(long value, MemoryUnit unit) {
super(value, unit);
}
/**
* Parses string representation of a memory size value.
* Value may end with one of suffixes;
*
* - 'k' or 'K' for 'kilo',
* - 'm' or 'M' for 'mega',
* - 'g' or 'G' for 'giga'.
*
*
* Default unit is bytes.
*
* Examples:
* 12345, 12345m, 12345K, 123456G
*
* @deprecated since 5.1, use {@link Capacity#parse(String)} instead.
*/
public static MemorySize parse(String value) {
return parse(value, MemoryUnit.BYTES);
}
/**
* Parses string representation of a memory size value.
* Value may end with one of suffixes;
*
* - 'k' or 'K' for 'kilo',
* - 'm' or 'M' for 'mega',
* - 'g' or 'G' for 'giga'.
*
*
* Uses default unit if value does not contain unit information.
*
* Examples:
* 12345, 12345m, 12345K, 123456G
*
* @deprecated since 5.1, use {@link Capacity#parse(String, MemoryUnit)} instead
*/
public static MemorySize parse(String value, MemoryUnit defaultUnit) {
if (value == null || value.length() == 0) {
return new MemorySize(0, MemoryUnit.BYTES);
}
MemoryUnit unit = defaultUnit;
final char last = value.charAt(value.length() - 1);
if (!Character.isDigit(last)) {
value = value.substring(0, value.length() - 1);
switch (last) {
case 'g':
case 'G':
unit = MemoryUnit.GIGABYTES;
break;
case 'm':
case 'M':
unit = MemoryUnit.MEGABYTES;
break;
case 'k':
case 'K':
unit = MemoryUnit.KILOBYTES;
break;
default:
throw new IllegalArgumentException("Could not determine memory unit of " + value + last);
}
}
return new MemorySize(Long.parseLong(value), unit);
}
}