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

ca.appsimulations.models.model.application.CallResolver Maven / Gradle / Ivy

The newest version!
package ca.appsimulations.models.model.application;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

@Data
@NoArgsConstructor
@Accessors(chain = true, fluent = true)
public class CallResolver {

    private Map> callsToMap = new HashMap<>();
    private Map> calledByMap = new HashMap<>();

    public void calls(String name, Call call) {
        addCallToMap(name, call, callsToMap);
    }

    public void calledBy(String name, Call call) {
        addCallToMap(name, call, calledByMap);
    }

    private void addCallToMap(String name,
                              Call call,
                              Map> callsMap) {
        List calls;
        if (callsMap.containsKey(name)) {
            calls = callsMap.get(name);
        }
        else {
            calls = new ArrayList<>();
        }
        calls.add(call);
        callsMap.put(name,
                     calls);
    }

    public List calledBy() {
        return calledByMap.values().stream().flatMap(calls -> calls.stream())
                .collect(toList());
    }

    public List callsTo() {
        return callsToMap.values().stream().flatMap(calls -> calls.stream())
                .collect(toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy