io.appulse.encon.config.NodeConfig Maven / Gradle / Ivy
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appulse.encon.config;
import static java.util.Collections.emptyList;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static lombok.AccessLevel.PRIVATE;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import io.appulse.encon.common.DistributionFlag;
import io.appulse.epmd.java.core.model.NodeType;
import io.appulse.epmd.java.core.model.Protocol;
import io.appulse.epmd.java.core.model.Version;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Singular;
import lombok.experimental.FieldDefaults;
import lombok.val;
/**
* Node configuration settings.
*
* @since 1.0.0
* @author Artem Labazin
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = PRIVATE)
public class NodeConfig {
/**
* Cached empty {@link NodeConfig} instance.
*/
public static final NodeConfig DEFAULT = NodeConfig.builder().build();
@SuppressWarnings("unchecked")
static NodeConfig newInstance (@NonNull Map map) {
NodeConfigBuilder builder = NodeConfig.builder();
ofNullable(map.get("epmd-port"))
.map(Object::toString)
.map(Integer::parseInt)
.ifPresent(builder::epmdPort);
ofNullable(map.get("type"))
.map(Object::toString)
.map(NodeType::valueOf)
.ifPresent(builder::type);
ofNullable(map.get("short-name"))
.map(Object::toString)
.map(Boolean::valueOf)
.ifPresent(builder::shortNamed);
ofNullable(map.get("cookie"))
.map(Object::toString)
.ifPresent(builder::cookie);
ofNullable(map.get("protocol"))
.map(Object::toString)
.map(Protocol::valueOf)
.ifPresent(builder::protocol);
if (map.containsKey("version") && map.get("version") instanceof Map) {
val versionMap = (Map) map.get("version");
ofNullable(versionMap.get("low"))
.map(Object::toString)
.map(Version::valueOf)
.ifPresent(builder::low);
ofNullable(versionMap.get("high"))
.map(Object::toString)
.map(Version::valueOf)
.ifPresent(builder::high);
}
ofNullable(map.get("distribution-flags"))
.filter(it -> it instanceof List)
.map(it -> (List) it)
.map(it -> it.stream().map(DistributionFlag::valueOf).collect(toSet()))
.ifPresent(builder::distributionFlags);
ofNullable(map.get("mailboxes"))
.filter(it -> it instanceof List)
.map(it -> (List