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

com.sigopt.net.PathBuilder Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package com.sigopt.net;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class PathBuilder {
    public static String build(String path, Map params) {
        params = MapHelper.ensure(params);

        Pattern r = Pattern.compile(":([^\\/]*)");
        Matcher m = r.matcher(path);
        while(m.find()) {
            String match = m.group(1);
            int start = m.start();
            int end = start + match.length() + 1; // +1 for :
            path = path.substring(0, start) + determineValue(match, params) + path.substring(end, path.length());
            m = r.matcher(path);
        }
        return path;
    }

    public static String determineValue(String match, Map params) {
        String param = params.get(match);
        if (param == null) {
            throw new NullPointerException("Missing required parameter: " + match);
        }
        return param;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy