Java Project Source Code learning Note (c): Pathmatcher

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.