/
SignalEnrichmentContext.java
116 lines (101 loc) · 4.26 KB
/
SignalEnrichmentContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.models.signalenrichment;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.internal.utils.cache.CacheLookupContext;
import org.eclipse.ditto.json.JsonFieldSelector;
/**
* Immutable implementation of {@link org.eclipse.ditto.internal.utils.cache.CacheLookupContext} in scope of the
* signal enrichment caching.
*/
@Immutable
public final class SignalEnrichmentContext implements CacheLookupContext {
private final DittoHeaders dittoHeaders;
@Nullable private final DittoHeaders dittoHeadersNotAddedToCacheKey;
@Nullable private final JsonFieldSelector jsonFieldSelector;
private SignalEnrichmentContext(final DittoHeaders dittoHeaders,
@Nullable final DittoHeaders dittoHeadersNotAddedToCacheKey,
@Nullable final JsonFieldSelector jsonFieldSelector) {
this.dittoHeaders = checkNotNull(dittoHeaders, "dittoHeaders");
this.dittoHeadersNotAddedToCacheKey = dittoHeadersNotAddedToCacheKey;
this.jsonFieldSelector = jsonFieldSelector;
}
/**
* Creates a new SignalEnrichmentContext from the passed optional {@code dittoHeaders},
* {@code dittoHeadersNotAddedToCacheKey} and {@code jsonFieldSelector}
* retaining the for caching relevant {@code dittoHeaders} from the passed ones, but not adding the
* {@code dittoHeadersNotAddedToCacheKey} to hashCode/equals, ignoring those for caching.
*
* @param dittoHeaders the DittoHeaders to use as key in the cache lookup context.
* @param dittoHeadersNotAddedToCacheKey the DittoHeaders to additionally use, but not include in the cache key.
* @param jsonFieldSelector the JsonFieldSelector to use in the cache lookup context.
* @return the created context.
*/
static SignalEnrichmentContext of(final DittoHeaders dittoHeaders,
@Nullable final DittoHeaders dittoHeadersNotAddedToCacheKey,
@Nullable final JsonFieldSelector jsonFieldSelector) {
return new SignalEnrichmentContext(dittoHeaders, dittoHeadersNotAddedToCacheKey, jsonFieldSelector);
}
/**
* Returns the DittoHeaders this context provides.
*
* @return the DittoHeaders.
*/
public DittoHeaders getDittoHeaders() {
return dittoHeaders;
}
/**
* @return the additional DittoHeaders to use, e.g. for looking up the cache entry, but which are not part of the
* cache key.
*/
public Optional<DittoHeaders> getDittoHeadersNotAddedToCacheKey() {
return Optional.ofNullable(dittoHeadersNotAddedToCacheKey);
}
/**
* Returns the optional JsonFieldSelector this context provides.
*
* @return the optional JsonFieldSelector.
*/
public Optional<JsonFieldSelector> getJsonFieldSelector() {
return Optional.ofNullable(jsonFieldSelector);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final SignalEnrichmentContext that = (SignalEnrichmentContext) o;
return Objects.equals(dittoHeaders, that.dittoHeaders) &&
Objects.equals(jsonFieldSelector, that.jsonFieldSelector);
}
@Override
public int hashCode() {
return Objects.hash(dittoHeaders, jsonFieldSelector);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"dittoHeaders=" + dittoHeaders +
", dittoHeadersNotAddedToCacheKey=" + dittoHeadersNotAddedToCacheKey +
", jsonFieldSelector=" + jsonFieldSelector +
"]";
}
}