Add a `copyOf` method to ImmutableCapabilities
This mirrors the guava api, and means we can reduce
the amount of work needed when ensuring the a
Capabilities instance is immutable.
import java.io.Serializable;
import java.util.Map;
+import java.util.Objects;
public class ImmutableCapabilities extends AbstractCapabilities implements Serializable {
@@ -74,4 +75,14 @@ public ImmutableCapabilities(Capabilities other) {
public ImmutableCapabilities(Map<String, ?> capabilities) {
capabilities.forEach(this::setCapability);
}
+
+ public static ImmutableCapabilities copyOf(Capabilities capabilities) {
+ Objects.requireNonNull(capabilities, "Capabilities must be set");
+
+ if (capabilities instanceof ImmutableCapabilities) {
+ return (ImmutableCapabilities) capabilities;
+ }
+
+ return new ImmutableCapabilities(capabilities);
+ }
}
\ No newline at end of file
