-
Notifications
You must be signed in to change notification settings - Fork 215
/
EnforcementCacheKey.java
114 lines (94 loc) · 3.71 KB
/
EnforcementCacheKey.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
/*
* 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.utils.cacheloaders;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.text.MessageFormat;
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.base.model.entity.type.EntityType;
import org.eclipse.ditto.internal.utils.cache.CacheKey;
/**
* Implementation for a {@link CacheKey} used in scope of policy enforcement.
*/
@Immutable
public final class EnforcementCacheKey implements CacheKey<EnforcementContext> {
static final String DELIMITER = ":";
private final EntityId entityId;
@Nullable private final EnforcementContext context;
EnforcementCacheKey(final EntityId entityId, @Nullable final EnforcementContext context) {
this.entityId = entityId;
this.context = context;
}
/**
* Deserialize entity ID with resource type from a string.
*
* @param string the string.
* @return the entity ID with resource type.
* @throws IllegalArgumentException if the string does not have the expected format.
*/
public static EnforcementCacheKey readFrom(final String string) {
checkNotNull(string, "string");
final int delimiterIndex = string.indexOf(DELIMITER);
if (delimiterIndex < 0) {
final String message = MessageFormat.format("Unexpected EntityId format: <{0}>", string);
throw new IllegalArgumentException(message);
} else {
final EntityType entityType = EntityType.of(string.substring(0, delimiterIndex));
final EntityId id = EntityId.of(entityType, string.substring(delimiterIndex + 1));
return new EnforcementCacheKey(id, null);
}
}
public static EnforcementCacheKey of(final EntityId entityId) {
return new EnforcementCacheKey(entityId, null);
}
public static EnforcementCacheKey of(final EntityId entityId, final EnforcementContext context) {
return new EnforcementCacheKey(entityId, context);
}
@Override
public EntityId getId() {
return entityId;
}
@Override
public Optional<EnforcementContext> getCacheLookupContext() {
return Optional.ofNullable(context);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final EnforcementCacheKey that = (EnforcementCacheKey) o;
return isIdEqualValueBased(that);
}
@Override
public int hashCode() {
return Objects.hash(entityId);
}
@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", entityId.getEntityType(), DELIMITER, entityId);
}
// this allows working with fallback entity IDs as well without breaking caching
private boolean isIdEqualValueBased(final EnforcementCacheKey that) {
return Objects.equals(entityId.getEntityType(), that.entityId.getEntityType()) &&
Objects.equals(entityId.toString(), that.entityId.toString());
}
}