1 java multithreading builder builder-pattern guava
我有一个下面的构建器类,我从多线程应用程序中使用它,所以我使它成为线程安全的。为简单起见,我在这里只展示了几个字段来演示这个问题。
public final class ClientKey {
private final long userId;
private final int clientId;
private final String processName;
private final Map<String, String> parameterMap;
private ClientKey(Builder builder) {
this.userId = builder.userId;
this.clientId = builder.clientId;
this.processName = builder.processName;
// initializing the required fields
// and below line throws exception once I try to clone the `ClientKey` object
builder.parameterMap.put("is_clientid", (clientId == 0) ? "false" : "true");
this.parameterMap = builder.parameterMap.build();
}
public static class Builder {
private final long userId;
private final int clientId;
private String processName;
private ImmutableMap.Builder<String, String> parameterMap = ImmutableMap.builder();
// this is for cloning
public Builder(ClientKey key) {
this.userId = key.userId;
this.clientId = key.clientId;
this.processName = key.processName;
this.parameterMap =
ImmutableMap.<String, String>builder().putAll(key.parameterMap);
}
public Builder(long userId, int clientId) {
this.userId = userId;
this.clientId = clientId;
}
public Builder parameterMap(Map<String, String> parameterMap) {
this.parameterMap.putAll(parameterMap);
return this;
}
public Builder processName(String processName) {
this.processName = processName;
return this;
}
public ClientKey build() {
return new ClientKey(this);
}
}
// getters
}
Run Code Online (Sandbox Code Playgroud)
下面是我的制作方法ClientKey,效果很好。
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("hello", "world");
ClientKey keys = new ClientKey.Builder(12345L, 200).parameterMap(testMap).build();
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试克隆keys如下所示的对象时,它会引发异常。
ClientKey clonedKey = new ClientKey.Builder(keys).processName("hello").build();
Run Code Online (Sandbox Code Playgroud)
它抛出异常,错误消息为: java.lang.IllegalArgumentException: Multiple entries with same key: is_clientid=true and is_clientid=true
builder.parameterMap.put("is_clientid", (clientId == 0) ? "false" : "true");
// from below line exception is coming
this.parameterMap = builder.parameterMap.build();
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我想让我的地图不可变,但我也想用必填字段进行初始化,而且我只能在ClientKey类的构造函数中进行。它在克隆ClientKey对象时抛出异常。
当您构造 a 时ClientKey,"is_clientid"键被放入映射中。因此,如果您调用ClientKey.Builder(ClientKey)构造函数,该putAll调用会将其复制到您的新ImmutableMap.Builder实例。当您随后构建您的 cloned 时ClientKey,ClientKey构造函数将再次尝试将相同的键添加到映射中,这会导致异常。
该ImmutableMap.Builder会已被写入以不同的方式,但事实并非如此。如果你想使用它,你就必须忍受它。
一种解决方案是不可复制与进入"is_clientid"关键新ImmutableMap.Builder在你的构造函数Builder。而不是this.parameterMap = ImmutableMap.<String, String>builder().putAll(key.parameterMap);你写:
this.parameterMap = new ImmutableMap.Builder<>();
for (Map.Entry<String,String> entry : key.parameterMap.entrySet()) {
if (!"is_clientid".equals(entry.getKey()) {
this.parameterMap.put(entry.getKey(), entry.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
另一个解决方案是不使用 Guava 的ImmutableMap.Builder,而是使用普通的 Java HashMap(当您尝试将重复的键放入其中时它不会抛出异常,旧条目会被简单地覆盖)。然后在你的ClientKey构造函数中你写:
this.parameterMap = Collections.unmodifiableMap(builder.parameterMap);
Run Code Online (Sandbox Code Playgroud)
你也可以这样写:
this.parameterMap = ImmutableMap.copyOf(builder.parameterMap);
Run Code Online (Sandbox Code Playgroud)
但这会生成地图的完整副本,对于非常大的地图,这可能需要一些时间。
结束语:如果您只想复制 a ClientKey,则不需要构建器;惯用的 Java 将使用复制构造函数或clone()方法(尽管后者被某些人所反对)。