io.bitsensor.lib.entity.BlockedAttacker Maven / Gradle / Ivy
package io.bitsensor.lib.entity;
import io.bitsensor.lib.entity.proto.Datapoint;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
public class BlockedAttacker {
@NotNull
private List blockedDatapoints;
private boolean isBlocked = true;
private Date creationDate;
private Date lastUpdated;
private String description = "";
public BlockedAttacker(List datapoints) {
setBlockedDatapoints(datapoints);
creationDate = new Date();
lastUpdated = creationDate;
}
public BlockedAttacker() {
creationDate = new Date();
lastUpdated = creationDate;
}
/**
* Returns a datapoint with only fields specified in {@link Constants#DATAPOINT_BLACKLIST_MATCH_FIELDS}.
*
*
* @return ensured datapoint.
*/
protected static List ensureAttackerFormat(List datapoints) {
return datapoints == null
? null
: datapoints.stream()
.map(datapoint -> {
if (datapoint == null)
return null;
Datapoint.Builder builder = datapoint.toBuilder();
List blackListMatchFields = asList(Constants.DATAPOINT_BLACKLIST_MATCH_FIELDS);
builder.getDescriptorForType().getFields().stream()
.filter(desc -> !blackListMatchFields.contains(desc.getName()))
.forEach(builder::clearField);
return builder.build();
})
.collect(Collectors.toList());
}
public boolean match(Datapoint datapointToMatch) {
return blockedDatapoints.stream().anyMatch(datapoint -> datapoint == datapointToMatch
|| datapointToMatch.toBuilder().mergeFrom(datapoint).build().equals(datapointToMatch));
}
public boolean isBlocked() {
return isBlocked;
}
public void setBlocked(boolean blocked) {
isBlocked = blocked;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getLastUpdated() {
return lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
public List getBlockedDatapoints() {
return blockedDatapoints;
}
public void setBlockedDatapoints(List blockedDatapoints) {
this.blockedDatapoints = ensureAttackerFormat(blockedDatapoints);
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlockedAttacker attacker = (BlockedAttacker) o;
if (!blockedDatapoints.equals(attacker.blockedDatapoints)) return false;
if (!creationDate.equals(attacker.creationDate)) return false;
if (!lastUpdated.equals(attacker.lastUpdated)) return false;
return description.equals(attacker.description);
}
@Override
public int hashCode() {
int result = blockedDatapoints.hashCode();
result = 31 * result + creationDate.hashCode();
result = 31 * result + lastUpdated.hashCode();
result = 31 * result + description.hashCode();
return result;
}
}