Skip to content

Commit 6d4378f

Browse files
DATAREDIS-425 - Add CompositeIndexResolver.
Add a composite IndexResolver implementation that iterates over a given collection of delegate IndexResolver instances and collects IndexedData from those.
1 parent 05143e3 commit 6d4378f

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.core.convert;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.LinkedHashSet;
22+
import java.util.List;
23+
import java.util.Set;
24+
25+
import org.springframework.data.util.TypeInformation;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.CollectionUtils;
28+
29+
/**
30+
* Composite {@link IndexResolver} implementation that iterates over a given collection of delegate
31+
* {@link IndexResolver} instances. <br />
32+
* <br />
33+
* <strong>NOTE</strong> {@link IndexedData} created by an {@link IndexResolver} can be overwritten by subsequent
34+
* {@link IndexResolver}.
35+
*
36+
* @author Christoph Strobl
37+
* @since 1.7
38+
*/
39+
public class CompositeIndexResolver implements IndexResolver {
40+
41+
private final List<IndexResolver> resolvers;
42+
43+
/**
44+
* Create new {@link CompositeIndexResolver}.
45+
*
46+
* @param resolvers must not be {@literal null}.
47+
*/
48+
public CompositeIndexResolver(Collection<IndexResolver> resolvers) {
49+
50+
Assert.notNull(resolvers, "Resolvers must not be null!");
51+
if (CollectionUtils.contains(resolvers.iterator(), null)) {
52+
throw new IllegalArgumentException("Resolvers must no contain null values");
53+
}
54+
this.resolvers = new ArrayList<IndexResolver>(resolvers);
55+
}
56+
57+
/*
58+
* (non-Javadoc)
59+
* @see org.springframework.data.redis.core.convert.IndexResolver#resolveIndexesFor(org.springframework.data.util.TypeInformation, java.lang.Object)
60+
*/
61+
@Override
62+
public Set<IndexedData> resolveIndexesFor(TypeInformation<?> typeInformation, Object value) {
63+
64+
if (resolvers.isEmpty()) {
65+
return Collections.emptySet();
66+
}
67+
68+
Set<IndexedData> data = new LinkedHashSet<IndexedData>();
69+
for (IndexResolver resolver : resolvers) {
70+
data.addAll(resolver.resolveIndexesFor(typeInformation, value));
71+
}
72+
return data;
73+
}
74+
75+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.core.convert;
17+
18+
import static org.hamcrest.core.IsEqual.*;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Matchers.*;
21+
import static org.mockito.Mockito.*;
22+
23+
import java.util.Arrays;
24+
import java.util.Collections;
25+
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.Mock;
29+
import org.mockito.runners.MockitoJUnitRunner;
30+
import org.springframework.data.util.TypeInformation;
31+
32+
/**
33+
* @author Christoph Strobl
34+
*/
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class CompositeIndexResolverUnitTests {
37+
38+
@Mock IndexResolver resolver1;
39+
@Mock IndexResolver resolver2;
40+
@Mock TypeInformation<?> typeInfoMock;
41+
42+
/**
43+
* @see DATAREDIS-425
44+
*/
45+
@Test(expected = IllegalArgumentException.class)
46+
public void shouldRejectNull() {
47+
new CompositeIndexResolver(null);
48+
}
49+
50+
/**
51+
* @see DATAREDIS-425
52+
*/
53+
@Test(expected = IllegalArgumentException.class)
54+
public void shouldRejectCollectionWithNullValues() {
55+
new CompositeIndexResolver(Arrays.asList(resolver1, null, resolver2));
56+
}
57+
58+
/**
59+
* @see DATAREDIS-425
60+
*/
61+
@Test
62+
public void shouldCollectionIndexesFromResolvers() {
63+
64+
when(resolver1.resolveIndexesFor(any(TypeInformation.class), anyObject())).thenReturn(
65+
Collections.<IndexedData> singleton(new SimpleIndexedPropertyValue("spring", "data", "redis")));
66+
when(resolver2.resolveIndexesFor(any(TypeInformation.class), anyObject())).thenReturn(
67+
Collections.<IndexedData> singleton(new SimpleIndexedPropertyValue("redis", "data", "spring")));
68+
69+
CompositeIndexResolver resolver = new CompositeIndexResolver(Arrays.asList(resolver1, resolver2));
70+
71+
assertThat(resolver.resolveIndexesFor(typeInfoMock, "o.O").size(), equalTo(2));
72+
}
73+
}

0 commit comments

Comments
 (0)