• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wm.utils;
18 
19 import androidx.annotation.NonNull;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.function.Consumer;
24 
25 /**
26  * Contains all the component created in the test.
27  */
28 public class TestComponentStack<T> {
29 
30     @NonNull
31     private final List<T> mItems = new ArrayList<>();
32 
33     /**
34      * Adds an item to the stack.
35      *
36      * @param item The item to add.
37      */
push(@onNull T item)38     public void push(@NonNull T item) {
39         mItems.add(item);
40     }
41 
42     /**
43      * Consumes the top element of the stack.
44      *
45      * @param consumer Consumer for the optional top element.
46      * @throws IndexOutOfBoundsException In case that stack is empty.
47      */
applyToTop(@onNull Consumer<T> consumer)48     public void applyToTop(@NonNull Consumer<T> consumer) {
49         consumer.accept(top());
50     }
51 
52     /**
53      * Returns the item at fromTop position from the top one if present or it throws an
54      * exception if not present.
55      *
56      * @param fromTop The position from the top of the item to return.
57      * @return The returned item.
58      * @throws IndexOutOfBoundsException In case that position doesn't exist.
59      */
60     @NonNull
getFromTop(int fromTop)61     public T getFromTop(int fromTop) {
62         return mItems.get(mItems.size() - fromTop - 1);
63     }
64 
65     /**
66      * @return The item at the base of the stack if present.
67      * @throws IndexOutOfBoundsException In case that stack is empty.
68      */
69     @NonNull
base()70     public T base() {
71         return mItems.get(0);
72     }
73 
74     /**
75      * @return The item at the top of the stack if present.
76      * @throws IndexOutOfBoundsException In case that stack is empty.
77      */
78     @NonNull
top()79     public T top() {
80         return mItems.get(mItems.size() - 1);
81     }
82 
83     /**
84      * @return {@code true} if the stack is empty.
85      */
isEmpty()86     public boolean isEmpty() {
87         return mItems.isEmpty();
88     }
89 
90     /**
91      * Allows access to the item at position beforeLast from the top.
92      *
93      * @param fromTop  The position from the top of the item to return.
94      * @param consumer Consumer for the optional returned element.
95      */
applyTo(int fromTop, @NonNull Consumer<T> consumer)96     public void applyTo(int fromTop, @NonNull Consumer<T> consumer) {
97         consumer.accept(getFromTop(fromTop));
98     }
99 
100     /**
101      * Invoked the consumer iterating over all the elements in the stack.
102      *
103      * @param consumer Consumer for the elements.
104      */
applyToAll(@onNull Consumer<T> consumer)105     public void applyToAll(@NonNull Consumer<T> consumer) {
106         for (int i = mItems.size() - 1; i >= 0; i--) {
107             consumer.accept(mItems.get(i));
108         }
109     }
110 }
111