From 1cad2ea6518cda5bd7031c80b529c466674b98bc Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Fri, 14 Jan 2022 23:34:54 +0800 Subject: [PATCH] Have NetworkStats implements iterable Make NetworkStats be iterable and expose iterator as system API which is better than expose size() and getValues(). This API could be used by the caller who needs to get entry from NetworkStats. Bug: 210073043 CTS-Coverage-Bug: 215061403 Test: build, TH Change-Id: I6cb4c8f63d7067133f2722b2be7e16b4098a697d --- framework-t/src/android/net/NetworkStats.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/framework-t/src/android/net/NetworkStats.java b/framework-t/src/android/net/NetworkStats.java index 9d532e7929..9175809d9c 100644 --- a/framework-t/src/android/net/NetworkStats.java +++ b/framework-t/src/android/net/NetworkStats.java @@ -41,6 +41,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Arrays; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -57,7 +58,7 @@ import java.util.function.Predicate; */ // @NotThreadSafe @SystemApi -public final class NetworkStats implements Parcelable { +public final class NetworkStats implements Parcelable, Iterable { private static final String TAG = "NetworkStats"; /** @@ -677,6 +678,35 @@ public final class NetworkStats implements Parcelable { operations[i] = entry.operations; } + /** + * Iterate over Entry objects. + * + * Return an iterator of this object that will iterate through all contained Entry objects. + * + * This iterator does not support concurrent modification and makes no guarantee of fail-fast + * behavior. If any method that can mutate the contents of this object is called while + * iteration is in progress, either inside the loop or in another thread, then behavior is + * undefined. + * The remove() method is not implemented and will throw UnsupportedOperationException. + * @hide + */ + @SystemApi + @NonNull public Iterator iterator() { + return new Iterator() { + int mIndex = 0; + + @Override + public boolean hasNext() { + return mIndex < size; + } + + @Override + public Entry next() { + return getValues(mIndex++, null); + } + }; + } + /** * Return specific stats entry. * @hide