Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.data;
     18 
     19 import java.util.ArrayList;
     20 import java.util.HashMap;
     21 
     22 public class PathMatcher {
     23     public static final int NOT_FOUND = -1;
     24 
     25     private ArrayList<String> mVariables = new ArrayList<String>();
     26     private Node mRoot = new Node();
     27 
     28     public PathMatcher() {
     29         mRoot = new Node();
     30     }
     31 
     32     public void add(String pattern, int kind) {
     33         String[] segments = Path.split(pattern);
     34         Node current = mRoot;
     35         for (int i = 0; i < segments.length; i++) {
     36             current = current.addChild(segments[i]);
     37         }
     38         current.setKind(kind);
     39     }
     40 
     41     public int match(Path path) {
     42         String[] segments = path.split();
     43         mVariables.clear();
     44         Node current = mRoot;
     45         for (int i = 0; i < segments.length; i++) {
     46             Node next = current.getChild(segments[i]);
     47             if (next == null) {
     48                 next = current.getChild("*");
     49                 if (next != null) {
     50                     mVariables.add(segments[i]);
     51                 } else {
     52                     return NOT_FOUND;
     53                 }
     54             }
     55             current = next;
     56         }
     57         return current.getKind();
     58     }
     59 
     60     public String getVar(int index) {
     61         return mVariables.get(index);
     62     }
     63 
     64     public int getIntVar(int index) {
     65         return Integer.parseInt(mVariables.get(index));
     66     }
     67 
     68     public long getLongVar(int index) {
     69         return Long.parseLong(mVariables.get(index));
     70     }
     71 
     72     private static class Node {
     73         private HashMap<String, Node> mMap;
     74         private int mKind = NOT_FOUND;
     75 
     76         Node addChild(String segment) {
     77             if (mMap == null) {
     78                 mMap = new HashMap<String, Node>();
     79             } else {
     80                 Node node = mMap.get(segment);
     81                 if (node != null) return node;
     82             }
     83 
     84             Node n = new Node();
     85             mMap.put(segment, n);
     86             return n;
     87         }
     88 
     89         Node getChild(String segment) {
     90             if (mMap == null) return null;
     91             return mMap.get(segment);
     92         }
     93 
     94         void setKind(int kind) {
     95             mKind = kind;
     96         }
     97 
     98         int getKind() {
     99             return mKind;
    100         }
    101     }
    102 }
    103