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

cn.taketoday.core.MultiValueMapAdapter Maven / Gradle / Ivy

/*
 * Original Author -> Harry Yang ([email protected]) https://taketoday.cn
 * Copyright © TODAY & 2017 - 2022 All Rights Reserved.
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see [http://www.gnu.org/licenses/]
 */

package cn.taketoday.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.taketoday.lang.Assert;
import cn.taketoday.lang.Nullable;

/**
 * Adapts a given {@link Map} to the {@link MultiValueMap} contract.
 *
 * @param  the key type
 * @param  the value element type
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @author Harry Yang
 * @see LinkedMultiValueMap
 * @since 4.0 2022/4/25 14:39
 */
@SuppressWarnings("serial")
public class MultiValueMapAdapter implements MultiValueMap, Serializable {

  private final Map> targetMap;

  /**
   * Wrap the given target {@link Map} as a {@link MultiValueMap} adapter.
   *
   * @param targetMap the plain target {@code Map}
   */
  public MultiValueMapAdapter(Map> targetMap) {
    Assert.notNull(targetMap, "'targetMap' must not be null");
    this.targetMap = targetMap;
  }

  // MultiValueMap implementation

  @Override
  @Nullable
  public V getFirst(K key) {
    List values = this.targetMap.get(key);
    return (values != null && !values.isEmpty() ? values.get(0) : null);
  }

  @Override
  public void add(K key, @Nullable V value) {
    List values = this.targetMap.computeIfAbsent(key, k -> new ArrayList<>(1));
    values.add(value);
  }

  @Override
  public void addAll(K key, @Nullable Collection values) {
    if (values != null) {
      List currentValues = this.targetMap.computeIfAbsent(key, k -> new ArrayList<>(1));
      currentValues.addAll(values);
    }
  }

  @Override
  public void set(K key, @Nullable V value) {
    List values = new ArrayList<>(1);
    values.add(value);
    this.targetMap.put(key, values);
  }

  // Map implementation

  @Override
  public int size() {
    return this.targetMap.size();
  }

  @Override
  public boolean isEmpty() {
    return this.targetMap.isEmpty();
  }

  @Override
  public boolean containsKey(Object key) {
    return this.targetMap.containsKey(key);
  }

  @Override
  public boolean containsValue(Object value) {
    return this.targetMap.containsValue(value);
  }

  @Override
  @Nullable
  public List get(Object key) {
    return this.targetMap.get(key);
  }

  @Override
  @Nullable
  public List put(K key, List value) {
    return this.targetMap.put(key, value);
  }

  @Override
  @Nullable
  public List remove(Object key) {
    return this.targetMap.remove(key);
  }

  @Override
  public void putAll(Map> map) {
    this.targetMap.putAll(map);
  }

  @Override
  public void clear() {
    this.targetMap.clear();
  }

  @Override
  public Set keySet() {
    return this.targetMap.keySet();
  }

  @Override
  public Collection> values() {
    return this.targetMap.values();
  }

  @Override
  public Set>> entrySet() {
    return this.targetMap.entrySet();
  }

  @Override
  public boolean equals(@Nullable Object other) {
    return (this == other || this.targetMap.equals(other));
  }

  @Override
  public int hashCode() {
    return this.targetMap.hashCode();
  }

  @Override
  public String toString() {
    return this.targetMap.toString();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy