Android Open Source - android-memento Sqlite Uri Matcher Impl






From Project

Back to project page android-memento.

License

The source code is released under:

Apache License

If you think the Android project android-memento listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * android-memento-lib https://github.com/twofortyfouram/android-memento
 * Copyright 2014 two forty four a.m. LLC
 */*from w  ww  .ja va 2 s .  com*/
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twofortyfouram.memento.debug.provider;

import com.twofortyfouram.assertion.Assertions;
import com.twofortyfouram.log.Lumberjack;
import com.twofortyfouram.memento.provider.ImmutableUriMatcher;
import com.twofortyfouram.memento.provider.sqlite.SqliteUriMatch;
import com.twofortyfouram.memento.provider.sqlite.SqliteUriMatcher;
import com.twofortyfouram.spackle.util.AndroidSdkVersion;
import com.twofortyfouram.spackle.util.ContextUtil;

import net.jcip.annotations.Immutable;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.UriMatcher;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.ArrayMap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Matcher for the Content Provider.
 */
@Immutable
public final class SqliteUriMatcherImpl implements SqliteUriMatcher {

    private static final int MATCH_TABLE_ONE_DIR = 0;

    private static final int MATCH_TABLE_ONE_ITEM = 1;

    @NonNull
    private final ImmutableUriMatcher mUriMatcher;

    @NonNull
    private final SqliteUriMatch mTableOneDirMatch;

    @NonNull
    private final SqliteUriMatch mTableOneItemMatch;

    /**
     * Construct a new matcher.
     */
    public SqliteUriMatcherImpl(@NonNull final Context context) {
        Assertions.assertNotNull(context, "context"); //$NON-NLS-1$
        final Context ctx = ContextUtil.cleanContext(context);

        mUriMatcher = new ImmutableUriMatcher(newUriMatcher(ctx));

        /*
         * Although a new match could be allocated each time #match(Uri) is called, let's avoid those
         * extra allocations for every call to the ContentProvider.
         */
        mTableOneDirMatch = newTableOneDirMatch(ctx);
        mTableOneItemMatch = newTableOneItemMatch(ctx);
    }

    @NonNull
    @Override
    public SqliteUriMatch match(@NonNull final Uri uri) {
        switch (mUriMatcher.match(uri)) {
            case MATCH_TABLE_ONE_DIR: {
                return mTableOneDirMatch;
            }
            case MATCH_TABLE_ONE_ITEM: {
                return mTableOneItemMatch;
            }
            default: {
                throw new IllegalArgumentException(Lumberjack.formatMessage(
                        "URI %s is unrecognized", uri)); //$NON-NLS-1$
            }
        }
    }

    @NonNull
    private static UriMatcher newUriMatcher(@NonNull final Context context) {
        final String contentAuthority = SqliteContentProviderImpl.getContentAuthority(context);
        final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(contentAuthority, TableOneContract.TABLE_NAME, MATCH_TABLE_ONE_DIR);
        matcher.addURI(contentAuthority,
                TableOneContract.TABLE_NAME + "/#", MATCH_TABLE_ONE_ITEM); //$NON-NLS-1$

        return matcher;
    }

    /**
     * @param context Application context.
     * @return A new match object for an item in {@link com.twofortyfouram.memento.debug.provider.TableOneContract}.
     */
    @NonNull
    private static SqliteUriMatch newTableOneDirMatch(@NonNull final Context context) {
        final Uri baseUri = TableOneContract.getContentUri(context);

        final Collection<Uri> notifyUris = new ArrayList<Uri>(1);
        notifyUris.add(baseUri);

        final String tableName = TableOneContract.TABLE_NAME;
        final String mimeType = TableOneContract.MIMETYPE_DIR;
        final boolean isIdUri = false;

        final Map<String, String> projectionMap = newTableOneProjectionMap();

        return new SqliteUriMatch(baseUri, notifyUris, tableName, mimeType, isIdUri,
                projectionMap);
    }

    /**
     * @param context Application context.
     * @return A new match object for an item in {@link com.twofortyfouram.memento.debug.provider.TableOneContract}.
     */
    @NonNull
    private static SqliteUriMatch newTableOneItemMatch(@NonNull final Context context) {
        final Uri baseUri = TableOneContract.getContentUri(context);

        final Collection<Uri> notifyUris = new ArrayList<Uri>(1);
        notifyUris.add(baseUri);

        final String tableName = TableOneContract.TABLE_NAME;
        final String mimeType = TableOneContract.MIMETYPE_ITEM;
        final boolean isIdUri = true;

        final Map<String, String> projectionMap = newTableOneProjectionMap();

        return new SqliteUriMatch(baseUri, notifyUris, tableName, mimeType, isIdUri,
                projectionMap);
    }

    /**
     * @return A new projection map for {@link com.twofortyfouram.memento.debug.provider.TableOneContract}.
     */
    @SuppressLint("NewApi")
    @NonNull
    private static Map<String, String> newTableOneProjectionMap() {
        final Map<String, String> projectionMap;
        if (AndroidSdkVersion.isAtLeastSdk(Build.VERSION_CODES.KITKAT)) {
            projectionMap = new ArrayMap<String, String>(2);
        } else {
            projectionMap = new HashMap<String, String>();
        }

        projectionMap.put(TableOneContract._ID, TableOneContract._ID);
        projectionMap.put(TableOneContract.COLUMN_STRING_COLUMN_ONE,
                TableOneContract.COLUMN_STRING_COLUMN_ONE);

        return projectionMap;
    }
}




Java Source Code List

com.twofortyfouram.memento.debug.provider.SqliteContentProviderImpl.java
com.twofortyfouram.memento.debug.provider.SqliteOpenHelperImpl.java
com.twofortyfouram.memento.debug.provider.SqliteUriMatcherImpl.java
com.twofortyfouram.memento.debug.provider.TableOneContract.java
com.twofortyfouram.memento.provider.ContentChangeNotificationQueueTest.java
com.twofortyfouram.memento.provider.ContentChangeNotificationQueue.java
com.twofortyfouram.memento.provider.ContentProviderOperationServiceTest.java
com.twofortyfouram.memento.provider.ContentProviderOperationService.java
com.twofortyfouram.memento.provider.ContentProviderUtilTest.java
com.twofortyfouram.memento.provider.ContentProviderUtil.java
com.twofortyfouram.memento.provider.ImmutableUriMatcherTest.java
com.twofortyfouram.memento.provider.ImmutableUriMatcher.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProviderIntegrationTest.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProviderTest.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProvider.java
com.twofortyfouram.memento.provider.sqlite.SqliteColumnBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteColumnBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteIndexBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteIndexBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteOpenHelperCompat.java
com.twofortyfouram.memento.provider.sqlite.SqliteStorageClass.java
com.twofortyfouram.memento.provider.sqlite.SqliteTableBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteTableBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatchTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatch.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatcher.java