MoneyWiseNameSpaceIterator.java

/*
 * MoneyWise: Finance Application
 * Copyright 2012-2026. Tony Washer
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.tonywasher.joceanus.moneywise.ui.base;

import io.github.tonywasher.joceanus.metis.list.MetisListKey;
import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataList;
import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;

/**
 * NameSpace iterator.
 */
public class MoneyWiseNameSpaceIterator
        implements Iterator<PrometheusDataItem> {
    /**
     * The editSet.
     */
    private final PrometheusEditSet theEditSet;

    /**
     * The keySet.
     */
    private final Deque<MetisListKey> theKeys;

    /**
     * The current iterator.
     */
    private Iterator<PrometheusDataItem> theIterator;

    /**
     * Constructor.
     *
     * @param pEditSet the editSet
     * @param pKeys    the listKeys.
     */
    public MoneyWiseNameSpaceIterator(final PrometheusEditSet pEditSet,
                                      final MetisListKey... pKeys) {
        /* Store the editSet */
        theEditSet = pEditSet;

        /* Create the list */
        theKeys = new ArrayDeque<>(Arrays.asList(pKeys));
    }


    @Override
    public boolean hasNext() {
        /* If we have an iterator and a next item */
        if (theIterator != null) {
            if (theIterator.hasNext()) {
                return true;
            }
            theIterator = null;
        }

        /* Loop to get an iterator that has an item */
        while (true) {
            /* No more items if the keys are empty */
            if (theKeys.isEmpty()) {
                return false;
            }

            /* Pop the next item */
            final MetisListKey myKey = theKeys.pop();

            /* Create iterator for next key */
            @SuppressWarnings("unchecked") final PrometheusDataList<PrometheusDataItem> myList = theEditSet.getDataList(myKey, PrometheusDataList.class);
            theIterator = myList.iterator();
            if (theIterator.hasNext()) {
                return true;
            }
            theIterator = null;
        }
    }

    @Override
    public PrometheusDataItem next() {
        return theIterator.next();
    }
}