com.regnosys.rosetta.translate.synonymmap.SynonymMap Maven / Gradle / Ivy
package com.regnosys.rosetta.translate.synonymmap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.regnosys.rosetta.generator.object.ExpandedAttribute;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.RosettaMergeSynonymValue;
import com.regnosys.rosetta.rosetta.RosettaType;
import com.regnosys.rosetta.translate.datamodel.Entity;
public class SynonymMap implements Comparable {
private final RosettaType rosetta;
private final SynonymMap superMap;// Inheritance parent
// paths that need to be captured to make conditional decisions;
private final Multimap conditionalCaptures;
private final Map attributeGroupMap;
private final Multimap attributeMergeSynonyms;
private final Set linkedEntities = new HashSet<>();
public SynonymMap(RosettaType rosetta) {
this(rosetta, null);
}
public SynonymMap() {
this(null, null);
}
public SynonymMap(RosettaType rosetta, SynonymMap superMap) {
this.rosetta = rosetta;
this.superMap = superMap;
this.attributeGroupMap = new LinkedHashMap<>();
this.conditionalCaptures = ArrayListMultimap.create();
this.attributeMergeSynonyms = HashMultimap.create();
}
public RosettaType getRosetta() {
return rosetta;
}
public SynonymMap getSuperMap() {
return superMap;
}
public List getMappedGroups() {
return attributeGroupMap.entrySet().stream().map(x -> new AttributeGroupMapping(x.getKey(), x.getValue()))
.collect(Collectors.toList());
}
public Collection childMappings() {
return attributeGroupMap.values();
}
public void addMappings(Map mappings) {
attributeGroupMap.putAll(mappings);
}
public void addMapping(AttributeGroup group, SynonymMap mapping) {
attributeGroupMap.put(group, mapping);
}
public Collection attributeGroups() {
return attributeGroupMap.keySet();
}
public Multimap getConditionalCaptures() {
return conditionalCaptures;
}
public Multimap getMergeSynonyms() {
return attributeMergeSynonyms;
}
public Set getLinkedEntities() {
return linkedEntities;
}
@Override
public String toString() {
return toString(new HashSet<>());
}
public String toString(Set alreadyWritten) {
String name = rosettaName();
if (!alreadyWritten.contains(name)) {
alreadyWritten.add(name);
return "SynonymMap [rosetta=" + name + ", map="
+ attributeGroupMap.entrySet().stream()
.map(me -> me.getKey() + "=" + me.getValue().toString(alreadyWritten))
.map(s -> s.replaceAll("\n", "\n\t")).collect(Collectors.joining(",\n", "\n{", "}"))
+ "]";
}
return name + "...";
}
public String rosettaName() {
return rosetta == null ? "Basic" : rosetta.getName();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((rosetta == null) ? 0 : rosetta.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;
SynonymMap other = (SynonymMap) obj;
if (rosetta == null) {
if (other.rosetta != null)
return false;
} else if (!rosetta.equals(other.rosetta))
return false;
return true;
}
@Override
public int compareTo(SynonymMap o) {
if (this.rosetta == o.rosetta)
return 0;
if (this.rosetta == null && o.rosetta != null)
return -1;
if (this.rosetta != null && o.rosetta == null)
return 1;
return this.rosetta.getName().compareTo(o.rosetta.getName());
}
protected Map getAttributeGroupMap() {
return attributeGroupMap;
}
public List getForClass(String className) {
return getForClass(className, new HashSet<>());
}
public List getForClass(String className, Set searched) {
if (searched.contains(this))
return Collections.emptyList();
searched.add(this);
List result = new ArrayList<>();
if (this.rosetta != null && this.rosetta.getName().equals(className)) {
result.add(this);
}
for (SynonymMap sub : attributeGroupMap.values()) {
result.addAll(sub.getForClass(className, searched));
}
return result;
}
public Optional> getMappingFor(String path) {
return attributeGroupMap.entrySet().stream().filter(e -> e.getKey().attsToString().equals(path)).findFirst();
}
public boolean isBasic() {
return rosetta == null || rosetta instanceof RosettaEnumeration;
}
public long hashForGeneration() {
long result = 0;
result = result * 8191 + (rosetta==null ? 0 : rosetta.getName().hashCode());
result = result * 8191 + (superMap==null ? 0 : superMap.hashForGeneration());
result = result * 8191 + conditionalCaptures.hashCode();
result *= 8191;
//avoid the recursion that has loops in and make it order agnostic by no multiplying at every step
for (Entry entry:attributeGroupMap.entrySet()) {
result += entry.getKey().hashForGeneration();
RosettaType nextType = entry.getValue().getRosetta();
result += (nextType==null ? 0 : nextType.getName().hashCode());
}
return result;
}
}