Merge "Replace possibly-expensive size() == 0 with isEmpty()"

This commit is contained in:
TreeHugger Robot
2017-03-27 17:15:20 +00:00
committed by Android (Google) Code Review
7 changed files with 70 additions and 8 deletions

View File

@@ -150,7 +150,7 @@ public class ClipboardManager extends android.text.ClipboardManager {
public void addPrimaryClipChangedListener(OnPrimaryClipChangedListener what) {
synchronized (mPrimaryClipChangedListeners) {
if (mPrimaryClipChangedListeners.size() == 0) {
if (mPrimaryClipChangedListeners.isEmpty()) {
try {
mService.addPrimaryClipChangedListener(
mPrimaryClipChangedServiceListener, mContext.getOpPackageName());
@@ -165,7 +165,7 @@ public class ClipboardManager extends android.text.ClipboardManager {
public void removePrimaryClipChangedListener(OnPrimaryClipChangedListener what) {
synchronized (mPrimaryClipChangedListeners) {
mPrimaryClipChangedListeners.remove(what);
if (mPrimaryClipChangedListeners.size() == 0) {
if (mPrimaryClipChangedListeners.isEmpty()) {
try {
mService.removePrimaryClipChangedListener(
mPrimaryClipChangedServiceListener);

View File

@@ -508,14 +508,14 @@ public class ContentProviderOperation implements Parcelable {
/** Create a ContentProviderOperation from this {@link Builder}. */
public ContentProviderOperation build() {
if (mType == TYPE_UPDATE) {
if ((mValues == null || mValues.size() == 0)
&& (mValuesBackReferences == null || mValuesBackReferences.size() == 0)) {
if ((mValues == null || mValues.isEmpty())
&& (mValuesBackReferences == null || mValuesBackReferences.isEmpty())) {
throw new IllegalArgumentException("Empty values");
}
}
if (mType == TYPE_ASSERT) {
if ((mValues == null || mValues.size() == 0)
&& (mValuesBackReferences == null || mValuesBackReferences.size() == 0)
if ((mValues == null || mValues.isEmpty())
&& (mValuesBackReferences == null || mValuesBackReferences.isEmpty())
&& (mExpectedCount == null)) {
throw new IllegalArgumentException("Empty values");
}

View File

@@ -203,6 +203,17 @@ public final class ContentValues implements Parcelable {
return mValues.size();
}
/**
* Indicates whether this collection is empty.
*
* @return true iff size == 0
* {@hide}
* TODO: consider exposing this new method publicly
*/
public boolean isEmpty() {
return mValues.isEmpty();
}
/**
* Remove a single value.
*

View File

@@ -1449,7 +1449,7 @@ public final class SQLiteDatabase extends SQLiteClosable {
sql.append('(');
Object[] bindArgs = null;
int size = (initialValues != null && initialValues.size() > 0)
int size = (initialValues != null && !initialValues.isEmpty())
? initialValues.size() : 0;
if (size > 0) {
bindArgs = new Object[size];
@@ -1541,7 +1541,7 @@ public final class SQLiteDatabase extends SQLiteClosable {
*/
public int updateWithOnConflict(String table, ContentValues values,
String whereClause, String[] whereArgs, int conflictAlgorithm) {
if (values == null || values.size() == 0) {
if (values == null || values.isEmpty()) {
throw new IllegalArgumentException("Empty values");
}

View File

@@ -45,6 +45,10 @@ To run tests in debug mode:
-e debug true
To uninstall the package:
adb shell pm uninstall -k com.android.frameworks.coretests
For more arguments, see the guide to command=line testing:
https://developer.android.com/studio/test/command-line.html

View File

@@ -23,6 +23,7 @@ public class ContentTests {
TestSuite suite = new TestSuite(ContentTests.class.getName());
suite.addTestSuite(AssetTest.class);
suite.addTestSuite(ContentValuesTest.class);
return suite;
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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 android.content;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
/*
runtest -c android.content.ContentValuesTest frameworks-core
or
make -j256 FrameworksCoreTests && \
adb shell pm uninstall -k com.android.frameworks.coretests && \
adb install out/target/product/bullhead/testcases/FrameworksCoreTests/FrameworksCoreTests.apk && \
adb shell am instrument -w -e package android.content \
com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner
*/
public class ContentValuesTest extends AndroidTestCase {
@SmallTest
public void testIsEmpty() throws Exception {
ContentValues cv = new ContentValues();
assertTrue(cv.isEmpty());
assertEquals(0, cv.size());
cv.put("key", "value");
assertFalse(cv.isEmpty());
assertEquals(1, cv.size());
}
}