org.kaizen4j.common.algorithm.weighted.WeightedServer Maven / Gradle / Ivy
package org.kaizen4j.common.algorithm.weighted;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.util.concurrent.atomic.AtomicInteger;
public class WeightedServer {
private String server;
private int weight;
private AtomicInteger currentWeight;
private AtomicInteger effectiveWeight;
public WeightedServer(String server, int weight) {
this.weight = weight;
this.server = server;
this.effectiveWeight = new AtomicInteger(weight);
this.currentWeight = new AtomicInteger(0);
}
public String getServer() {
return server;
}
public int getWeight() {
return weight;
}
int getCurrentWeight() {
return currentWeight.get();
}
int addAndGetCurrentWeight(int delta) {
return this.currentWeight.addAndGet(delta);
}
void setCurrentWeight(int value) {
this.currentWeight = new AtomicInteger(value);
}
int getEffectiveWeight() {
return effectiveWeight.get();
}
int addAndGetEffectiveWeight(int delta) {
return this.effectiveWeight.addAndGet(delta);
}
void setEffectiveWeight(int value) {
this.effectiveWeight = new AtomicInteger(value);
}
@Override
public boolean equals(final Object object) {
if (object instanceof WeightedServer) {
WeightedServer other = (WeightedServer) object;
return new EqualsBuilder().append(this.server, other.getServer()).append(this.weight, other.getWeight())
.isEquals();
}
return false;
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(this.server).append(this.weight).toHashCode();
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}