-
Notifications
You must be signed in to change notification settings - Fork 215
/
InvalidateCacheEntry.java
101 lines (85 loc) · 3.21 KB
/
InvalidateCacheEntry.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
/*
* Copyright (c) 2022 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.policies.enforcement;
import static org.eclipse.ditto.base.model.json.JsonSchemaVersion.V_2;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.internal.utils.cacheloaders.EnforcementCacheKey;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
/**
* Concierge-service internal command signaling that the cache for a specific {@link org.eclipse.ditto.internal.utils.cache.CacheKey} should be invalidated.
* Is emitted via Pub/Sub when for example a Policy is modified.
* TODO TJ candidate for removal
*/
@Immutable
public final class InvalidateCacheEntry implements Jsonifiable<JsonObject> {
private static final JsonFieldDefinition<String> JSON_ENTITY_ID =
JsonFactory.newStringFieldDefinition("entityId", V_2);
private final EnforcementCacheKey entityId;
private InvalidateCacheEntry(final EnforcementCacheKey entityId) {this.entityId = entityId;}
/**
* Creates a new {@link InvalidateCacheEntry} from the passed {@code entityId}.
*
* @param entityId the EntityId to build the InvalidateCacheEntry for.
* @return the created InvalidateCacheEntry instance.
*/
public static InvalidateCacheEntry of(final EnforcementCacheKey entityId) {
return new InvalidateCacheEntry(entityId);
}
/**
* Creates a new {@link InvalidateCacheEntry} from a JSON object.
*
* @param jsonObject the JsonObject to create the InvalidateCacheEntry from.
* @return the created InvalidateCacheEntry instance.
*/
public static InvalidateCacheEntry fromJson(final JsonObject jsonObject) {
final String entityIdStr = jsonObject.getValueOrThrow(JSON_ENTITY_ID);
return new InvalidateCacheEntry(EnforcementCacheKey.readFrom(entityIdStr));
}
/**
* @return the EntityId to invalidate caches for.
*/
public EnforcementCacheKey getEntityId() {
return entityId;
}
@Override
public JsonObject toJson() {
return JsonFactory.newObjectBuilder()
.set(JSON_ENTITY_ID, entityId.toString())
.build();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof InvalidateCacheEntry)) {
return false;
}
final InvalidateCacheEntry that = (InvalidateCacheEntry) o;
return Objects.equals(entityId, that.entityId);
}
@Override
public int hashCode() {
return Objects.hash(entityId);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"entityId=" + entityId +
"]";
}
}