All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wenweihu86.raft.util.ConfigurationUtils Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.wenweihu86.raft.util;

import com.github.wenweihu86.raft.proto.RaftMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenweihu86 on 2017/5/22.
 */
public class ConfigurationUtils {

    public static boolean containsServer(RaftMessage.Configuration configuration, int serverId) {
        for (RaftMessage.Server server : configuration.getServersList()) {
            if (server.getServerId() == serverId) {
                return true;
            }
        }
        return false;
    }

    public static RaftMessage.Configuration removeServers(
            RaftMessage.Configuration configuration, List servers) {
        RaftMessage.Configuration.Builder confBuilder = RaftMessage.Configuration.newBuilder();
        for (RaftMessage.Server server : configuration.getServersList()) {
            boolean toBeRemoved = false;
            for (RaftMessage.Server server1 : servers) {
                if (server.getServerId() == server1.getServerId()) {
                    toBeRemoved = true;
                    break;
                }
            }
            if (!toBeRemoved) {
                confBuilder.addServers(server);
            }
        }
        return confBuilder.build();
    }

    public static RaftMessage.Server getServer(RaftMessage.Configuration configuration, int serverId) {
        for (RaftMessage.Server server : configuration.getServersList()) {
            if (server.getServerId() == serverId) {
                return server;
            }
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy