All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.translate.GeneratorPathUtil Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import com.regnosys.rosetta.common.translation.Path;
import com.regnosys.rosetta.common.translation.Path.PathElement;

public class GeneratorPathUtil {

	public static boolean anyMatch(Path p, List paths) {
		return paths.stream().anyMatch(ps->p.endsWith(ps));
	}

	public static  Optional getValueForPath(Map values, Collection paths) {
		return values.entrySet().stream()
				.filter(e->matches(e.getKey(), paths))
				.map(e->e.getValue())
				.findFirst();
	}
	
	public static  Collection getValuesForPathEnding(Map values, List paths) {
		return values.entrySet().stream()
				.filter(e->endsWith(e.getKey(), paths))
				.map(e->e.getValue())
				.collect(Collectors.toList());
	}

	public static boolean matches(Set paths, Collection path) {
		return path.stream().map(Path::parse).anyMatch(p->paths.stream().anyMatch(k->p.nameStartMatches(k)));
	}
	
	private static boolean matches(Path path, Collection paths) {
		return paths.stream().map(Path::parse).anyMatch(k->path.nameStartMatches(k));
	}

	public static boolean matches(Path rosettaPath, String matchString) {
		Path matchPath = Path.parse(matchString);
		List elements = matchPath.getElements();
		List rElements = rosettaPath.getElements();
		if (rElements.size() matchStrings) {
		return matchStrings.stream().anyMatch(m->endsWith(rosettaPath, m));
	}
	
	public static boolean endsWith(Path rosettaPath, String matchString) {
		return withoutValue(rosettaPath).endsWith(matchString.split("\\."));
	}
	
	private static Path withoutValue(Path path) {
		return new Path(path.getElements().stream().filter(e->!e.getPathName().equals("value")).collect(Collectors.toList()));
	}
	
	//returns true if the near branch is a branch of the full path (based mostly on matching indexes)
	public static boolean sameBranchMatches(Path fullPath, Path nearBranch) {
		//find branching point
		int branchPoint= fullPath.getElements().size()-1;
		while (branchPoint>=0 && !fullPath.getElements().get(branchPoint).getPathName().equals(nearBranch.getElements().get(0).getPathName())) {
			branchPoint--;
		}
		if (branchPoint<0) return false;//there isn't any common point between these branches
		
		//else we have found out branching point
		//the remainder of the path (until the path splits) must have matching indexes
		int i=0;
		
		while (true) {
			if (i>=nearBranch.getElements().size()) return true;//they matched all the way to the end of one
			if (i>=fullPath.getElements().size()-branchPoint) return true;
			
			PathElement fullElement = fullPath.getElements().get(branchPoint+i);
			PathElement branchElement = fullPath.getElements().get(i);
			if (!fullElement.getPathName().equals(branchElement.getPathName())) return true;//they remained matching untill the split point
			if (!fullElement.getIndex().equals(branchElement.getIndex())) return false;//path and branch have different indexes - they dont match
			
			i++;
		}
	}
	
	public static  Set getPathMatchingValues(Set itemPaths, Map valueCache) {
		Set result = new HashSet<>();
		for (Path itemPath:itemPaths) {
			for (Entry entry:valueCache.entrySet()) {
				if (sameBranchMatches(itemPath, entry.getKey())) {
					result.add(entry.getValue());
				}
			}
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy