/
EntityRegionMap.java
147 lines (124 loc) · 3.98 KB
/
EntityRegionMap.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2019 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.services.utils.cacheloaders;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import akka.actor.ActorRef;
/**
* Immutable sharable map from resource types to actor reference.
*/
@Immutable
public final class EntityRegionMap implements Function<String, ActorRef> {
private final Map<String, ActorRef> rawMap;
private EntityRegionMap(final Map<String, ActorRef> rawMap) {
this.rawMap = Collections.unmodifiableMap(new HashMap<>(rawMap));
}
private EntityRegionMap(final Builder builder) {
this(builder.hashMap);
}
/**
* Find entity region for a resource type.
*
* @param resourceType resource type of an entity.
* @return actor reference to the shard region of the entity.
*/
public Optional<ActorRef> lookup(final String resourceType) {
requireNonNull(resourceType);
return Optional.ofNullable(findRegion(resourceType));
}
@Nullable
private ActorRef findRegion(final String resourceType) {
return rawMap.get(resourceType);
}
@Override
@Nullable
public ActorRef apply(final String resourceType) {
requireNonNull(resourceType);
return findRegion(resourceType);
}
/**
* Creates an {@link EntityRegionMap} with a single entry.
*
* @param resourceType the resource type.
* @param targetActor the actor reference.
* @return the created {@link EntityRegionMap}.
*/
public static EntityRegionMap singleton(final String resourceType, final ActorRef targetActor) {
return new EntityRegionMap(Collections.singletonMap(resourceType, targetActor));
}
/**
* Create a new builder of {@code EntityRegionMap}.
*
* @return a new builder.
*/
public static Builder newBuilder() {
return new Builder();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final EntityRegionMap that = (EntityRegionMap) o;
return Objects.equals(rawMap, that.rawMap);
}
@Override
public int hashCode() {
return Objects.hash(rawMap);
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " [" +
"rawMap=" + rawMap +
']';
}
/**
* Builder of {@code EntityRegionMap}.
*/
@NotThreadSafe
public static final class Builder {
private final HashMap<String, ActorRef> hashMap = new HashMap<>();
/**
* Add a resource-type-to-actor-reference mapping.
*
* @param resourceType the resource type.
* @param targetActor the actor reference.
* @return this builder.
*/
public Builder put(final String resourceType, final ActorRef targetActor) {
requireNonNull(resourceType);
requireNonNull(targetActor);
hashMap.put(resourceType, targetActor);
return this;
}
/**
* Create an {@code EntityRegionMap} from this builder.
*
* @return the {@code EntityRegionMap}.
*/
public EntityRegionMap build() {
return new EntityRegionMap(this);
}
}
}