Merge "Address comments at ag/18372083"
diff --git a/staticlibs/framework/com/android/net/module/util/PerUidCounter.java b/staticlibs/framework/com/android/net/module/util/PerUidCounter.java
index 7e0526d..0b2de7a 100644
--- a/staticlibs/framework/com/android/net/module/util/PerUidCounter.java
+++ b/staticlibs/framework/com/android/net/module/util/PerUidCounter.java
@@ -41,8 +41,8 @@
      * @param maxCountPerUid the maximum count per uid allowed
      */
     public PerUidCounter(final int maxCountPerUid) {
-        if (maxCountPerUid < 0) {
-            throw new IllegalArgumentException("Maximum counter value cannot be negative");
+        if (maxCountPerUid <= 0) {
+            throw new IllegalArgumentException("Maximum counter value must be positive");
         }
         mMaxCountPerUid = maxCountPerUid;
     }
@@ -58,12 +58,10 @@
      * @param uid the uid that the counter was made under
      */
     public void incrementCountOrThrow(final int uid) {
-        synchronized (mUidToCount) {
-            incrementCountOrThrow(uid, 1 /* numToIncrement */);
-        }
+        incrementCountOrThrow(uid, 1 /* numToIncrement */);
     }
 
-    public void incrementCountOrThrow(final int uid, final int numToIncrement) {
+    public synchronized void incrementCountOrThrow(final int uid, final int numToIncrement) {
         if (numToIncrement <= 0) {
             throw new IllegalArgumentException("Increment count must be positive");
         }
@@ -71,8 +69,8 @@
         if (newCount > mMaxCountPerUid) {
             throw new IllegalStateException("Uid " + uid + " exceeded its allowed limit");
         }
-        // Since the count cannot be greater than Integer.MAX_VALUE here,
-        // it is safe to cast to int.
+        // Since the count cannot be greater than Integer.MAX_VALUE here since mMaxCountPerUid
+        // is an integer, it is safe to cast to int.
         mUidToCount.put(uid, (int) newCount);
     }
 
@@ -86,12 +84,10 @@
      * @param uid the uid that the count was made under
      */
     public void decrementCountOrThrow(final int uid) {
-        synchronized (mUidToCount) {
-            decrementCountOrThrow(uid, 1 /* numToDecrement */);
-        }
+        decrementCountOrThrow(uid, 1 /* numToDecrement */);
     }
 
-    public void decrementCountOrThrow(final int uid, final int numToDecrement) {
+    public synchronized void decrementCountOrThrow(final int uid, final int numToDecrement) {
         if (numToDecrement <= 0) {
             throw new IllegalArgumentException("Decrement count must be positive");
         }
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/PerUidCounterTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/PerUidCounterTest.kt
index c479d81..0f2d52a 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/PerUidCounterTest.kt
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/PerUidCounterTest.kt
@@ -33,13 +33,8 @@
         assertFailsWith<IllegalArgumentException> {
             PerUidCounter(-1)
         }
-
-        val uselessCounter = PerUidCounter(0)
-        assertFailsWith<IllegalStateException> {
-            uselessCounter.incrementCountOrThrow(UID_A)
-        }
-        assertFailsWith<IllegalStateException> {
-            uselessCounter.decrementCountOrThrow(UID_A)
+        assertFailsWith<IllegalArgumentException> {
+            PerUidCounter(0)
         }
 
         val largeMaxCounter = PerUidCounter(Integer.MAX_VALUE)