Skip to content

Commit d0817b5

Browse files
youngledothegreystone
authored andcommitted
8490: Include numeric types without Persister in "Add Filter from Attribute" menu
Reviewed-by: hirt
1 parent a57d665 commit d0817b5

3 files changed

Lines changed: 375 additions & 39 deletions

File tree

core/org.openjdk.jmc.common/src/main/java/org/openjdk/jmc/common/unit/QuantityConversionException.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
33
*
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -213,41 +213,31 @@ public String getInteractivePrototype() {
213213
}
214214
}
215215

216-
/*
217-
* NOTE: The type parameter T doesn't strictly need to extend Comparable<T>, particularly not
218-
* for UNPARSEABLE. For the other cases, it seemed possible that they could be used where being
219-
* Comparable would be beneficial.
220-
*/
221-
public static <T extends Comparable<T>> QuantityConversionException unparsable(
222-
String badString, T prototype, IPersister<T> persister) {
216+
public static <T> QuantityConversionException unparsable(String badString, T prototype, IPersister<T> persister) {
223217
return new Persisted(Problem.UNPARSEABLE, badString, prototype, persister);
224218
}
225219

226-
public static <T extends Comparable<T>> QuantityConversionException noUnit(
227-
String badString, T prototype, IPersister<T> persister) {
220+
public static <T> QuantityConversionException noUnit(String badString, T prototype, IPersister<T> persister) {
228221
return new Persisted(Problem.NO_UNIT, badString, prototype, persister);
229222
}
230223

231-
public static <T extends Comparable<T>> QuantityConversionException unknownUnit(
232-
String badString, T prototype, IPersister<T> persister) {
224+
public static <T> QuantityConversionException unknownUnit(String badString, T prototype, IPersister<T> persister) {
233225
return new Persisted(Problem.UNKNOWN_UNIT, badString, prototype, persister);
234226
}
235227

236-
public static <T extends Comparable<T>> QuantityConversionException tooLow(
237-
T badValue, T min, IPersister<T> persister) {
228+
public static <T> QuantityConversionException tooLow(T badValue, T min, IPersister<T> persister) {
238229
return new Persisted(Problem.TOO_LOW, badValue, min, persister);
239230
}
240231

241-
public static <T extends Comparable<T>> QuantityConversionException tooHigh(
242-
T badValue, T max, IPersister<T> persister) {
232+
public static <T> QuantityConversionException tooHigh(T badValue, T max, IPersister<T> persister) {
243233
return new Persisted(Problem.TOO_HIGH, badValue, max, persister);
244234
}
245235

246236
/*
247237
* FIXME: This currently reports that the value is "below precision". Replace precisionLimit
248238
* with a closest valid quantity (and change the problem message)?
249239
*/
250-
public static <T extends Comparable<T>> QuantityConversionException belowPrecision(
240+
public static <T> QuantityConversionException belowPrecision(
251241
T badValue, T precisionLimit, IPersister<T> persister) {
252242
return new Persisted(Problem.TOO_SMALL_MAGNITUDE, badValue, precisionLimit, persister);
253243
}

core/org.openjdk.jmc.common/src/main/java/org/openjdk/jmc/common/unit/UnitLookup.java

Lines changed: 210 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
33
*
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -91,14 +91,9 @@ final public class UnitLookup {
9191
private static final String UNIT_ID_SEPARATOR = ":";
9292
public static final LinearKindOfQuantity MEMORY = createMemory();
9393
public static final LinearKindOfQuantity TIMESPAN = createTimespan();
94-
/*
95-
* NOTE: These 3 (count, index, and identifier) cannot be persisted/restored due to Long(1) and
96-
* Integer(1) not being equal or comparable. We either need to split into concrete wrappers,
97-
* support a custom Comparator, or wrap into a (simple) IQuantity.
98-
*/
99-
public static final ContentType<Number> COUNT = createCount();
100-
public static final ContentType<Number> INDEX = createIndex();
101-
public static final ContentType<Number> IDENTIFIER = createIdentifier();
94+
public static final ContentType<Long> COUNT = createCount();
95+
public static final ContentType<Long> INDEX = createIndex();
96+
public static final ContentType<Long> IDENTIFIER = createIdentifier();
10297
public static final KindOfQuantity<TimestampUnit> TIMESTAMP = createTimestamp(TIMESPAN);
10398
public static final LinearKindOfQuantity PERCENTAGE = createPercentage();
10499
public static final LinearKindOfQuantity NUMBER = createNumber();
@@ -426,15 +421,99 @@ private static String formatHexNumber(IQuantity quantity) {
426421
return String.format("0x%08X", quantity.longValue());
427422
}
428423

424+
private static Number parseNumber(String numberStr) {
425+
try {
426+
return Long.parseLong(numberStr);
427+
} catch (NumberFormatException eLong) {
428+
return Double.parseDouble(numberStr);
429+
}
430+
}
431+
429432
// FIXME: Rename to createPrimitiveNumber? Remove?
430433
private static ContentType<Number> createRawNumber() {
431-
ContentType<Number> contentType = new ContentType<>("raw number");
434+
ContentType<Number> contentType = new LeafContentType<Number>("raw number") {
435+
@Override
436+
public boolean validate(Number value) {
437+
checkNull(value);
438+
return false;
439+
}
440+
441+
@Override
442+
public String persistableString(Number value) {
443+
validate(value);
444+
return value.toString();
445+
}
446+
447+
@Override
448+
public Number parsePersisted(String persistedValue) throws QuantityConversionException {
449+
checkNull(persistedValue);
450+
try {
451+
return parseNumber(persistedValue);
452+
} catch (NumberFormatException e) {
453+
throw QuantityConversionException.unparsable(persistedValue, 0, this);
454+
}
455+
}
456+
457+
@Override
458+
public String interactiveFormat(Number value) {
459+
validate(value);
460+
return value.toString();
461+
}
462+
463+
@Override
464+
public Number parseInteractive(String interactiveValue) throws QuantityConversionException {
465+
checkNull(interactiveValue);
466+
try {
467+
return parseNumber(interactiveValue);
468+
} catch (NumberFormatException e) {
469+
throw QuantityConversionException.unparsable(interactiveValue, 0, this);
470+
}
471+
}
472+
};
432473
contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value"));
433474
return contentType;
434475
}
435476

436477
private static ContentType<Long> createRawLong() {
437-
ContentType<Long> contentType = new ContentType<>("raw long");
478+
ContentType<Long> contentType = new LeafContentType<Long>("raw long") {
479+
@Override
480+
public boolean validate(Long value) {
481+
checkNull(value);
482+
return false;
483+
}
484+
485+
@Override
486+
public String persistableString(Long value) {
487+
validate(value);
488+
return value.toString();
489+
}
490+
491+
@Override
492+
public Long parsePersisted(String persistedValue) throws QuantityConversionException {
493+
checkNull(persistedValue);
494+
try {
495+
return Long.parseLong(persistedValue);
496+
} catch (NumberFormatException e) {
497+
throw QuantityConversionException.unparsable(persistedValue, 0L, this);
498+
}
499+
}
500+
501+
@Override
502+
public String interactiveFormat(Long value) {
503+
validate(value);
504+
return value.toString();
505+
}
506+
507+
@Override
508+
public Long parseInteractive(String interactiveValue) throws QuantityConversionException {
509+
checkNull(interactiveValue);
510+
try {
511+
return Long.parseLong(interactiveValue);
512+
} catch (NumberFormatException e) {
513+
throw QuantityConversionException.unparsable(interactiveValue, 0L, this);
514+
}
515+
}
516+
};
438517
contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value"));
439518
return contentType;
440519
}
@@ -568,25 +647,134 @@ private static LinearKindOfQuantity createPercentage() {
568647
return percentage;
569648
}
570649

571-
private static ContentType<Number> createCount() {
572-
ContentType<Number> contentType = new ContentType<>("count");
573-
// contentType.addDisplayUnit(
574-
// new DisplayUnit(contentType, DisplayUnit.ENGINEERING_NOTATION_IDENTIFIER, "Engineering Notation"));
575-
contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value"));
650+
private static ContentType<Long> createCount() {
651+
ContentType<Long> contentType = new LeafContentType<Long>("count") {
652+
@Override
653+
public boolean validate(Long value) {
654+
checkNull(value);
655+
return false;
656+
}
657+
658+
@Override
659+
public String persistableString(Long value) {
660+
validate(value);
661+
return value.toString();
662+
}
663+
664+
@Override
665+
public Long parsePersisted(String persistedValue) throws QuantityConversionException {
666+
checkNull(persistedValue);
667+
try {
668+
return Long.parseLong(persistedValue);
669+
} catch (NumberFormatException e) {
670+
throw QuantityConversionException.unparsable(persistedValue, 0L, this);
671+
}
672+
}
673+
674+
@Override
675+
public String interactiveFormat(Long value) {
676+
validate(value);
677+
return value.toString();
678+
}
576679

577-
// contentType.addDisplayUnit(
578-
// new DisplayUnit(contentType, DisplayUnit.SCIENTIFIC_NOTATION_IDENTIFIER, "Scientific Notation"));
680+
@Override
681+
public Long parseInteractive(String interactiveValue) throws QuantityConversionException {
682+
checkNull(interactiveValue);
683+
try {
684+
return Long.parseLong(interactiveValue);
685+
} catch (NumberFormatException e) {
686+
throw QuantityConversionException.unparsable(interactiveValue, 0L, this);
687+
}
688+
}
689+
};
690+
contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value"));
579691
return contentType;
580692
}
581693

582-
private static ContentType<Number> createIdentifier() {
583-
ContentType<Number> contentType = new ContentType<>("identifier");
694+
private static ContentType<Long> createIdentifier() {
695+
ContentType<Long> contentType = new LeafContentType<Long>("identifier") {
696+
@Override
697+
public boolean validate(Long value) {
698+
checkNull(value);
699+
return false;
700+
}
701+
702+
@Override
703+
public String persistableString(Long value) {
704+
validate(value);
705+
return value.toString();
706+
}
707+
708+
@Override
709+
public Long parsePersisted(String persistedValue) throws QuantityConversionException {
710+
checkNull(persistedValue);
711+
try {
712+
return Long.parseLong(persistedValue);
713+
} catch (NumberFormatException e) {
714+
throw QuantityConversionException.unparsable(persistedValue, 0L, this);
715+
}
716+
}
717+
718+
@Override
719+
public String interactiveFormat(Long value) {
720+
validate(value);
721+
return value.toString();
722+
}
723+
724+
@Override
725+
public Long parseInteractive(String interactiveValue) throws QuantityConversionException {
726+
checkNull(interactiveValue);
727+
try {
728+
return Long.parseLong(interactiveValue);
729+
} catch (NumberFormatException e) {
730+
throw QuantityConversionException.unparsable(interactiveValue, 0L, this);
731+
}
732+
}
733+
};
584734
contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value"));
585735
return contentType;
586736
}
587737

588-
private static ContentType<Number> createIndex() {
589-
ContentType<Number> contentType = new ContentType<>("index");
738+
private static ContentType<Long> createIndex() {
739+
ContentType<Long> contentType = new LeafContentType<Long>("index") {
740+
@Override
741+
public boolean validate(Long value) {
742+
checkNull(value);
743+
return false;
744+
}
745+
746+
@Override
747+
public String persistableString(Long value) {
748+
validate(value);
749+
return value.toString();
750+
}
751+
752+
@Override
753+
public Long parsePersisted(String persistedValue) throws QuantityConversionException {
754+
checkNull(persistedValue);
755+
try {
756+
return Long.parseLong(persistedValue);
757+
} catch (NumberFormatException e) {
758+
throw QuantityConversionException.unparsable(persistedValue, 0L, this);
759+
}
760+
}
761+
762+
@Override
763+
public String interactiveFormat(Long value) {
764+
validate(value);
765+
return value.toString();
766+
}
767+
768+
@Override
769+
public Long parseInteractive(String interactiveValue) throws QuantityConversionException {
770+
checkNull(interactiveValue);
771+
try {
772+
return Long.parseLong(interactiveValue);
773+
} catch (NumberFormatException e) {
774+
throw QuantityConversionException.unparsable(interactiveValue, 0L, this);
775+
}
776+
}
777+
};
590778
contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value"));
591779
return contentType;
592780
}

0 commit comments

Comments
 (0)