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

com.simplaex.bedrock.ArrayMapBuilder Maven / Gradle / Ivy

There is a newer version: 2020.02.27
Show newest version
package com.simplaex.bedrock;

import javax.annotation.Nonnull;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public final class ArrayMapBuilder, V>
  extends AbstractBuilder, ArrayMap, ArrayMapBuilder> {

  private final TreeMap underlyingMap = new TreeMap<>();

  @Nonnull
  public ArrayMapBuilder add(final K key, final V value) {
    underlyingMap.put(key, value);
    return this;
  }

  public ArrayMapBuilder addKeyValue(final K key, final V value) {
    return add(key, value);
  }

  public ArrayMapBuilder addPair(final Pair elem) {
    return add(elem);
  }

  @Override
  public ArrayMapBuilder add(final Pair elem) {
    return add(elem.fst(), elem.snd());
  }

  @Nonnull
  @Override
  public ArrayMap result() {
    if (underlyingMap.isEmpty()) {
      return ArrayMap.empty();
    }
    return ArrayMap.ofMap(underlyingMap);
  }

  public ArrayMapBuilder clear() {
    underlyingMap.clear();
    return this;
  }

  public boolean isEmpty() {
    return underlyingMap.isEmpty();
  }

  public void forEach(final BiConsumer consumer) {
    forEach(pair -> consumer.accept(pair.fst(), pair.snd()));
  }

  @Override
  public Iterator> iterator() {
    return new Iterator>() {

      private final Iterator> underlyingIterator = underlyingMap.entrySet().iterator();

      @Override
      public boolean hasNext() {
        return underlyingIterator.hasNext();
      }

      @Override
      public Pair next() {
        final Map.Entry entry = underlyingIterator.next();
        return Pair.of(entry.getKey(), entry.getValue());
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy