Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5f49564d authored by Kweku Adams's avatar Kweku Adams Committed by Android (Google) Code Review
Browse files

Merge "Improve forEach."

parents f0b055e6 14bbb8a7
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.function.Consumer;
import java.util.function.Predicate;

@RunWith(AndroidJUnit4.class)
@@ -38,6 +39,38 @@ public class ArraySetPerfTest {
    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    @Test
    public void testForEach_Small() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        Consumer<Integer> consumer = (i) -> {
        };
        while (state.keepRunning()) {
            for (int i = 0; i < NUM_ITERATIONS; ++i) {
                ArraySet<Integer> set = new ArraySet<>();
                for (int j = 0; j < SET_SIZE_SMALL; j++) {
                    set.add(j);
                }
                set.forEach(consumer);
            }
        }
    }

    @Test
    public void testForEach_Large() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        Consumer<Integer> consumer = (i) -> {
        };
        while (state.keepRunning()) {
            for (int i = 0; i < NUM_ITERATIONS; ++i) {
                ArraySet<Integer> set = new ArraySet<>();
                for (int j = 0; j < SET_SIZE_LARGE; j++) {
                    set.add(j);
                }
                set.forEach(consumer);
            }
        }
    }

    @Test
    public void testValueAt_InBounds() {
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
+1 −0
Original line number Diff line number Diff line
@@ -45993,6 +45993,7 @@ package android.util {
    method public boolean contains(Object);
    method public boolean containsAll(java.util.Collection<?>);
    method public void ensureCapacity(int);
    method public void forEach(java.util.function.Consumer<? super E>);
    method public int indexOf(Object);
    method public boolean isEmpty();
    method public java.util.Iterator<E> iterator();
+18 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
@@ -747,6 +748,23 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
        return mSize;
    }

    /**
     * Performs the given action for all elements in the stored order. This implementation overrides
     * the default implementation to avoid using the {@link #iterator()}.
     *
     * @param action The action to be performed for each element
     */
    @Override
    public void forEach(Consumer<? super E> action) {
        if (action == null) {
            throw new NullPointerException("action must not be null");
        }

        for (int i = 0; i < mSize; ++i) {
            action.accept(valueAt(i));
        }
    }

    @Override
    public Object[] toArray() {
        Object[] result = new Object[mSize];