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

com.arangodb.shaded.vertx.core.shareddata.impl.LocalMapImpl Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.shareddata.impl;

import com.arangodb.shaded.vertx.core.shareddata.LocalMap;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

import static com.arangodb.shaded.vertx.core.shareddata.impl.Checker.checkType;
import static com.arangodb.shaded.vertx.core.shareddata.impl.Checker.copyIfRequired;

/**
 * @author Tim Fox
 */
class LocalMapImpl implements LocalMap {

  private final ConcurrentMap> maps;
  private final String name;
  private final ConcurrentMap map = new ConcurrentHashMap<>();

  LocalMapImpl(String name, ConcurrentMap> maps) {
    this.name = name;
    this.maps = maps;
  }

  @Override
  public V get(Object key) {
    return copyIfRequired(map.get(key));
  }

  @Override
  public V put(K key, V value) {
    checkType(key);
    checkType(value);
    return map.put(key, value);
  }

  @Override
  public V remove(Object key) {
    return copyIfRequired(map.remove(key));
  }

  @Override
  public void clear() {
    map.clear();
  }

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

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

  @Override
  public V putIfAbsent(K key, V value) {
    checkType(key);
    checkType(value);
    return copyIfRequired(map.putIfAbsent(key, value));
  }

  @Override
  public boolean remove(Object key, Object value) {
    return map.remove(key, value);
  }

  @Override
  public boolean replace(K key, V oldValue, V newValue) {
    return map.replace(key, oldValue, newValue);
  }

  @Override
  public boolean removeIfPresent(K key, V value) {
    return map.remove(key, value);
  }

  @Override
  public boolean replaceIfPresent(K key, V oldValue, V newValue) {
    checkType(key);
    checkType(oldValue);
    checkType(newValue);
    return map.replace(key, oldValue, newValue);
  }

  @Override
  public V replace(K key, V value) {
    checkType(key);
    checkType(value);
    return copyIfRequired(map.replace(key, value));
  }

  @Override
  public void replaceAll(BiFunction function) {
    map.replaceAll((k, v) -> {
      checkType(k);
      checkType(v);
      V output = function.apply(k, v);
      if (output != null) {
        checkType(output);
      }
      return output;
    });
  }

  @Override
  public void close() {
    maps.remove(name);
  }

  @Override
  public Set keySet() {
    Set keys = new HashSet<>(map.size());
    for (K k : map.keySet()) {
      keys.add(copyIfRequired(k));
    }
    return keys;
  }

  @Override
  public Collection values() {
    List values = new ArrayList<>(map.size());
    for (V v : map.values()) {
      values.add(copyIfRequired(v));
    }
    return values;
  }

  /**
   * Composes the given bi-function ({@code f(a,b)}) with a function checking the type of the output:
   * {@code checkType(f(a,b))}. So the output of the given function is checked to verify that it uses a valid type.
   *
   * @param function the function
   * @return the composition
   */
  private BiFunction typeChecked(BiFunction
                                                                        function) {
    return (k, v) -> {
      checkType(k);
      V output = function.apply(k, v);
      if (output != null) {
        checkType(output);
      }
      return output;
    };
  }

  /**
   * Composes the given function ({@code f(a)}) with a function checking the type of the output. So the output of the
   * given function is checked to verify that is uses a valid type.
   *
   * @param function the function
   * @return the composition
   */
  private Function typeChecked(Function
                                                           function) {
    return k -> {
      checkType(k);
      V output = function.apply(k);
      if (output != null) {
        checkType(output);
      }
      return output;
    };
  }

  @Override
  public V compute(K key, BiFunction remappingFunction) {
    return map.compute(key, typeChecked(remappingFunction));
  }

  @Override
  public V computeIfAbsent(K key, Function mappingFunction) {
    return map.computeIfAbsent(key, typeChecked(mappingFunction));
  }

  @Override
  public V computeIfPresent(K key, BiFunction remappingFunction) {
    return map.computeIfPresent(key, typeChecked(remappingFunction));
  }

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

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

  @Override
  public Set> entrySet() {
    Set> entries = new HashSet<>(map.size());
    for (Map.Entry entry : map.entrySet()) {
      entries.add(new Map.Entry() {

        @Override
        public K getKey() {
          return copyIfRequired(entry.getKey());
        }

        @Override
        public V getValue() {
          return copyIfRequired(entry.getValue());
        }

        @Override
        public V setValue(V value) {
          throw new UnsupportedOperationException();
        }
      });
    }
    return entries;
  }

  @Override
  public void forEach(BiConsumer action) {
    // Cannot delegate, it needs to copy the objects to avoid modifications
    for (Map.Entry entry : entrySet()) {
      action.accept(entry.getKey(), entry.getValue());
    }
  }

  @Override
  public V getOrDefault(Object key, V defaultValue) {
    return copyIfRequired(map.getOrDefault(key, defaultValue));
  }

  @Override
  public V merge(K key, V value, BiFunction remappingFunction) {
    checkType(key);
    checkType(value);
    return map.merge(key, value, (k, v) -> {
      // No need to check the key, already check above.
      V output = remappingFunction.apply(k, v);
      if (output != null) {
        checkType(output);
      }
      return output;
    });
  }

  @Override
  public void putAll(Map m) {
    // Iterate over the set to entry and call `put` on each entry to validate the types
    for (Entry entry : m.entrySet()) {
      put(entry.getKey(), entry.getValue());
    }
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy