Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions collections/src/main/java/io/aiven/commons/collections/Scale.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,23 @@ public enum Scale {
*/
PiB(TiB.bytes * KiB.bytes);

/**
/**
* The International Electrotechnical Commission (IEC) standardized binary
* prefixes. Developed by the IEC to avoid ambiguity through their similarity to
* the standard metric terms. These are based on powers of 2.
*
* @see <a href='https://www.iec.ch/prefixes-binary-multiples'>IEC prefixes for
* binary multiples</a>.
*/
public static final List<Scale> IEC = Arrays.asList(KiB, MiB, GiB, TiB, PiB);

/**
* The SI standardized prefix scales. These are the metric units, as such they
* are all powers of 10.
*/
public static final List<Scale> SI = Arrays.asList(KB, MB, GB, TB, PB);

/**
* The format used to output the values.
*/
final DecimalFormat dec = new DecimalFormat("0.0 ");
Expand All @@ -88,7 +104,7 @@ public enum Scale {
* @param bytes
* the number of bytes in a single unit of the scale.
*/
Scale(long bytes) {
Scale(final long bytes) {
this.bytes = bytes;
}

Expand All @@ -100,7 +116,7 @@ public enum Scale {
* @return A string representing the number of units that comprise the
* {@code byteCount}.
*/
public String format(long byteCount) {
public String format(final long byteCount) {
return dec.format(byteCount * 1.0 / bytes).concat(this.name());
}

Expand All @@ -111,7 +127,7 @@ public String format(long byteCount) {
* the number of units at this scale.
* @return A string representing the number of units at this scale.
*/
public String units(int unitCount) {
public String units(final int unitCount) {
return dec.format(unitCount).concat(this.name());
}

Expand All @@ -122,7 +138,7 @@ public String units(int unitCount) {
* the number of units.
* @return the number of bytes in {@code unitCount} units of this scale.
*/
public long asBytes(double unitCount) {
public long asBytes(final double unitCount) {
return (long) unitCount * bytes;
}

Expand All @@ -138,7 +154,7 @@ public long asBytes(double unitCount) {
* the list of possible scales.
* @return the first matching scale.
*/
public static Scale scaleOf(long byteCount, List<Scale> possibleScales) {
public static Scale scaleOf(final long byteCount, final List<Scale> possibleScales) {
final List<Scale> ordered = new ArrayList<>(possibleScales);
// sort descending size.
ordered.sort((a, b) -> Long.compare(b.bytes, a.bytes));
Expand All @@ -163,24 +179,7 @@ public static Scale scaleOf(long byteCount, List<Scale> possibleScales) {
* best Scale representation.
* @see #scaleOf(long, List)
*/
public static String size(int byteCount, List<Scale> possibleScales) {
public static String size(final int byteCount, final List<Scale> possibleScales) {
return scaleOf(byteCount, possibleScales).format(byteCount);
}

/**
* The International Electrotechnical Commission (IEC) standardized binary
* prefixes. Developed by the IEC to avoid ambiguity through their similarity to
* the standard metric terms. These are based on powers of 2.
*
* @see <a href='https://www.iec.ch/prefixes-binary-multiples'>IEC prefixes for
* binary multiples</a>.
*/
public static final List<Scale> IEC = Arrays.asList(KiB, MiB, GiB, TiB, PiB);

/**
* The SI standardized prefix scales. These are the metric units, as such they
* are all powers of 10.
*/
public static final List<Scale> SI = Arrays.asList(KB, MB, GB, TB, PB);

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public class ScaleValidator implements ConfigDef.Validator {
* @param possibleScales
* the potential scales for display.
*/
ScaleValidator(final Number minBytes, final Number maxBytes, List<Scale> possibleScales) {
ScaleValidator(final Number minBytes, final Number maxBytes, final List<Scale> possibleScales) {
this.minBytes = minBytes;
this.minScale = Scale.scaleOf(minBytes.longValue(), possibleScales);
this.minScale = minBytes == null ? Scale.B : Scale.scaleOf(minBytes.longValue(), possibleScales);
this.maxBytes = maxBytes;
this.maxScale = Scale.scaleOf(maxBytes.longValue(), possibleScales);
this.maxScale = maxBytes == null ? Scale.B : Scale.scaleOf(maxBytes.longValue(), possibleScales);
}

/**
Expand All @@ -69,7 +69,7 @@ public class ScaleValidator implements ConfigDef.Validator {
* @param possibleScales
* The list of potential scale values.
*/
public static ScaleValidator atLeast(Number min, List<Scale> possibleScales) {
public static ScaleValidator atLeast(final Number min, final List<Scale> possibleScales) {
return new ScaleValidator(min, null, possibleScales);
}

Expand All @@ -83,19 +83,19 @@ public static ScaleValidator atLeast(Number min, List<Scale> possibleScales) {
* @param possibleScales
* The list of potential scale values.
*/
public static ScaleValidator between(Number min, Number max, List<Scale> possibleScales) {
public static ScaleValidator between(final Number min, final Number max, final List<Scale> possibleScales) {
return new ScaleValidator(min, max, possibleScales);
}

@Override
public void ensureValid(String name, Object o) {
if (o == null)
public void ensureValid(final String name, final Object value) {
if (value == null)
throw new ConfigException(name, null, "Value must be non-null");
Number n = (Number) o;
if (minBytes != null && n.longValue() < minBytes.longValue())
throw new ConfigException(name, o, "Value must be at least " + minScale.format(minBytes.longValue()));
if (maxBytes != null && n.doubleValue() > maxBytes.doubleValue())
throw new ConfigException(name, o, "Value must be no more than " + maxScale.format(maxBytes.longValue()));
final Number number = (Number) value;
if (minBytes != null && number.longValue() < minBytes.longValue())
throw new ConfigException(name, value, "Value must be at least " + minScale.format(minBytes.longValue()));
if (maxBytes != null && number.doubleValue() > maxBytes.doubleValue())
throw new ConfigException(name, value, "Value must be no more than " + maxScale.format(maxBytes.longValue()));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>io.aiven.commons</groupId>
<artifactId>aiven-commons</artifactId>
<version>1</version>
<version>2-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Aiven Common Utilities</name>
Expand Down