Skip to content

Commit

Permalink
Update in response to pull request comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Apr 15, 2024
1 parent b0cf632 commit ee04664
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 68 deletions.
3 changes: 1 addition & 2 deletions rpm/src/main/java/org/eclipse/packager/rpm/RpmBaseTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public interface RpmBaseTag {
/**
* 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();
Class<?> getDataType();
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public Integer getValue() {
}

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

private final static Map<Integer, RpmSignatureTag> all = new HashMap<>(RpmSignatureTag.values().length);
Expand Down
4 changes: 2 additions & 2 deletions rpm/src/main/java/org/eclipse/packager/rpm/RpmTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public Integer getValue() {
return this.value;
}

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

private final static Map<Integer, RpmTag> all = new HashMap<>(RpmTag.values().length);
Expand Down
44 changes: 22 additions & 22 deletions rpm/src/main/java/org/eclipse/packager/rpm/RpmTagValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public Optional<String> asString() {
}

if (this.value instanceof String[]) {
final String[] array = (String[]) this.value;
final String[] arr = (String[]) this.value;

if (array.length == 1) {
return Optional.of(array[0]);
if (arr.length > 0) {
return Optional.of(arr[0]);
} else {
return Optional.empty();
}

throw new IllegalArgumentException("Array contains more than one string value");
}

return Optional.empty();
Expand Down Expand Up @@ -139,13 +139,13 @@ public Optional<Integer> asInteger() {
}

if (this.value instanceof Integer[]) {
final Integer[] array = (Integer[]) this.value;
final Integer[] arr = (Integer[]) this.value;

if (array.length == 1) {
return Optional.of(array[0]);
if (arr.length > 0) {
return Optional.of(arr[0]);
} else {
return Optional.empty();
}

throw new IllegalArgumentException("Array contains more than one integer value");
}

return Optional.empty();
Expand All @@ -161,27 +161,27 @@ public Optional<Long> asLong() {
}

if (this.value instanceof Integer[]) {
final Integer[] array = (Integer[]) this.value;
final Integer[] arr = (Integer[]) this.value;

if (array.length == 1) {
return Optional.of(toLong(array[0]));
if (arr.length > 0) {
return Optional.of(toLong(arr[0]));
} else {
return Optional.empty();
}

throw new IllegalArgumentException("Array contains more than one integer value");
}

if (this.value instanceof Long) {
return Optional.of((Long) this.value);
}

if (this.value instanceof Long[]) {
final Long[] array = (Long[]) this.value;
final Long[] arr = (Long[]) this.value;

if (array.length == 1) {
return Optional.of(array[0]);
if (arr.length > 0) {
return Optional.of(arr[0]);
} else {
return Optional.empty();
}

throw new IllegalArgumentException("Array contains more than one long value");
}

return Optional.empty();
Expand Down Expand Up @@ -216,8 +216,8 @@ public String toString() {
}

if (this.value instanceof Object[]) {
final Object[] array = (Object[]) this.value;
return array.length == 1 ? Objects.toString(array[0]) : Arrays.toString((Object[]) this.value);
final Object[] arr = (Object[]) this.value;
return arr.length == 1 ? Objects.toString(arr[0]) : Arrays.toString((Object[]) this.value);
}

return Objects.toString(this.value);
Expand Down
2 changes: 1 addition & 1 deletion rpm/src/main/java/org/eclipse/packager/rpm/Rpms.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static String toHex(final byte[] data, final int offset, final int length
return sb.toString();
}

public static String dumpValue(final HeaderValue<?> value) {
public static String dumpValue(final HeaderValue value) {
final StringBuilder sb = new StringBuilder();
dumpValue(sb, value.getValue());
return sb.toString();
Expand Down
6 changes: 3 additions & 3 deletions rpm/src/main/java/org/eclipse/packager/rpm/app/Dumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ private static void dumpHeader(final String string, final InputHeader<? extends
System.out.format(string + "%n");
System.out.format("=================================%n");

Set<Entry<Integer, HeaderValue<?>>> data;
Set<Entry<Integer, HeaderValue>> data;
if (sorted) {
data = new TreeMap<>(header.getRawTags()).entrySet();
} else {
data = header.getRawTags().entrySet();
}

for (final Map.Entry<Integer, HeaderValue<?>> entry : data) {
for (final Map.Entry<Integer, HeaderValue> entry : data) {
final RpmBaseTag tag = func.apply(entry.getKey());
final HeaderValue<?> value = entry.getValue();
final HeaderValue value = entry.getValue();
System.out.format("%20s - %s%n", tag != null ? tag : entry.getKey(), Rpms.dumpValue(value));

if (entry.getKey() == IMMUTABLE_TAG_SIGNATURE || entry.getKey() == IMMUTABLE_TAG_HEADER) {
Expand Down
36 changes: 36 additions & 0 deletions rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,52 @@ private static void putDependencies(final Header<RpmTag> header, final Collectio
Header.putIntFields(header, deps, flagsTag, dep -> RpmDependencyFlags.encode(dep.getFlags()));
}

/**
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param header the RPM header
* @return the list of dependencies
*/
public static List<Dependency> getRequirements(final ReadableHeader<RpmTag> header) {
return getDependencies(header, RpmTag.REQUIRE_NAME, RpmTag.REQUIRE_VERSION, RpmTag.REQUIRE_FLAGS);
}

/**
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param header the RPM header
* @return the list of dependencies
*/
public static List<Dependency> getProvides(final ReadableHeader<RpmTag> header) {
return getDependencies(header, RpmTag.PROVIDE_NAME, RpmTag.PROVIDE_VERSION, RpmTag.PROVIDE_FLAGS);
}

/**
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param header the RPM header
* @return the list of dependencies
*/
public static List<Dependency> getConflicts(final ReadableHeader<RpmTag> header) {
return getDependencies(header, RpmTag.CONFLICT_NAME, RpmTag.CONFLICT_VERSION, RpmTag.CONFLICT_FLAGS);
}

/**
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param header the RPM header
* @return the list of dependencies
*/
public static List<Dependency> getObsoletes(final ReadableHeader<RpmTag> header) {
return getDependencies(header, RpmTag.OBSOLETE_NAME, RpmTag.OBSOLETE_VERSION, RpmTag.OBSOLETE_FLAGS);
}

/**
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param header the RPM header
* @return the list of dependencies
*/
public static List<Dependency> getSuggests(final ReadableHeader<RpmTag> header) {
return getDependencies(header, RpmTag.SUGGEST_NAME, RpmTag.SUGGEST_VERSION, RpmTag.SUGGEST_FLAGS);
}
Expand All @@ -105,6 +135,12 @@ public static List<Dependency> getSupplements(final ReadableHeader<RpmTag> heade
return getDependencies(header, RpmTag.SUPPLEMENT_NAME, RpmTag.SUPPLEMENT_VERSION, RpmTag.SUPPLEMENT_FLAGS);
}

/**
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param header the RPM header
* @return the list of dependencies
*/
public static List<Dependency> getEnhances(final ReadableHeader<RpmTag> header) {
return getDependencies(header, RpmTag.ENHANCE_NAME, RpmTag.ENHANCE_VERSION, RpmTag.ENHANCE_FLAGS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public final class RpmInformations {
private RpmInformations() {
}

/**
* Returns the RPM information for the given RPM input stream.
*
* <p><em>Note that since version 0.21.0 <code>IllegalArgumentException</code> is thrown if an error occurs while reading the RPM header.</em></p>
*
* @param in the RPM input stream
* @return the RPM information for the given RPM input stream
* @throws IOException if an error occurs while reading from the given RPM input stream
* @throws IllegalArgumentException if there are any problems reading the headers
*/
public static RpmInformation makeInformation(final RpmInputStream in) throws IOException {
final InputHeader<RpmTag> header = in.getPayloadHeader();
final InputHeader<RpmSignatureTag> signature = in.getSignatureHeader();
Expand Down
22 changes: 11 additions & 11 deletions rpm/src/main/java/org/eclipse/packager/rpm/parse/HeaderValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import org.eclipse.packager.rpm.Rpms;
import org.eclipse.packager.rpm.header.Type;

public class HeaderValue<T> {
public class HeaderValue {
private final int tag;

private RpmTagValue<T> value;
private RpmTagValue<?> value;

private final int originalType;

Expand All @@ -48,7 +48,7 @@ public int getTag() {
return this.tag;
}

public RpmTagValue<T> getValue() {
public RpmTagValue<?> getValue() {
return this.value;
}

Expand All @@ -75,30 +75,30 @@ void fillFromStore(final ByteBuffer storeData) {
case NULL:
break;
case CHAR:
this.value = (RpmTagValue<T>) new RpmTagValue<>(this.count == 1 ? Character.valueOf((char) storeData.get()) : IntStream.range(0, this.count).mapToObj(i -> (char) storeData.get()).toArray(Character[]::new));
this.value = new RpmTagValue<>(this.count == 1 ? Character.valueOf((char) storeData.get()) : IntStream.range(0, this.count).mapToObj(i -> (char) storeData.get()).toArray(Character[]::new));
break;
case BYTE:
case UNKNOWN:
this.value = (RpmTagValue<T>) new RpmTagValue<>(this.count == 1 ? Byte.valueOf(storeData.get()) : IntStream.range(0, this.count).mapToObj(i -> storeData.get()).toArray(Byte[]::new));
this.value = new RpmTagValue<>(this.count == 1 ? Byte.valueOf(storeData.get()) : IntStream.range(0, this.count).mapToObj(i -> storeData.get()).toArray(Byte[]::new));
break;
case SHORT:
this.value = (RpmTagValue<T>) new RpmTagValue<>(this.count == 1 ? Short.valueOf(storeData.getShort()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getShort()).toArray(Short[]::new));
this.value = new RpmTagValue<>(this.count == 1 ? Short.valueOf(storeData.getShort()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getShort()).toArray(Short[]::new));
break;
case INT:
this.value = (RpmTagValue<T>) new RpmTagValue<>(this.count == 1 ? Integer.valueOf(storeData.getInt()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getInt()).toArray(Integer[]::new));
this.value = new RpmTagValue<>(this.count == 1 ? Integer.valueOf(storeData.getInt()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getInt()).toArray(Integer[]::new));
break;
case LONG:
this.value = (RpmTagValue<T>) new RpmTagValue<>(this.count == 1 ? Long.valueOf(storeData.getLong()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getLong()).toArray(Long[]::new));
this.value = new RpmTagValue<>(this.count == 1 ? Long.valueOf(storeData.getLong()) : IntStream.range(0, this.count).mapToObj(i -> storeData.getLong()).toArray(Long[]::new));
break;
case STRING:
this.value = (RpmTagValue<T>) new RpmTagValue<>(makeString(storeData));
this.value = new RpmTagValue<>(makeString(storeData));
break;
case BLOB:
this.value = (RpmTagValue<T>) new RpmTagValue<>(makeBlob(storeData));
this.value = new RpmTagValue<>(makeBlob(storeData));
break;
case STRING_ARRAY:
case I18N_STRING:
this.value = (RpmTagValue<T>) new RpmTagValue<>(IntStream.range(0, this.count).mapToObj(i -> makeString(storeData)).toArray(String[]::new));
this.value = new RpmTagValue<>(IntStream.range(0, this.count).mapToObj(i -> makeString(storeData)).toArray(String[]::new));
break;
}
}
Expand Down
57 changes: 35 additions & 22 deletions rpm/src/main/java/org/eclipse/packager/rpm/parse/InputHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.packager.rpm.parse;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand All @@ -25,15 +26,15 @@
import org.eclipse.packager.rpm.RpmTagValue;

public class InputHeader<T extends RpmBaseTag> implements ReadableHeader<T> {
private final Map<Integer, HeaderValue<?>> entries;
private final Map<Integer, HeaderValue> entries;

private final long start;

private final long length;

public InputHeader(final HeaderValue<?>[] entries, final long start, final long length) {
final Map<Integer, HeaderValue<?>> tags = new LinkedHashMap<>(entries.length);
for (final HeaderValue<?> entry : entries) {
public InputHeader(final HeaderValue[] entries, final long start, final long length) {
final Map<Integer, HeaderValue> tags = new LinkedHashMap<>(entries.length);
for (final HeaderValue entry : entries) {
tags.put(entry.getTag(), entry);
}

Expand Down Expand Up @@ -70,17 +71,6 @@ public boolean hasTag(final T tag) {
return hasTag(tag.getValue());
}

public <E> E get(T tag) {
Optional<HeaderValue<E>> optHeaderValue = getOptionalTag(tag, tag.getDataType());
if (optHeaderValue.isPresent()) {
HeaderValue<E> headerValue = optHeaderValue.get();
RpmTagValue<E> rpmTagValue = headerValue.getValue();
return rpmTagValue.getValue();
}

return null;
}

@Override
public String getString(T tag) {
if (!String.class.isAssignableFrom(tag.getDataType()) && !String[].class.isAssignableFrom(tag.getDataType())) {
Expand Down Expand Up @@ -148,24 +138,47 @@ public byte[] getByteArray(T tag) {
return getOptionalTag(tag, byte[].class).flatMap(headerValue -> headerValue.getValue().asByteArray()).orElse(null);
}

public <E> Optional<HeaderValue<E>> getOptionalTag(final int tag, Class<E> dataType) {
public Optional<HeaderValue> getOptionalTag(final int tag, Class<?> dataType) {
return getEntry(tag, dataType);
}

public <E> Optional<HeaderValue<E>> getOptionalTag(final T tag, Class<E> dataType) {
public <E> Optional<HeaderValue> getOptionalTag(final T tag, Class<E> dataType) {
return getOptionalTag(tag.getValue(), dataType);
}

@SuppressWarnings("unchecked")
private <E> Optional<HeaderValue<E>> getEntry(final int tag, Class<E> dataType) {
return Optional.ofNullable((HeaderValue<E>) this.entries.get(tag));
private Optional<HeaderValue> getEntry(final int tag, Class<?> dataType) {
final HeaderValue headerValue = this.entries.get(tag);

if (headerValue == null) {
return Optional.empty();
}

final RpmTagValue<?> rpmTagValue = headerValue.getValue();
final Object value = rpmTagValue.getValue();
Object v = value;

if (value == null) {
return Optional.empty();
}

if (dataType.isArray() && !v.getClass().isArray()) {
final Class<?> arrayType = Array.newInstance(v.getClass(), 0).getClass();

if (!arrayType.isAssignableFrom(dataType)) {
throw new IllegalArgumentException("Tag " + tag + " is type " + v.getClass().getSimpleName() + " which is an array, but not assignable from " + dataType.getSimpleName());
}
} else if (!v.getClass().isAssignableFrom(dataType)) {
throw new IllegalArgumentException("Tag " + tag + " is type " + v.getClass().getSimpleName() + " which is not assignable from " + dataType.getSimpleName());
}

return Optional.of(headerValue);
}

private <E> Optional<HeaderValue<E>> getEntry(final T tag, Class<E> dataType) {
private Optional<HeaderValue> getEntry(final T tag, Class<?> dataType) {
return getEntry(tag.getValue(), dataType);
}

public Map<Integer, HeaderValue<?>> getRawTags() {
public Map<Integer, HeaderValue> getRawTags() {
return this.entries;
}
}
Loading

0 comments on commit ee04664

Please sign in to comment.