Skip to content

Commit

Permalink
fix for #1336
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszwalacik committed Nov 10, 2023
1 parent 69b784b commit 408c558
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<Type> getConcreteClassTypeArguments() {
public Object map(Object sourceArray, EnumerableFunction mapFunction, OwnerContext owner) {
Validate.argumentsAreNotNull(sourceArray, mapFunction, owner);

Object targetArray = newArray(sourceArray, null, false);
Object targetArray = newArray(sourceArray, false);

int len = Array.getLength(sourceArray);
EnumerationAwareOwnerContext enumerationContext = new IndexableEnumerationOwnerContext(owner);
Expand All @@ -55,9 +55,18 @@ public boolean isEmpty(Object array) {

@Override
public Object map(Object sourceArray, Function mapFunction, boolean filterNulls) {
return this.map(sourceArray, mapFunction, filterNulls, false);
}

@Override
public Object mapPreservingSourceItemType(Object sourceArray, Function mapFunction) {
return this.map(sourceArray, mapFunction, false, true);
}

private Object map(Object sourceArray, Function mapFunction, boolean filterNulls, boolean keepSourceItemTypes) {
Validate.argumentsAreNotNull(sourceArray, mapFunction);

Object targetArray = newArray(sourceArray, mapFunction, true);
Object targetArray = newArray(sourceArray, keepSourceItemTypes);

int len = Array.getLength(sourceArray);
int t = 0;
Expand All @@ -80,7 +89,7 @@ protected Stream<Object> items(Object source) {
return Arrays.asList((Object[])source).stream();
}

private Object newArray(Object sourceArray, Function mapFunction, boolean doSample) {
private Object newArray(Object sourceArray, boolean preserveItemType) {
int len = Array.getLength(sourceArray);
if (len == 0) {
return sourceArray;
Expand All @@ -90,11 +99,8 @@ private Object newArray(Object sourceArray, Function mapFunction, boolean doSamp
return Array.newInstance(getItemClass(), len);
}

if (doSample) {
Object sample = mapFunction.apply(Array.get(sourceArray, 0));
if (getItemClass().isAssignableFrom(sample.getClass())) {
return Array.newInstance(getItemClass(), len);
}
if (preserveItemType) {
return Array.newInstance(getItemClass(), len);
}

return Array.newInstance(Object.class, len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public Object map(Object sourceEnumerable, Function mapFunction) {
return map(sourceEnumerable, mapFunction, false);
}

/**
* For building Shadows. Item type of new enumerable matters only for arrays
*/
public Object mapPreservingSourceItemType(Object sourceEnumerable, Function mapFunction) {
return this.map(sourceEnumerable, mapFunction);
}

public abstract Object map(Object sourceEnumerable, Function mapFunction, boolean filterNulls);

public abstract boolean isEmpty(Object container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private class EnumerableWiring extends Wiring {
void wire() {
EnumerableType propertyType = property.getType();

Object targetContainer = propertyType.map(targetWithShadows, (valueOrShadow) -> {
Object targetContainer = propertyType.mapPreservingSourceItemType(targetWithShadows, (valueOrShadow) -> {
if (valueOrShadow instanceof ShadowBuilder) {
//injecting reference to shadow
return ((ShadowBuilder) valueOrShadow).shadow;
Expand Down

0 comments on commit 408c558

Please sign in to comment.