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

io.ebean.common.ModifyEntrySet Maven / Gradle / Ivy

There is a newer version: 15.8.1
Show newest version
package io.ebean.common;

import java.util.*;

/**
 * Handles the Entry Set for BeanMap.
 */
class ModifyEntrySet implements Set> {

  private final BeanMap owner;
  private final Set> entrySet;

  ModifyEntrySet(BeanMap owner, Set> entrySet) {
    this.owner = owner;
    this.entrySet = entrySet;
  }

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

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

  @Override
  public boolean contains(Object o) {
    return entrySet.contains(o);
  }

  @Override
  public Object[] toArray() {
    return entrySet.toArray();
  }

  @Override
  public  T[] toArray(T[] a) {
    return entrySet.toArray(a);
  }

  @Override
  public boolean containsAll(Collection entries) {
    return entrySet.containsAll(entries);
  }

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

  @Override
  public boolean add(Map.Entry entry) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean addAll(Collection> c) {
    throw new UnsupportedOperationException();
  }

  @SuppressWarnings("rawtypes")
  @Override
  public boolean remove(Object o) {
    if (o instanceof Map.Entry) {
      Map.Entry entry = (Map.Entry) o;
      final E val = owner.get(entry.getKey());
      if (Objects.equals(val, entry.getValue())) {
        owner.remove(entry.getKey());
        return true;
      }
    }
    return false;
  }

  @Override
  public boolean retainAll(Collection entries) {
    boolean modified = false;
    final Iterator> it = iterator();
    while (it.hasNext()) {
      if (!entries.contains(it.next())) {
        it.remove();
        modified = true;
      }
    }
    return modified;
  }

  @Override
  public boolean removeAll(Collection entries) {
    boolean modified = false;
    for (Object entry : entries) {
      modified |= remove(entry);
    }
    return modified;
  }

  @Override
  public Iterator> iterator() {
    return new EntrySetIterator(new ArrayList<>(entrySet).iterator());
  }

  class EntrySetIterator implements Iterator> {

    private final Iterator> iterator;
    private Map.Entry entry;

    EntrySetIterator(Iterator> iterator) {
      this.iterator = iterator;
    }

    @Override
    public boolean hasNext() {
      return iterator.hasNext();
    }

    @Override
    public Map.Entry next() {
      entry = iterator.next();
      return entry;
    }

    @Override
    public void remove() {
      owner.remove(entry.getKey());
      iterator.remove();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy