001package io.avaje.http.api; 002 003import java.util.HashMap; 004import java.util.Map; 005 006/** 007 * A path segment that can simple like value like <code>chair</code> 008 * or contain matrix parameter values using semi-colon delimitation 009 * like <code>chair;vendor=ikea;size=small</code>. 010 * <p> 011 * Matrix parameters are optional 'qualifiers' of the path segment. 012 */ 013public final class PathSegment { 014 015 private final String val; 016 017 private Map<String, String> matrixValues; 018 019 /** 020 * Create with a given value that may contain matrix parameters. 021 */ 022 public static PathSegment of(String value) { 023 return new PathSegment(value); 024 } 025 026 /** 027 * Create with a given value that may contain matric parameters. 028 */ 029 public PathSegment(String value) { 030 String[] vals = value.split(";"); 031 this.val = vals[0]; 032 if (vals.length > 1) { 033 matrixValues = new HashMap<>(); 034 for (String val : vals) { 035 String[] keyVal = val.split("="); 036 if (keyVal.length == 2) { 037 matrixValues.put(keyVal[0], keyVal[1]); 038 } 039 } 040 } 041 } 042 043 /** 044 * Return the main segment value. 045 * <p> 046 * For "chair" this returns "chair" 047 * <p> 048 * For "chair;vendor=ikea;size=small" this returns "chair" 049 */ 050 public String val() { 051 return val; 052 } 053 054 /** 055 * Return a metric value for the given key. 056 * <p> 057 * For example, given "chair;vendor=ikea;size=small" 058 * <p> 059 * matrix("vendor") returns "ikea". 060 * 061 * @param key The matrix key 062 * @return The matrix value if supplied or null 063 */ 064 public String matrix(String key) { 065 return matrixValues == null ? null : matrixValues.get(key); 066 } 067 068}