Skip to content

Commit

Permalink
Enhance type safety when getting tag values
Browse files Browse the repository at this point in the history
This removes all uses of `ReadableHeader::getValue` which returns
`Object` and replaces them with methods that return specific types.
This simplifies the code and eliminates the need for the caller to
cast the result.

The method `RpmBaseTag::getDataType` was added to store the tag's
data type. The tag's data type is checked when the various
`ReadableHeader` methods are called.
  • Loading branch information
dwalluck committed Apr 14, 2024
1 parent 454e760 commit b0cf632
Show file tree
Hide file tree
Showing 25 changed files with 643 additions and 511 deletions.
24 changes: 16 additions & 8 deletions rpm/src/main/java/org/eclipse/packager/rpm/ReadableHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@

package org.eclipse.packager.rpm;

import java.util.Optional;
import java.util.List;

public interface ReadableHeader<T extends RpmBaseTag> {
/**
* Get the value from a header structure
*
* @param tag the tag
* @return the optional value
*/
public Optional<Object> getValue(T tag);
boolean hasTag(T tag);

String getString(T tag);

Integer getInteger(T tag);

Long getLong(T tag);

List<String> getStringList(T tag);

List<Integer> getIntegerList(T tag);

List<Long> getLongList(T tag);

byte[] getByteArray(T tag);
}
11 changes: 10 additions & 1 deletion rpm/src/main/java/org/eclipse/packager/rpm/RpmBaseTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@ public interface RpmBaseTag {
*
* @return the key value
*/
public Integer getValue();
Integer getValue();

/**
* Get the data type of the tag.
*
* @param <E> the data type
* @return the class representing the data type of this tag
*/
<E> Class<E> getDataType();
}

40 changes: 27 additions & 13 deletions rpm/src/main/java/org/eclipse/packager/rpm/RpmSignatureTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,38 @@
import java.util.Map;

public enum RpmSignatureTag implements RpmBaseTag {
PUBKEYS(266),
DSAHEADER(267),
RSAHEADER(268),
SHA1HEADER(269),
LONGARCHIVESIZE(271),
SHA256HEADER(273),

SIZE(1000),
PGP(1002),
MD5(1004),
PAYLOAD_SIZE(1007),
LONGSIZE(5009);
PUBKEYS(266, String[].class),
DSAHEADER(267, byte[].class),
RSAHEADER(268, byte[].class),
SHA1HEADER(269, String.class),
LONGARCHIVESIZE(271, Long.class),
SHA256HEADER(273, String.class),

SIZE(1000, Integer.class),
PGP(1002, byte[].class),
MD5(1004, byte[].class),
PAYLOAD_SIZE(1007, Integer.class),
LONGSIZE(5009, Long.class);

private final Integer value;

RpmSignatureTag(final Integer value) {
private final Class<?> dataType;

<T> RpmSignatureTag(final Integer value, final Class<T> dataType) {
this.value = value;
this.dataType = dataType;
}

@Override
public Integer getValue() {
return this.value;
}

@Override
public <E> Class<E> getDataType() {
return (Class<E>) this.dataType;
}

private final static Map<Integer, RpmSignatureTag> all = new HashMap<>(RpmSignatureTag.values().length);

static {
Expand All @@ -52,4 +60,10 @@ public Integer getValue() {
public static RpmSignatureTag find(final Integer value) {
return all.get(value);
}

@Override
public String toString() {
RpmSignatureTag tag = find(this.value);
return dataType.getSimpleName() + " " + (tag != null ? tag.name() + "(" + this.value + ")" : "UNKNOWN(" + this.value + ")");
}
}
203 changes: 108 additions & 95 deletions rpm/src/main/java/org/eclipse/packager/rpm/RpmTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,113 +17,120 @@
import java.util.Map;

public enum RpmTag implements RpmBaseTag {
NAME(1000),
VERSION(1001),
RELEASE(1002),
EPOCH(1003),
SUMMARY(1004),
DESCRIPTION(1005),
BUILDTIME(1006),
BUILDHOST(1007),
SIZE(1009),
DISTRIBUTION(1010),
VENDOR(1011),
LICENSE(1014),
PACKAGER(1015),
GROUP(1016),
URL(1020),
OS(1021),
ARCH(1022),
PREINSTALL_SCRIPT(1023),
POSTINSTALL_SCRIPT(1024),
PREREMOVE_SCRIPT(1025),
POSTREMOVE_SCRIPT(1026),
FILE_SIZES(1028),
FILE_MODES(1030),
FILE_RDEVS(1033),
FILE_MTIMES(1034),
FILE_DIGESTS(1035),
FILE_LINKTO(1036),
FILE_FLAGS(1037),
FILE_USERNAME(1039),
FILE_GROUPNAME(1040),
SOURCE_PACKAGE(1044),
FILE_VERIFYFLAGS(1045),
ARCHIVE_SIZE(1046),
PROVIDE_NAME(1047),
REQUIRE_FLAGS(1048),
REQUIRE_NAME(1049),
REQUIRE_VERSION(1050),
CONFLICT_FLAGS(1053),
CONFLICT_NAME(1054),
CONFLICT_VERSION(1055),
RPMVERSION(1064),
TRIGGER_SCRIPTS(1065),
TRIGGER_NAME(1066),
TRIGGER_VERSION(1067),
TRIGGER_FLAGS(1068),
TRIGGER_INDEX(1069),
VERIFY_SCRIPT(1079),
CHANGELOG_TIMESTAMP(1080),
CHANGELOG_AUTHOR(1081),
CHANGELOG_TEXT(1082),
PREINSTALL_SCRIPT_PROG(1085),
POSTINSTALL_SCRIPT_PROG(1086),
PREREMOVE_SCRIPT_PROG(1087),
POSTREMOVE_SCRIPT_PROG(1088),
VERIFY_SCRIPT_PROG(1091),
TRIGGERSCRIPT_PROG(1092),
OBSOLETE_NAME(1090),
FILE_DEVICES(1095),
FILE_INODES(1096),
FILE_LANGS(1097),
PREFIXES(1098),
PROVIDE_FLAGS(1112),
PROVIDE_VERSION(1113),
OBSOLETE_FLAGS(1114),
OBSOLETE_VERSION(1115),
DIR_INDEXES(1116),
BASENAMES(1117),
DIRNAMES(1118),
OPTFLAGS(1122),
PAYLOAD_FORMAT(1124),
PAYLOAD_CODING(1125),
PAYLOAD_FLAGS(1126),
PLATFORM(1132),
PRETRANSACTION_SCRIPT(1151),
POSTTRANSACTION_SCRIPT(1152),
PRETRANSACTION_SCRIPT_PROG(1153),
POSTTRANSACTION_SCRIPT_PROG(1154),
LONGSIZE(5009),
FILE_DIGESTALGO(5011),
RECOMMEND_NAME(5046),
RECOMMEND_VERSION(5047),
RECOMMEND_FLAGS(5048),
SUGGEST_NAME(5049),
SUGGEST_VERSION(5050),
SUGGEST_FLAGS(5051),
SUPPLEMENT_NAME(5052),
SUPPLEMENT_VERSION(5053),
SUPPLEMENT_FLAGS(5054),
ENHANCE_NAME(5055),
ENHANCE_VERSION(5056),
ENHANCE_FLAGS(5057),
NAME(1000, String.class),
VERSION(1001, String.class),
RELEASE(1002, String.class),
EPOCH(1003, Integer.class),
SUMMARY(1004, String[].class),
DESCRIPTION(1005, String[].class),
BUILDTIME(1006, Integer.class),
BUILDHOST(1007, String.class),
SIZE(1009, Integer.class),
DISTRIBUTION(1010, String.class),
VENDOR(1011, String.class),
LICENSE(1014, String.class),
PACKAGER(1015, String.class),
GROUP(1016, String[].class),
URL(1020, String.class),
OS(1021, String.class),
ARCH(1022, String.class),
PREINSTALL_SCRIPT(1023, String.class),
POSTINSTALL_SCRIPT(1024, String.class),
PREREMOVE_SCRIPT(1025, String.class),
POSTREMOVE_SCRIPT(1026, String.class),
FILE_SIZES(1028, Integer[].class),
FILE_MODES(1030, Short[].class),
FILE_RDEVS(1033, Short[].class),
FILE_MTIMES(1034, Integer[].class),
FILE_DIGESTS(1035, String[].class),
FILE_LINKTO(1036, String[].class),
FILE_FLAGS(1037, Integer[].class),
FILE_USERNAME(1039, String[].class),
FILE_GROUPNAME(1040, String[].class),
SOURCE_PACKAGE(1044, String.class),
FILE_VERIFYFLAGS(1045, Integer[].class),
ARCHIVE_SIZE(1046, Integer.class),
PROVIDE_NAME(1047, String[].class),
REQUIRE_FLAGS(1048, Integer[].class),
REQUIRE_NAME(1049, String[].class),
REQUIRE_VERSION(1050, String[].class),
CONFLICT_FLAGS(1053, Integer[].class),
CONFLICT_NAME(1054, String[].class),
CONFLICT_VERSION(1055, String[].class),
RPMVERSION(1064, String.class),
TRIGGER_SCRIPTS(1065, String[].class),
TRIGGER_NAME(1066, String[].class),
TRIGGER_VERSION(1067, String[].class),
TRIGGER_FLAGS(1068, Integer[].class),
TRIGGER_INDEX(1069, Integer[].class),
VERIFY_SCRIPT(1079, String.class),
CHANGELOG_TIMESTAMP(1080, Integer[].class),
CHANGELOG_AUTHOR(1081, String[].class),
CHANGELOG_TEXT(1082, String[].class),
PREINSTALL_SCRIPT_PROG(1085, String[].class),
POSTINSTALL_SCRIPT_PROG(1086, String[].class),
PREREMOVE_SCRIPT_PROG(1087, String[].class),
POSTREMOVE_SCRIPT_PROG(1088, String[].class),
OBSOLETE_NAME(1090, String[].class),
VERIFY_SCRIPT_PROG(1091, String[].class),
TRIGGERSCRIPT_PROG(1092, String[].class),
FILE_DEVICES(1095, Integer[].class),
FILE_INODES(1096, Integer[].class),
FILE_LANGS(1097, String[].class),
PREFIXES(1098, String[].class),
PROVIDE_FLAGS(1112, Integer[].class),
PROVIDE_VERSION(1113, String[].class),
OBSOLETE_FLAGS(1114, Integer[].class),
OBSOLETE_VERSION(1115, String[].class),
DIR_INDEXES(1116, Integer.class),
BASENAMES(1117, String[].class),
DIRNAMES(1118, String[].class),
OPTFLAGS(1122, String.class),
PAYLOAD_FORMAT(1124, String.class),
PAYLOAD_CODING(1125, String.class),
PAYLOAD_FLAGS(1126, String.class),
PLATFORM(1132, String.class),
PRETRANSACTION_SCRIPT(1151, String.class),
POSTTRANSACTION_SCRIPT(1152, String.class),
PRETRANSACTION_SCRIPT_PROG(1153, String[].class),
POSTTRANSACTION_SCRIPT_PROG(1154, String[].class),
LONGSIZE(5009, Long.class),
FILE_DIGESTALGO(5011, Integer.class),
RECOMMEND_NAME(5046, String[].class),
RECOMMEND_VERSION(5047, String[].class),
RECOMMEND_FLAGS(5048, Integer[].class),
SUGGEST_NAME(5049, String[].class),
SUGGEST_VERSION(5050, String[].class),
SUGGEST_FLAGS(5051, Integer[].class),
SUPPLEMENT_NAME(5052, String[].class),
SUPPLEMENT_VERSION(5053, String[].class),
SUPPLEMENT_FLAGS(5054, Integer[].class),
ENHANCE_NAME(5055, String[].class),
ENHANCE_VERSION(5056, String[].class),
ENHANCE_FLAGS(5057, Integer[].class),

PAYLOAD_DIGEST(5092),
PAYLOAD_DIGEST_ALGO(5093),
PAYLOAD_DIGEST_ALT(5097);
PAYLOAD_DIGEST(5092, String[].class),
PAYLOAD_DIGEST_ALGO(5093, Integer.class),
PAYLOAD_DIGEST_ALT(5097, String[].class);

private final Integer value;

RpmTag(final Integer value) {
private final Class<?> dataType;

<T> RpmTag(final Integer value, Class<T> dataType) {
this.value = value;
this.dataType = dataType;
}

@Override
public Integer getValue() {
return this.value;
}

public <E> Class<E> getDataType() {
return (Class<E>) this.dataType;
}

private final static Map<Integer, RpmTag> all = new HashMap<>(RpmTag.values().length);

static {
Expand All @@ -135,4 +142,10 @@ public Integer getValue() {
public static RpmTag find(final Integer value) {
return all.get(value);
}

@Override
public String toString() {
RpmTag tag = find(this.value);
return dataType.getSimpleName() + " " + (tag != null ? tag.name() + "(" + this.value + ")" : "UNKNOWN(" + this.value + ")");
}
}
Loading

0 comments on commit b0cf632

Please sign in to comment.