/
SignalEnrichmentCacheKey.java
91 lines (75 loc) · 2.94 KB
/
SignalEnrichmentCacheKey.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
/*
* 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.entity.id.EntityId;
import org.eclipse.ditto.internal.utils.cache.CacheKey;
/**
* Implementation for a {@link CacheKey} used in scope of signal enrichment.
*/
@Immutable
public final class SignalEnrichmentCacheKey implements CacheKey<SignalEnrichmentContext> {
static final String DELIMITER = ":";
private final EntityId id;
@Nullable private final SignalEnrichmentContext context;
private SignalEnrichmentCacheKey(final EntityId id, @Nullable final SignalEnrichmentContext context) {
this.id = checkNotNull(id, "id");
this.context = context;
}
/**
* Create a new entity ID from the given {@code resourceType} and {@code id}.
*
* @param id the entity ID.
* @param cacheLookupContext additional context information to use for the cache lookup.
* @return the entity ID with resource type object.
* @throws NullPointerException if {@code id} is {@code null}.
*/
static SignalEnrichmentCacheKey of(final EntityId id, @Nullable final SignalEnrichmentContext cacheLookupContext) {
return new SignalEnrichmentCacheKey(id, cacheLookupContext);
}
@Override
public EntityId getId() {
return id;
}
@Override
public Optional<SignalEnrichmentContext> getCacheLookupContext() {
return Optional.ofNullable(context);
}
@Override
public boolean equals(@Nullable final Object o) {
if (o instanceof SignalEnrichmentCacheKey that) {
return isIdEqualValueBased(that) && Objects.equals(context, that.context);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(id, context);
}
@Override
public String toString() {
// cache context is not in the string representation
// because it is used for serialization and cache context is local
return String.format("%s%s%s", id.getEntityType(), DELIMITER, id);
}
// this allows working with fallback entity IDs as well without breaking caching
private boolean isIdEqualValueBased(final SignalEnrichmentCacheKey that) {
return Objects.equals(id.getEntityType(), that.id.getEntityType()) &&
Objects.equals(id.toString(), that.id.toString());
}
}