Skip to content

Commit

Permalink
Support for indexing into any Collection/Iterable
Browse files Browse the repository at this point in the history
Closes gh-907
  • Loading branch information
jhoeller committed Jul 11, 2023
1 parent e048b09 commit c20a2e4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -657,22 +656,32 @@ else if (value instanceof List list) {
growCollectionIfNecessary(list, index, indexedPropertyName.toString(), ph, i + 1);
value = list.get(index);
}
else if (value instanceof Set set) {
// Apply index to Iterator in case of a Set.
else if (value instanceof Iterable iterable) {
// Apply index to Iterator in case of a Set/Collection/Iterable.
int index = Integer.parseInt(key);
if (index < 0 || index >= set.size()) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Set of size " +
set.size() + ", accessed using property path '" + propertyName + "'");
if (value instanceof Collection<?> coll) {
if (index < 0 || index >= coll.size()) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Collection of size " +
coll.size() + ", accessed using property path '" + propertyName + "'");
}
}
Iterator<Object> it = set.iterator();
for (int j = 0; it.hasNext(); j++) {
Iterator<Object> it = iterable.iterator();
boolean found = false;
int currIndex = 0;
for (; it.hasNext(); currIndex++) {
Object elem = it.next();
if (j == index) {
if (currIndex == index) {
value = elem;
found = true;
break;
}
}
if (!found) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Cannot get element with index " + index + " from Iterable of size " +
currIndex + ", accessed using property path '" + propertyName + "'");
}
}
else if (value instanceof Map map) {
Class<?> mapKeyType = ph.getResolvableType().getNested(i + 1).asMap().resolveGeneric(0);
Expand All @@ -685,13 +694,17 @@ else if (value instanceof Map map) {
else {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Property referenced in indexed property path '" + propertyName +
"' is neither an array nor a List nor a Set nor a Map; returned value was [" + value + "]");
"' is neither an array nor a List/Set/Collection/Iterable nor a Map; " +
"returned value was [" + value + "]");
}
indexedPropertyName.append(PROPERTY_KEY_PREFIX).append(key).append(PROPERTY_KEY_SUFFIX);
}
}
return value;
}
catch (InvalidPropertyException ex) {
throw ex;
}
catch (IndexOutOfBoundsException ex) {
throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
"Index of out of bounds in property path '" + propertyName + "'", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ void isReadableWritableForIndexedProperties() {
assertThat(accessor.isReadableProperty("list")).isTrue();
assertThat(accessor.isReadableProperty("set")).isTrue();
assertThat(accessor.isReadableProperty("map")).isTrue();
assertThat(accessor.isReadableProperty("myTestBeans")).isTrue();
assertThat(accessor.isReadableProperty("xxx")).isFalse();

assertThat(accessor.isWritableProperty("array")).isTrue();
assertThat(accessor.isWritableProperty("list")).isTrue();
assertThat(accessor.isWritableProperty("set")).isTrue();
assertThat(accessor.isWritableProperty("map")).isTrue();
assertThat(accessor.isWritableProperty("myTestBeans")).isTrue();
assertThat(accessor.isWritableProperty("xxx")).isFalse();

assertThat(accessor.isReadableProperty("array[0]")).isTrue();
Expand All @@ -159,6 +161,8 @@ void isReadableWritableForIndexedProperties() {
assertThat(accessor.isReadableProperty("map[key4][0].name")).isTrue();
assertThat(accessor.isReadableProperty("map[key4][1]")).isTrue();
assertThat(accessor.isReadableProperty("map[key4][1].name")).isTrue();
assertThat(accessor.isReadableProperty("myTestBeans[0]")).isTrue();
assertThat(accessor.isReadableProperty("myTestBeans[1]")).isFalse();
assertThat(accessor.isReadableProperty("array[key1]")).isFalse();

assertThat(accessor.isWritableProperty("array[0]")).isTrue();
Expand All @@ -173,6 +177,8 @@ void isReadableWritableForIndexedProperties() {
assertThat(accessor.isWritableProperty("map[key4][0].name")).isTrue();
assertThat(accessor.isWritableProperty("map[key4][1]")).isTrue();
assertThat(accessor.isWritableProperty("map[key4][1].name")).isTrue();
assertThat(accessor.isReadableProperty("myTestBeans[0]")).isTrue();
assertThat(accessor.isReadableProperty("myTestBeans[1]")).isFalse();
assertThat(accessor.isWritableProperty("array[key1]")).isFalse();
}

Expand Down Expand Up @@ -1388,6 +1394,7 @@ void getAndSetIndexedProperties() {
assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name8");
assertThat(accessor.getPropertyValue("map['key5[foo]'].name")).isEqualTo("name8");
assertThat(accessor.getPropertyValue("map[\"key5[foo]\"].name")).isEqualTo("name8");
assertThat(accessor.getPropertyValue("myTestBeans[0].name")).isEqualTo("nameZ");

MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].name", "name5");
Expand All @@ -1401,6 +1408,7 @@ void getAndSetIndexedProperties() {
pvs.add("map[key4][0].name", "nameA");
pvs.add("map[key4][1].name", "nameB");
pvs.add("map[key5[foo]].name", "name10");
pvs.add("myTestBeans[0].name", "nameZZ");
accessor.setPropertyValues(pvs);
assertThat(tb0.getName()).isEqualTo("name5");
assertThat(tb1.getName()).isEqualTo("name4");
Expand All @@ -1419,6 +1427,7 @@ void getAndSetIndexedProperties() {
assertThat(accessor.getPropertyValue("map[key4][0].name")).isEqualTo("nameA");
assertThat(accessor.getPropertyValue("map[key4][1].name")).isEqualTo("nameB");
assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name10");
assertThat(accessor.getPropertyValue("myTestBeans[0].name")).isEqualTo("nameZZ");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,10 @@
package org.springframework.beans.testfixture.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -47,6 +49,8 @@ public class IndexedTestBean {

private SortedMap sortedMap;

private MyTestBeans myTestBeans;


public IndexedTestBean() {
this(true);
Expand All @@ -71,6 +75,7 @@ public void populate() {
TestBean tb8 = new TestBean("name8", 0);
TestBean tbX = new TestBean("nameX", 0);
TestBean tbY = new TestBean("nameY", 0);
TestBean tbZ = new TestBean("nameZ", 0);
this.array = new TestBean[] {tb0, tb1};
this.list = new ArrayList<>();
this.list.add(tb2);
Expand All @@ -87,6 +92,7 @@ public void populate() {
list.add(tbY);
this.map.put("key4", list);
this.map.put("key5[foo]", tb8);
this.myTestBeans = new MyTestBeans(tbZ);
}


Expand Down Expand Up @@ -146,4 +152,27 @@ public void setSortedMap(SortedMap sortedMap) {
this.sortedMap = sortedMap;
}

public MyTestBeans getMyTestBeans() {
return myTestBeans;
}

public void setMyTestBeans(MyTestBeans myTestBeans) {
this.myTestBeans = myTestBeans;
}


public static class MyTestBeans implements Iterable<TestBean> {

private final Collection<TestBean> testBeans;

public MyTestBeans(TestBean... testBeans) {
this.testBeans = Arrays.asList(testBeans);
}

@Override
public Iterator<TestBean> iterator() {
return this.testBeans.iterator();
}
}

}

0 comments on commit c20a2e4

Please sign in to comment.