1 /* 2 * Copyright (C) 2011 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 package com.android.providers.contacts.util; 17 18 import android.content.UriMatcher; 19 import android.net.Uri; 20 21 /** 22 * Implementation of {@link TypedUriMatcher}. 23 * 24 * @param <T> the type of the URI 25 */ 26 public class TypedUriMatcherImpl<T extends UriType> implements TypedUriMatcher<T> { 27 private final String mAuthority; 28 private final T[] mValues; 29 private final T mNoMatchUriType; 30 private final UriMatcher mUriMatcher; 31 32 public TypedUriMatcherImpl(String authority, T[] values) { 33 mAuthority = authority; 34 mValues = values; 35 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 36 T candidateNoMatchUriType = null; 37 for (T value : values) { 38 String path = value.path(); 39 if (path != null) { 40 addUriType(path, value); 41 } else { 42 candidateNoMatchUriType = value; 43 } 44 } 45 this.mNoMatchUriType = candidateNoMatchUriType; 46 } 47 48 private void addUriType(String path, T value) { 49 mUriMatcher.addURI(mAuthority, path, value.ordinal()); 50 } 51 52 @Override 53 public T match(Uri uri) { 54 int match = mUriMatcher.match(uri); 55 if (match == UriMatcher.NO_MATCH) { 56 return mNoMatchUriType; 57 } 58 return mValues[match]; 59 } 60 } 61