|
7 | 7 | import io.javaoperatorsdk.operator.processing.event.source.ResourceCache; |
8 | 8 | import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource; |
9 | 9 |
|
10 | | -public class DependentResource<R extends HasMetadata, P extends HasMetadata> { |
| 10 | +public class DependentResource<R extends HasMetadata, P extends HasMetadata> |
| 11 | + implements Builder<R, P>, Updater<R, P> { |
11 | 12 |
|
12 | 13 | private final DefaultDependentResourceConfiguration<R, P> configuration; |
13 | | - private final Builder<R> builder; |
14 | | - private final Updater<R> updater; |
| 14 | + private final Builder<R, P> builder; |
| 15 | + private final Updater<R, P> updater; |
15 | 16 | private final Fetcher<R> fetcher; |
16 | 17 | private ResourceEventSource<R, P> source; |
17 | 18 |
|
18 | 19 | public DependentResource(DefaultDependentResourceConfiguration<R, P> configuration, |
19 | | - Builder<R> builder, Updater<R> updater, Fetcher<R> fetcher) { |
| 20 | + Builder<R, P> builder, Updater<R, P> updater, Fetcher<R> fetcher) { |
20 | 21 | this.configuration = configuration; |
21 | 22 | this.builder = builder; |
22 | 23 | this.updater = updater; |
23 | 24 | this.fetcher = fetcher; |
24 | 25 | } |
25 | 26 |
|
26 | | - public R build() { |
27 | | - if (getConfiguration().creatable()) { |
28 | | - throw new IllegalStateException( |
29 | | - "Should be implemented if DependentResource is configured as creatable"); |
30 | | - } |
31 | | - throw new IllegalStateException("Should not be called if DependentResource is not creatable"); |
| 27 | + @Override |
| 28 | + public R buildFor(P primary) { |
| 29 | + return builder.buildFor(primary); |
32 | 30 | } |
33 | 31 |
|
34 | 32 | public ResourceCache<R> getCache() { |
35 | 33 | return source.getResourceCache(); |
36 | 34 | } |
37 | 35 |
|
38 | 36 | public R fetchFor(HasMetadata owner) { |
39 | | - return getCache().get(ResourceID.fromResource(owner)).orElse(null); |
| 37 | + return fetcher != null ? fetcher.fetchFor(owner, getCache()) |
| 38 | + : getCache().get(ResourceID.fromResource(owner)).orElse(null); |
40 | 39 | } |
41 | 40 |
|
42 | 41 | public DependentResourceConfiguration<R, P> getConfiguration() { |
43 | 42 | return configuration; |
44 | 43 | } |
45 | 44 |
|
46 | | - public R update(R fetched) { |
47 | | - if (getConfiguration().updatable()) { |
48 | | - throw new IllegalStateException( |
49 | | - "Should be implemented if DependentResource is configured as updatable"); |
50 | | - } |
51 | | - throw new IllegalStateException("Should not be called if DependentResource is not updatable"); |
| 45 | + @Override |
| 46 | + public R update(R fetched, P primary) { |
| 47 | + return updater.update(fetched, primary); |
52 | 48 | } |
53 | 49 |
|
54 | 50 | public void setSource(ResourceEventSource<R, P> source) { |
|
0 commit comments