com.regnosys.rosetta.translate.datamodel.EntityImpl Maven / Gradle / Ivy
package com.regnosys.rosetta.translate.datamodel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class EntityImpl implements Entity {
private final NamespaceName name;
private final List attributes;
private Entity extendedEntity;
private final boolean hasValue;
private final List extendingEntities;
public EntityImpl(NamespaceName name, Entity extendedEntity, boolean hasValue) {
this.name = name;
this.extendedEntity = extendedEntity;
this.hasValue = hasValue || (extendedEntity == null ? false : extendedEntity.hasData());
this.attributes = new ArrayList<>();
this.extendingEntities = new ArrayList<>();
if (extendedEntity != null)
extendedEntity.getKnownExtendingEntities().add(this);
}
public EntityImpl(NamespaceName name, boolean hasValue) {
this(name, null, hasValue);
}
protected void setExtended(Entity extended) {
this.extendedEntity = extended;
}
@Override
public NamespaceName getName() {
return name;
}
@Override
public List getAttributes() {
return attributes;
}
@Override
public void addAttribute(Attribute att) {
attributes.add(att);
}
@Override
public Entity getExtendedEntity() {
return extendedEntity;
}
@Override
public List getKnownExtendingEntities() {
return extendingEntities;
}
@Override
public boolean hasChild() {
return attributes.size() != 0;
}
@Override
public boolean hasData() {
return hasValue;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EntityImpl other = (EntityImpl) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString(Set alreadyWritten) {
StringBuilder builder = new StringBuilder();
builder.append(name);
if (!alreadyWritten.contains(this.getName().getName())) {
builder.append("\n");
alreadyWritten.add(this.getName().getName());
for (Attribute att : attributes) {
String attString = att.toString(alreadyWritten);
attString = attString.replace("\n", "\n ");
builder.append(" ");
builder.append(attString);
builder.append("\n");
}
} else {
builder.append(".../n");
}
return builder.toString();
}
@Override
public String toString() {
return toString(new HashSet<>());
}
}