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

com.vtence.molecule.routing.DynamicPath Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package com.vtence.molecule.routing;

import com.vtence.molecule.lib.Matcher;

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

public class DynamicPath implements Matcher, WithBoundParameters {

    private final Path pattern;

    public DynamicPath(String pattern) {
        this.pattern = new Path(pattern);
    }

    public boolean matches(String actual) {
        Path path = new Path(actual);
        if (!pattern.sameLengthAs(path)) return false;

        for (int i = 0; i < pattern.segmentCount(); i++) {
            if (!isDynamic(pattern.segment(i)) && !pattern.segment(i).equals(path.segment(i)))
                return false;
        }
        return true;
    }

    private boolean isDynamic(String segment) {
        return segment.startsWith(":");
    }

    public Map parametersBoundTo(String path) {
        Path p = new Path(path);
        Map boundParameters = new HashMap();

        for (int i = 0; i < pattern.segmentCount(); i++) {
            String segment = pattern.segment(i);
            if (isDynamic(segment)) {
                boundParameters.put(stripLeadingColon(segment), p.segment(i));
            }
        }
        return boundParameters;
    }

    private String stripLeadingColon(String segment) {
        return segment.substring(1);
    }

    public static class Path {
        private final String path;

        public Path(String path) {
            this.path = path;
        }

        public String[] segments() {
            return removeEmptyParts(path.split("/"));
        }

        private static String[] removeEmptyParts(String[] parts) {
            List segments = new ArrayList();
            for (String part : parts) {
                if (!part.isEmpty()) segments.add(part);
            }
            return segments.toArray(new String[segments.size()]);
        }

        public boolean sameLengthAs(Path other) {
            return other.segments().length == segments().length;
        }

        public String segment(int index) {
            return segments()[index];
        }

        public int segmentCount() {
            return segments().length;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy