Let's take a look at the Inner class node nodes:
private static Class Node{private hashmap<string, node> mmap;private int mkind = Not_found; Node addChild (String segment) {if (null = = MMap) {mMap = new hashmap<string, node> ();} Else{node node = mmap.get (segment); if (node! = null) return Node; node n = new node (); Mmap.put (segment, n); return n;} Node getchild (String segment) {if (MMap = = null) return Null;return mmap.get (segment);} void setkind (int kind) {mkind = kind;} int Getkind () {return mkind;}}
Obviously, this is a one-way linked list data structure:
Where the node object in Mmap points to the next node object.
Import Java.util.arraylist;import Java.util.hashmap;public class pathmatcher{public static final int not_found =-1; Private arraylist<string> mvariables = new arraylist<string> ();p rivate node Mroot = new node ();p ublic Pathmat Cher () {mroot = new Node ();} public void Add (String pattern, int kind) {string[] segments = path.split (pattern); Node current = mroot;for (int i = 0; i < segments.length; i++) {current = Current.addchild (Segments[i]);} Current.setkind (kind);} public int Match (path path) {string[] segments = Path.split (); Mvariables.clear (); Node current = mroot;for (int i = 0; i < segments.length; i++) {Node next = Current.getchild (Segments[i]); if (null = = Next {next = Current.getchild ("*"), if (next! = null) {Mvariables.add (segments[i]);} Else{return Not_found;}} current = Next;} return Current.getkind ();}}
Add constructs a one-way list with the root node as mroot, and the reason that node uses HashMap to save the next node in the list is because Mroot a root node points to many of the one-way linked list branches. Mkind is the key value for each single-linked list branch, which is assigned to the last node.
The last segment of the Path string is "*", matches any character, and the character is saved by Mvariables.
Java Project Source Code learning Note (c): Pathmatcher