All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.firebase.database.collection.ImmutableSortedMapIterator Maven / Gradle / Ivy

Go to download

This is the official Firebase Admin Java SDK. Build extraordinary native JVM apps in minutes with Firebase. The Firebase platform can power your app’s backend, user authentication, static hosting, and more.

There is a newer version: 9.2.0
Show newest version
/*
 * Copyright 2017 Google Inc.
 *
 * 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 com.google.firebase.database.collection;

import java.util.AbstractMap;
import java.util.Comparator;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Stack;

public class ImmutableSortedMapIterator implements Iterator> {

  private final Stack> nodeStack;

  private final boolean isReverse;

  ImmutableSortedMapIterator(
      LLRBNode root, K startKey, Comparator comparator, boolean isReverse) {
    this.nodeStack = new Stack<>();
    this.isReverse = isReverse;

    LLRBNode node = root;
    while (!node.isEmpty()) {
      int cmp;
      if (startKey != null) {
        cmp =
            isReverse
                ? comparator.compare(startKey, node.getKey())
                : comparator.compare(node.getKey(), startKey);
      } else {
        cmp = 1;
      }
      if (cmp < 0) {
        // This node is less than our start key. ignore it
        if (isReverse) {
          node = node.getLeft();
        } else {
          node = node.getRight();
        }
      } else if (cmp == 0) {
        // This node is exactly equal to our start key. Push it on the stack, but stop
        // iterating;
        this.nodeStack.push((LLRBValueNode) node);
        break;
      } else {
        this.nodeStack.push((LLRBValueNode) node);
        if (isReverse) {
          node = node.getRight();
        } else {
          node = node.getLeft();
        }
      }
    }
  }

  @Override
  public boolean hasNext() {
    return nodeStack.size() > 0;
  }

  @Override
  public Map.Entry next() {
    try {
      final LLRBValueNode node = nodeStack.pop();
      Map.Entry entry = new AbstractMap.SimpleEntry<>(node.getKey(), node.getValue());
      if (this.isReverse) {
        LLRBNode next = node.getLeft();
        while (!next.isEmpty()) {
          this.nodeStack.push((LLRBValueNode) next);
          next = next.getRight();
        }
      } else {
        LLRBNode next = node.getRight();
        while (!next.isEmpty()) {
          this.nodeStack.push((LLRBValueNode) next);
          next = next.getLeft();
        }
      }
      return entry;
    } catch (EmptyStackException e) {
      // No more children
      throw new NoSuchElementException();
    }
  }

  @Override
  public void remove() {
    throw new UnsupportedOperationException("remove called on immutable collection");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy