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

io.annot8.common.implementations.properties.MapMutableProperties Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
/* Annot8 (annot8.io) - Licensed under Apache-2.0. */
package io.annot8.common.implementations.properties;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import io.annot8.core.properties.MutableProperties;
import io.annot8.core.properties.Properties;

/**
 * Implementation of MutableProperties interface using an in-memory HashMap to store the properties.
 */
public class MapMutableProperties implements MutableProperties {

  private final Map properties = new HashMap<>();

  /** Create a new instance with no key-values */
  public MapMutableProperties() {
    // Do nothing
  }

  /** Create a new instance with key-values from an existing Properties object */
  public MapMutableProperties(Properties properties) {
    properties.getAll().forEach(this.properties::put);
  }

  /** Create a new instance with key-values from an existing Map */
  public MapMutableProperties(Map properties) {
    properties.forEach(this.properties::put);
  }

  @Override
  public Map getAll() {
    return properties;
  }

  @Override
  public void set(String key, Object value) {
    properties.put(key, value);
  }

  @Override
  public Optional remove(String key) {
    if (properties.containsKey(key)) {
      return Optional.of(properties.remove(key));
    } else {
      return Optional.empty();
    }
  }

  @Override
  public String toString() {
    return this.getClass().getName()
        + " ["
        + properties
            .entrySet()
            .stream()
            .map(e -> e.getKey() + "=" + e.getValue())
            .collect(Collectors.joining(", "))
        + "]";
  }

  @Override
  public int hashCode() {
    return Objects.hash(properties);
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Properties)) {
      return false;
    }

    Properties p = (Properties) o;
    return Objects.equals(properties, p.getAll());
  }
}