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

org.elasticsearch.util.collect.ConstrainedMap Maven / Gradle / Ivy

There is a newer version: 8.13.4
Show newest version
/*
 * Copyright (C) 2007 Google Inc.
 *
 * 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 org.elasticsearch.util.collect;

import org.elasticsearch.util.annotations.GwtCompatible;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static org.elasticsearch.util.base.Preconditions.*;

/**
 * Factory and utilities pertaining to the {@code MapConstraint} interface.
 *
 * @author Mike Bostock
 */
@GwtCompatible
class ConstrainedMap extends ForwardingMap {
  final Map delegate;
  final MapConstraint constraint;
  private volatile Set> entrySet;

  ConstrainedMap(
      Map delegate, MapConstraint constraint) {
    this.delegate = checkNotNull(delegate);
    this.constraint = checkNotNull(constraint);
  }

  @Override protected Map delegate() {
    return delegate;
  }
  @Override public Set> entrySet() {
    if (entrySet == null) {
      entrySet = constrainedEntrySet(delegate.entrySet(), constraint);
    }
    return entrySet;
  }
  @Override public V put(K key, V value) {
    constraint.checkKeyValue(key, value);
    return delegate.put(key, value);
  }
  @Override public void putAll(Map map) {
    for (Entry entry : map.entrySet()) {
      put(entry.getKey(), entry.getValue());
    }
  }

  private static  Entry constrainedEntry(
      final Entry entry,
      final MapConstraint constraint) {
    checkNotNull(entry);
    checkNotNull(constraint);
    return new ForwardingMapEntry() {
      @Override protected Entry delegate() {
        return entry;
      }
      @Override public V setValue(V value) {
        constraint.checkKeyValue(getKey(), value);
        return entry.setValue(value);
      }
    };
  }

  private static  Set> constrainedEntrySet(
      Set> entries,
      MapConstraint constraint) {
    return new ConstrainedEntrySet(entries, constraint);
  }

  private static class ConstrainedEntries
      extends ForwardingCollection> {
    final MapConstraint constraint;
    final Collection> entries;

    ConstrainedEntries(Collection> entries,
        MapConstraint constraint) {
      this.entries = entries;
      this.constraint = constraint;
    }
    @Override protected Collection> delegate() {
      return entries;
    }

    @Override public Iterator> iterator() {
      final Iterator> iterator = entries.iterator();
      return new ForwardingIterator>() {
        @Override public Entry next() {
          return constrainedEntry(iterator.next(), constraint);
        }
        @Override protected Iterator> delegate() {
          return iterator;
        }
      };
    }

    // See Collections.CheckedMap.CheckedEntrySet for details on attacks.

    @Override public Object[] toArray() {
      return ObjectArrays.toArrayImpl(this);
    }
    @Override public  T[] toArray(T[] array) {
      return ObjectArrays.toArrayImpl(this, array);
    }
    @Override public boolean contains(Object o) {
      return Maps.containsEntryImpl(delegate(), o);
    }
    @Override public boolean containsAll(Collection c) {
      return Collections2.containsAll(this, c);
    }
    @Override public boolean remove(Object o) {
      return Maps.removeEntryImpl(delegate(), o);
    }
    @Override public boolean removeAll(Collection c) {
      return Iterators.removeAll(iterator(), c);
    }
    @Override public boolean retainAll(Collection c) {
      return Iterators.retainAll(iterator(), c);
    }
  }

  static class ConstrainedEntrySet
      extends ConstrainedEntries implements Set> {
    ConstrainedEntrySet(Set> entries,
        MapConstraint constraint) {
      super(entries, constraint);
    }

    // See Collections.CheckedMap.CheckedEntrySet for details on attacks.

    @Override public boolean equals(@Nullable Object object) {
      return Collections2.setEquals(this, object);
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy