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

ratpack.util.internal.ImmutableDelegatingMultiValueMap Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2013 the original author or authors.
 *
 * 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 ratpack.util.internal;

import com.google.common.collect.*;
import ratpack.util.MultiValueMap;
import ratpack.util.Types;

import java.util.*;
import java.util.stream.Collectors;

public class ImmutableDelegatingMultiValueMap implements MultiValueMap {

  private static final ImmutableDelegatingMultiValueMap EMPTY = new ImmutableDelegatingMultiValueMap<>(ImmutableMap.of());

  @SuppressWarnings("unchecked")
  public static  MultiValueMap empty() {
    return Types.cast(EMPTY);
  }

  private final Map> delegate;

  public ImmutableDelegatingMultiValueMap(Map> map) {
    this.delegate = map;
  }

  @SuppressWarnings("unchecked")
  public List getAll(K key) {
    return delegate.get(key);
  }

  @SuppressWarnings("unchecked")
  public Map> getAll() {
    return delegate;
  }

  public int size() {
    return delegate.size();
  }

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

  public boolean containsKey(Object key) {
    return delegate.containsKey(key);
  }

  public boolean containsValue(Object value) {
    for (List values : delegate.values()) {
      //noinspection SuspiciousMethodCalls
      if (values.contains(value)) {
        return true;
      }
    }
    return false;
  }

  public V get(Object key) {
    List result = delegate.get(key);
    return result != null && result.size() > 0 ? result.get(0) : null;
  }

  public V put(K key, V value) {
    throw new UnsupportedOperationException("This implementation is immutable");
  }

  public V remove(Object key) {
    throw new UnsupportedOperationException("This implementation is immutable");
  }

  @SuppressWarnings("NullableProblems")
  public void putAll(Map m) {
    throw new UnsupportedOperationException("This implementation is immutable");
  }

  public void clear() {
    throw new UnsupportedOperationException("This implementation is immutable");
  }

  @Override
  public ListMultimap asMultimap() {
    ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder();
    for (Entry> entry : delegate.entrySet()) {
      builder.putAll(entry.getKey(), entry.getValue());
    }
    return builder.build();
  }

  @SuppressWarnings({"unchecked", "NullableProblems"})
  public Set keySet() {
    return delegate.keySet();
  }

  @SuppressWarnings("NullableProblems")
  public Collection values() {
    return Lists.newArrayList(Iterables.concat(delegate.values()));
  }

  @SuppressWarnings("NullableProblems")
  public Set> entrySet() {
    return delegate.entrySet().stream()
      .filter(entry -> entry.getValue().size() > 0)
      .map(entry -> new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue().get(0)))
      .collect(Collectors.toSet());
  }

  @Override
  public String toString() {
    if (delegate.isEmpty()) {
      return "[:]";
    }
    StringBuilder buffer = new StringBuilder("[");
    boolean first = true;
    for (Entry> entry : delegate.entrySet()) {
      if (first) {
        first = false;
      } else {
        buffer.append(", ");
      }
      buffer.append(entry.getKey().toString());
      buffer.append(":[");
      boolean firstValue = true;
      for (V value : entry.getValue()) {
        if (firstValue) {
          firstValue = false;
        } else {
          buffer.append(", ");
        }
        buffer.append(value.toString());
      }
      buffer.append("]");
    }
    buffer.append("]");
    return buffer.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy