• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.timezone.location.storage.tzs2range.write;
18 
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 
22 /** An iterator that can have elements pushed back onto it. {@link #remove()} is not supported. */
23 public final class PushBackIterator<E> implements Iterator<E> {
24 
25     private final ArrayList<E> mPushBackStack = new ArrayList<>();
26 
27     private final Iterator<E> mIterator;
28 
PushBackIterator(Iterator<E> iterator)29     public PushBackIterator(Iterator<E> iterator) {
30         mIterator = iterator;
31     }
32 
33     @Override
hasNext()34     public boolean hasNext() {
35         return !mPushBackStack.isEmpty() || mIterator.hasNext();
36     }
37 
38     @Override
next()39     public E next() {
40         if (!mPushBackStack.isEmpty()) {
41             return mPushBackStack.remove(mPushBackStack.size() - 1);
42         }
43         return mIterator.next();
44     }
45 
46     /**
47      * Pushes the element to the front of the iterator again.
48      */
pushBack(E element)49     public void pushBack(E element) {
50         mPushBackStack.add(element);
51     }
52 }
53