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

io.ebeaninternal.json.ModifyAwareList Maven / Gradle / Ivy

There is a newer version: 15.8.1
Show newest version
package io.ebeaninternal.json;

import io.ebean.ModifyAwareType;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;

/**
 * Modify aware wrapper of a list.
 */
public final class ModifyAwareList implements List, ModifyAwareType, Serializable {

  private static final long serialVersionUID = 1;

  final List list;
  final ModifyAwareType owner;

  public ModifyAwareList(List list) {
    this.list = list;
    this.owner = new ModifyAwareFlag();
  }

  public ModifyAwareList(ModifyAwareType owner, List list) {
    this.list = list;
    this.owner = owner;
  }

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

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o instanceof ModifyAwareList) {
      ModifyAwareList that = (ModifyAwareList) o;
      return Objects.equals(list, that.list);
    }
    if (!(o instanceof List)) return false;
    List that = (List) o;
    return Objects.equals(list, that);
  }

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

  @Override
  public boolean isMarkedDirty() {
    return owner.isMarkedDirty();
  }

  @Override
  public void setMarkedDirty(boolean markedDirty) {
    owner.setMarkedDirty(markedDirty);
  }

  private void markAsDirty() {
    owner.setMarkedDirty(true);
  }

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

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

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

  @Override
  public Iterator iterator() {
    return new ModifyAwareIterator<>(owner, list.iterator());
  }

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

  @Override
  public  T[] toArray(T[] a) {
    //noinspection SuspiciousToArrayCall
    return list.toArray(a);
  }

  @Override
  public boolean add(E e) {
    markAsDirty();
    return list.add(e);
  }

  @Override
  public boolean remove(Object o) {
    markAsDirty();
    return list.remove(o);
  }

  @Override
  public boolean containsAll(Collection c) {
    return list.containsAll(c);
  }

  @Override
  public boolean addAll(Collection c) {
    markAsDirty();
    return list.addAll(c);
  }

  @Override
  public boolean addAll(int index, Collection c) {
    markAsDirty();
    return list.addAll(index, c);
  }

  @Override
  public boolean removeAll(Collection c) {
    markAsDirty();
    return list.removeAll(c);
  }

  @Override
  public boolean retainAll(Collection c) {
    markAsDirty();
    return list.retainAll(c);
  }

  @Override
  public void clear() {
    markAsDirty();
    list.clear();
  }

  @Override
  public E get(int index) {
    return list.get(index);
  }

  @Override
  public E set(int index, E element) {
    markAsDirty();
    return list.set(index, element);
  }

  @Override
  public void add(int index, E element) {
    markAsDirty();
    list.add(index, element);
  }

  @Override
  public E remove(int index) {
    markAsDirty();
    return list.remove(index);
  }

  @Override
  public int indexOf(Object o) {
    return list.indexOf(o);
  }

  @Override
  public int lastIndexOf(Object o) {
    return list.lastIndexOf(o);
  }

  @Override
  public ListIterator listIterator() {
    return new ModifyAwareListIterator<>(owner, list.listIterator());
  }

  @Override
  public ListIterator listIterator(int index) {
    return new ModifyAwareListIterator<>(owner, list.listIterator(index));
  }

  @Override
  public List subList(int fromIndex, int toIndex) {
    return new ModifyAwareList<>(owner, list.subList(fromIndex, toIndex));
  }

  /**
   * Create an return a modify aware Set.
   */
  public ModifyAwareSet asSet() {
    return new ModifyAwareSet<>(owner, new LinkedHashSet<>(list));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy