Created
January 6, 2020 04:45
-
-
Save dfparker2002/e1c13def045a2b6f2ec21c9324a1d4c2 to your computer and use it in GitHub Desktop.
Break out sling value properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/10d9603878e4d534144aad8196af804652de67d3/bundle/src/main/java/com/adobe/acs/commons/mcp/model/ValueFormat.java | |
/* | |
* #%L | |
* ACS AEM Commons Bundle | |
* %% | |
* Copyright (C) 2017 Adobe | |
* %% | |
* 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. | |
* #L% | |
*/ | |
import java.lang.reflect.Field; | |
import java.util.function.Function; | |
/** | |
* Describes the desired output format of a value within a report | |
*/ | |
@SuppressWarnings({"checkstyle:constantname", "squid:S00115"}) | |
public enum ValueFormat { | |
plain, storageSize("_short", ValueFormat::getHumanSize); | |
int columnCount = 1; | |
String suffix = ""; | |
Function<Object, Object> altFunction; | |
ValueFormat() { | |
} | |
ValueFormat(String alternate, Function<Object, Object> altFunc) { | |
columnCount = 2; | |
suffix = alternate; | |
altFunction = altFunc; | |
} | |
public static ValueFormat forField(Enum anEnum) { | |
try { | |
Field field = anEnum.getDeclaringClass().getField(anEnum.name()); | |
if (!field.isAnnotationPresent(FieldFormat.class)) { | |
return ValueFormat.plain; | |
} else { | |
FieldFormat rf = field.getAnnotation(FieldFormat.class); | |
return rf.value(); | |
} | |
} catch (IllegalArgumentException | NoSuchFieldException | SecurityException ex) { | |
// Ignore errors, assume plain | |
} | |
return ValueFormat.plain; | |
} | |
public Object getAlternateValue(Object val) { | |
return altFunction.apply(val); | |
} | |
public static String getHumanSize(Object val) { | |
if (val == null) { | |
return null; | |
} | |
Long bytes = (val instanceof Long) ? (Long) val : Long.parseLong(String.valueOf(val)); | |
return humanReadableByteCount(bytes, false); | |
} | |
// From: https://programming.guide/worlds-most-copied-so-snippet.html | |
public static strictfp String humanReadableByteCount(long bytes, boolean si) { | |
int unit = si ? 1000 : 1024; | |
long absBytes = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); | |
if (absBytes < unit) { | |
return bytes + " B"; | |
} | |
int exp = (int) (Math.log(absBytes) / Math.log(unit)); | |
long th = (long) (Math.pow(unit, exp) * (unit - 0.05)); | |
if (exp < 6 && absBytes >= th - ((th & 0xfff) == 0xd00 ? 52 : 0)) { | |
exp++; | |
} | |
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); | |
if (exp > 4) { | |
bytes /= unit; | |
exp -= 1; | |
} | |
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); | |
} | |
} | |
// test | |
// src: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/10d9603878e4d534144aad8196af804652de67d3/bundle/src/test/java/com/adobe/acs/commons/mcp/model/ValueFormatTest.java | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* Basic sanity check of value formats in MCP | |
*/ | |
public class ValueFormatTest { | |
private static String ONE = String.format("%.1f", 1f); | |
enum FieldEnum { | |
@FieldFormat(ValueFormat.plain) | |
somePlainField, | |
@FieldFormat(ValueFormat.storageSize) | |
someStorageSizeField, | |
someUnannotatedField | |
} | |
@Test | |
public void testDefinedAnnotations() { | |
assertEquals(ValueFormat.plain, ValueFormat.forField(FieldEnum.somePlainField)); | |
assertEquals(ValueFormat.storageSize, ValueFormat.forField(FieldEnum.someStorageSizeField)); | |
} | |
@Test | |
public void testUndefinedAnnotations() { | |
assertEquals(ValueFormat.plain, ValueFormat.forField(FieldEnum.someUnannotatedField)); | |
} | |
@Test | |
public void oneKb() { | |
assertEquals(ONE + " KiB", ValueFormat.getHumanSize(1 << 10)); | |
} | |
@Test | |
public void oneMb() { | |
assertEquals(ONE + " MiB", ValueFormat.getHumanSize(1 << 20)); | |
} | |
@Test | |
public void oneGb() { | |
assertEquals(ONE + " GiB", ValueFormat.getHumanSize(1 << 30)); | |
} | |
@Test | |
public void onetb() { | |
assertEquals(ONE + " TiB", ValueFormat.getHumanSize(((long) (1 << 30)) * 1024L)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment