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

org.libj.util.ObservableNavigableSet Maven / Gradle / Ivy

Go to download

Supplementary utilities for classes that belong to java.util, or are considered essential as to justify existence in java.util.

The newest version!
/* Copyright (c) 2023 LibJ
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * You should have received a copy of The MIT License (MIT) along with this
 * program. If not, see .
 */

package org.libj.util;

import java.util.Iterator;
import java.util.NavigableSet;
import java.util.Set;

/**
 * A {@link DelegateSet} that provides callback methods to observe the retrieval, addition, and removal of elements, either due to
 * direct method invocation on the set instance itself, or via {@link #iterator()}, {@link #stream()}, and any other entrypoint that
 * facilitates modification of the elements in this set.
 *
 * @param  The type of elements in this set.
 * @see #afterGet(Object,RuntimeException)
 * @see #beforeAdd(Object,Object)
 * @see #afterAdd(Object,RuntimeException)
 * @see #beforeRemove(Object)
 * @see #afterRemove(Object,RuntimeException)
 */
public abstract class ObservableNavigableSet extends ObservableSortedSet implements NavigableSet {
  /**
   * Creates a new {@link ObservableNavigableSet} with the specified target {@code set}.
   *
   * @param set The target {@link Set}.
   * @throws NullPointerException If {@code set} is null.
   */
  public ObservableNavigableSet(final NavigableSet set) {
    super(set);
  }

  @Override
  public E lower(final E e) {
    return ((NavigableSet)target).lower(e);
  }

  @Override
  public E floor(final E e) {
    return ((NavigableSet)target).floor(e);
  }

  @Override
  public E ceiling(final E e) {
    return ((NavigableSet)target).ceiling(e);
  }

  @Override
  public E higher(final E e) {
    return ((NavigableSet)target).higher(e);
  }

  @Override
  public E pollFirst() {
    return ((NavigableSet)target).pollFirst();
  }

  @Override
  public E pollLast() {
    return ((NavigableSet)target).pollLast();
  }

  protected class NavigableSubSet extends ObservableNavigableSet {
    protected NavigableSubSet(final NavigableSet set) {
      super(set);
    }

    @Override
    protected E afterGet(final E value, final RuntimeException e) {
      return ObservableNavigableSet.this.afterGet(value, e);
    }

    @Override
    protected Object beforeAdd(final E element, final Object preventDefault) {
      return ObservableNavigableSet.this.beforeAdd(element, preventDefault);
    }

    @Override
    protected void afterAdd(final E element, final RuntimeException e) {
      ObservableNavigableSet.this.afterAdd(element, e);
    }

    @Override
    protected boolean beforeRemove(final Object element) {
      return ObservableNavigableSet.this.beforeRemove(element);
    }

    @Override
    protected void afterRemove(final Object element, final RuntimeException e) {
      ObservableNavigableSet.this.afterRemove(element, e);
    }
  }

  @Override
  public NavigableSet descendingSet() {
    return new NavigableSubSet(((NavigableSet)target).descendingSet());
  }

  @Override
  public Iterator descendingIterator() {
    return new ObservableIterator(((NavigableSet)target).descendingIterator());
  }

  @Override
  public NavigableSet subSet(final E fromElement, final boolean fromInclusive, final E toElement, final boolean toInclusive) {
    return new NavigableSubSet(((NavigableSet)target).subSet(fromElement, fromInclusive, toElement, toInclusive));
  }

  @Override
  public NavigableSet headSet(final E toElement, final boolean inclusive) {
    return new NavigableSubSet(((NavigableSet)target).headSet(toElement, inclusive));
  }

  @Override
  public NavigableSet tailSet(final E fromElement, final boolean inclusive) {
    return new NavigableSubSet(((NavigableSet)target).tailSet(fromElement, inclusive));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy