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

org.apache.baremaps.data.collection.DataMap Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 org.apache.baremaps.data.collection;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.function.BiConsumer;

/**
 * A {@code DataMap} maps keys to values. It is similar to a {@link java.util.Map Map}, but
 * can hold up to {@link Long#MAX_VALUE} entries.
 *
 * @param  The type of the keys.
 * @param  The type of the values.
 */
public interface DataMap {

  /**
   * Returns the number of values stored in the data map.
   *
   * @return the number of values
   */
  long size();

  /**
   * Returns the value associated with the specified key or null if the key is not present.
   *
   * @param key the key
   * @return the value
   */
  V get(Object key);

  /**
   * Returns the values associated with the specified keys or null if the key is not present.
   *
   * @param keys the keys
   * @return the values
   */
  default Iterable getAll(Iterable keys) {
    List values = new ArrayList<>();
    keys.forEach(key -> values.add(get(key)));
    return values;
  }

  /**
   * Associates the specified value with the specified key in the data map.
   *
   * @param key the key
   * @param value the value
   * @return the previous value associated with the key, or null if there was no mapping for the key
   */
  V put(K key, V value);

  /**
   * Associates the specified values with the specified keys in the data map.
   *
   * @param entries the entries
   */
  default void putAll(Iterable> entries) {
    entries.forEach(entry -> put(entry.getKey(), entry.getValue()));
  }

  /**
   * Returns true if the data map contains a mapping for the specified key.
   *
   * @param key the key
   * @return true if the data map contains a mapping for the key
   */
  boolean containsKey(Object key);

  /**
   * Returns true if the data map contains a mapping for the specified value.
   *
   * @param value the value
   * @return true if the data map contains a mapping for the value
   */
  boolean containsValue(V value);

  /**
   * Clears the data map.
   */
  void clear();

  /**
   * Returns true if the data map contains no elements.
   * 
   * @return true if the data map contains no elements
   */
  default boolean isEmpty() {
    return size() == 0;
  }

  /**
   * Returns an iterator over the keys of the data map.
   *
   * @return an iterator
   */
  Iterator keyIterator();

  /**
   * Returns an iterable over the keys of the data map.
   *
   * @return an iterable
   */
  default Iterable keys() {
    return this::keyIterator;
  }

  /**
   * Returns an iterator over the values of the data map.
   *
   * @return an iterator
   */
  Iterator valueIterator();

  /**
   * Returns an iterable over the values of the data map.
   *
   * @return an iterable
   */
  default Iterable values() {
    return this::valueIterator;
  }

  /**
   * Returns an iterator over the entries of the data map.
   *
   * @return an iterator
   */
  Iterator> entryIterator();

  /**
   * Returns an iterable over the entries of the data map.
   *
   * @return an iterable
   */
  default Iterable> entries() {
    return this::entryIterator;
  }

  /**
   * Performs the given action for each entry in the data map.
   *
   * @param action the action to be performed
   */
  default void forEach(BiConsumer action) {
    for (Entry entry : entries()) {
      action.accept(entry.getKey(), entry.getValue());
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy