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

com.alibaba.nacos.client.naming.event.InstancesDiff Maven / Gradle / Ivy

There is a newer version: 2.4.2
Show newest version
/*
 * Copyright 1999-2023 Alibaba Group Holding Ltd.
 *
 * 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 com.alibaba.nacos.client.naming.event;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.common.utils.CollectionUtils;

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

/**
 * The differences in instances compared to the last callback.
 *
 * @author lideyou
 */
public class InstancesDiff {
    
    private final List addedInstances = new ArrayList<>();
    
    private final List removedInstances = new ArrayList<>();
    
    private final List modifiedInstances = new ArrayList<>();
    
    public InstancesDiff() {
    }
    
    public InstancesDiff(List addedInstances, List removedInstances,
            List modifiedInstances) {
        setAddedInstances(addedInstances);
        setRemovedInstances(removedInstances);
        setModifiedInstances(modifiedInstances);
    }
    
    public List getAddedInstances() {
        return addedInstances;
    }
    
    public void setAddedInstances(Collection addedInstances) {
        this.addedInstances.clear();
        if (CollectionUtils.isNotEmpty(addedInstances)) {
            this.addedInstances.addAll(addedInstances);
        }
    }
    
    public List getRemovedInstances() {
        return removedInstances;
    }
    
    public void setRemovedInstances(Collection removedInstances) {
        this.removedInstances.clear();
        if (CollectionUtils.isNotEmpty(removedInstances)) {
            this.removedInstances.addAll(removedInstances);
        }
    }
    
    public List getModifiedInstances() {
        return modifiedInstances;
    }
    
    public void setModifiedInstances(Collection modifiedInstances) {
        this.modifiedInstances.clear();
        if (CollectionUtils.isNotEmpty(modifiedInstances)) {
            this.modifiedInstances.addAll(modifiedInstances);
        }
    }
    
    /**
     * Check if any instances have changed.
     *
     * @return true if there are instances that have changed
     */
    public boolean hasDifferent() {
        return isAdded() || isRemoved() || isModified();
    }
    
    /**
     * Check if any instances have been added.
     *
     * @return true if there are instances that have been added.
     */
    public boolean isAdded() {
        return CollectionUtils.isNotEmpty(this.addedInstances);
    }
    
    /**
     * Check if any instances have been added.
     *
     * @return true if there are instances that have been added.
     */
    public boolean isRemoved() {
        return CollectionUtils.isNotEmpty(this.removedInstances);
    }
    
    /**
     * Check if any instances have been added.
     *
     * @return true if there are instances that have been added.
     */
    public boolean isModified() {
        return CollectionUtils.isNotEmpty(this.modifiedInstances);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy