Back to project page android-memento.
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.
/* * android-memento-lib https://github.com/twofortyfouram/android-memento * Copyright 2014 two forty four a.m. LLC *//from w w w . j a va 2 s .c o m * 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.provider; import com.twofortyfouram.annotation.Slow; import com.twofortyfouram.annotation.Slow.Speed; import com.twofortyfouram.assertion.Assertions; import com.twofortyfouram.log.Lumberjack; import net.jcip.annotations.ThreadSafe; import android.content.ContentProvider; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.BaseColumns; import android.support.annotation.NonNull; /** * Utility class for working with {@link ContentProvider}. */ @ThreadSafe public final class ContentProviderUtil { /** * Projection for {@link BaseColumns#_COUNT}. */ @NonNull private static final String[] PROJECTION_COUNT = { BaseColumns._COUNT }; /** * @param context Application context. * @param uri URI to count. * @return The number of rows for {@code uri}. */ @Slow(Speed.MILLISECONDS) public static int getCountForUri(@NonNull final Context context, @NonNull final Uri uri) { Assertions.assertNotNull(context, "context"); //$NON-NLS-1$ Assertions.assertNotNull(uri, "uri"); //$NON-NLS-1$ int result = 0; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, PROJECTION_COUNT, null, null, null); if (null != cursor) { final int cursorCount = cursor.getCount(); if (1 != cursorCount) { final String message = Lumberjack.formatMessage( "Row count should be 1 but was actually %d", cursorCount); //$NON-NLS-1$ throw new AssertionError(message); } cursor.moveToPosition(0); result = cursor.getInt(cursor.getColumnIndexOrThrow(BaseColumns._COUNT)); } } finally { if (null != cursor) { cursor.close(); cursor = null; } } return result; } /** * Private constructor prevents instantiation. * * @throws UnsupportedOperationException because this class cannot be instantiated. */ private ContentProviderUtil() { throw new UnsupportedOperationException("This class is non-instantiable"); //$NON-NLS-1$ } }