com.regnosys.rosetta.translate.synonymmap.AttributeGroup Maven / Gradle / Ivy
package com.regnosys.rosetta.translate.synonymmap;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.regnosys.rosetta.generator.object.ExpandedAttribute;
public class AttributeGroup implements Comparable {
/**
* Single path, defined as a list of attributes
*/
private final List attributePath;
/**
* List of synonym groups. Each attribute can have multiple synonym rows
* associated with it
*/
private final List synonymGroups;
public AttributeGroup(List synonymGroups, List attributePath) {
//this.synonymGroups = synonymGroups;
this.attributePath = attributePath;
Set sgSet = new LinkedHashSet<>(synonymGroups);//pass the groups through a set in order to remove duplicates
this.synonymGroups = new ArrayList<>(sgSet);
}
public List getSynonymGroups() {
return synonymGroups;
}
public List getAttributePath() {
return attributePath;
}
public String attsToString() {
return attributePath.stream().map(att -> attToString(att)).collect(Collectors.joining(", "));
}
private String attToString(ExpandedAttribute att) {
return att.getName() + (att.getSup() > 1 ? "*" : "");
}
public static String attributeName(ExpandedAttribute att) {
String name = att.getName();
return name;
}
@Override
public String toString() {
String atts = attsToString();
return synonymGroups + "[" + atts + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((attributePath == null) ? 0 : attributePath.hashCode());
result = prime * result + ((synonymGroups == null) ? 0 : synonymGroups.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;
AttributeGroup other = (AttributeGroup) obj;
if (attributePath == null) {
if (other.attributePath != null)
return false;
} else if (!attributePath.equals(other.attributePath))
return false;
if (synonymGroups == null) {
if (other.synonymGroups != null)
return false;
} else if (!synonymGroups.equals(other.synonymGroups))
return false;
return true;
}
@Override
public int compareTo(AttributeGroup l2) {
List atp1 = attributePath;
List atp2 = l2.attributePath;
int minLen = Math.min(atp1.size(), atp2.size());
for (int i = 0; i < minLen; i++) {
ExpandedAttribute at1 = atp1.get(i);
ExpandedAttribute at2 = atp2.get(i);
int comp = at1.getName().compareTo(at2.getName());
if (comp != 0)
return comp;
}
return synonymGroups.toString().compareTo(l2.synonymGroups.toString());
}
public boolean startsWith(String string) {
return attributePath.size() > 0 && attributePath.get(0).getName().equals(string);
}
public long hashForGeneration() {
long result = 0;
for (ExpandedAttribute expandedAttribute : attributePath) {
result += expandedAttribute.getName().hashCode();
String enclosingType = expandedAttribute.getEnclosingType();
if (enclosingType!=null) {
result += enclosingType.hashCode();
}
}
result *= 8191;
for (SynonymGroup sg : synonymGroups) {
result += sg.hashForGeneration();
}
return result;
}
}