Skip to content

Commit 72bc045

Browse files
committed
Add getRequiredKeySpace to KeyValuePersistentEntity.
Improve nullability handling for entities that are required to provide a keyspace. Closes #640
1 parent 8d926d5 commit 72bc045

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentEntity.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
package org.springframework.data.keyvalue.core.mapping;
1717

1818
import org.jspecify.annotations.Nullable;
19+
20+
import org.springframework.data.mapping.PersistentEntity;
1921
import org.springframework.data.mapping.model.MutablePersistentEntity;
2022

2123
/**
24+
* KeyValue-specific extension of {@link PersistentEntity} declaring a {@literal keySpace}.
25+
*
2226
* @author Christoph Strobl
27+
* @author Mark Paluch
2328
* @param <T>
2429
*/
2530
public interface KeyValuePersistentEntity<T, P extends KeyValuePersistentProperty<P>>
@@ -32,4 +37,24 @@ public interface KeyValuePersistentEntity<T, P extends KeyValuePersistentPropert
3237
*/
3338
@Nullable
3439
String getKeySpace();
40+
41+
/**
42+
* Returns the required {@literal keySpace} or throws an {@link IllegalStateException} if the {@literal keySpace} is
43+
* not defined.
44+
*
45+
* @return the {@literal keySpace} property of this {@link PersistentEntity}.
46+
* @throws IllegalStateException if the {@literal keySpace} is not defined.
47+
* @since 4.0
48+
*/
49+
default String getRequiredKeySpace() {
50+
51+
String keySpace = getKeySpace();
52+
53+
if (keySpace != null) {
54+
return keySpace;
55+
}
56+
57+
throw new IllegalStateException(String.format("Required keySpace not defined for %s", getType()));
58+
}
59+
3560
}

src/test/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntityUnitTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
*/
3636
class BasicKeyValuePersistentEntityUnitTests {
3737

38-
private KeyValueMappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext = new KeyValueMappingContext<>();
38+
KeyValueMappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext = new KeyValueMappingContext<>();
3939

4040
@Test // DATAKV-268
4141
void shouldDeriveKeyspaceFromClassName() {
4242

43-
assertThat(mappingContext.getPersistentEntity(KeyspaceEntity.class).getKeySpace())
43+
assertThat(mappingContext.getPersistentEntity(KeyspaceEntity.class).getRequiredKeySpace())
4444
.isEqualTo(KeyspaceEntity.class.getName());
4545
}
4646

@@ -54,7 +54,7 @@ void shouldEvaluateKeyspaceExpression() {
5454
persistentEntity.setEvaluationContextProvider(
5555
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
5656

57-
assertThat(persistentEntity.getKeySpace()).isEqualTo("some_foo");
57+
assertThat(persistentEntity.getRequiredKeySpace()).isEqualTo("some_foo");
5858
}
5959

6060
@Test // DATAKV-268
@@ -64,7 +64,7 @@ void shouldEvaluateEntityWithoutKeyspace() {
6464
persistentEntity.setEvaluationContextProvider(
6565
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
6666

67-
assertThat(persistentEntity.getKeySpace()).isEqualTo(NoKeyspaceEntity.class.getName());
67+
assertThat(persistentEntity.getRequiredKeySpace()).isEqualTo(NoKeyspaceEntity.class.getName());
6868
}
6969

7070
@Test // GH-461

0 commit comments

Comments
 (0)