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

eu.cedarsoft.utils.ParentCache Maven / Gradle / Ivy

The newest version!
package eu.cedarsoft.utils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * Stores the parent of children (and their indicies) in weak maps.
 */
public class ParentCache {
  /**
   * The child is the key, the parent the value
   */
  @NotNull
  private final Map> parents = new WeakHashMap>();
  /**
   * Contains the inidices of the child
   */
  @NotNull
  private final Map indicies = new WeakHashMap();


  /**
   * Returns the parent of the given child
   *
   * @param child the child
   * @return the parent
   */
  @Nullable
  public  P findParent( @NotNull C child ) {
    WeakReference reference = parents.get( child );
    if ( reference == null ) {
      return null;
    } else {
      return ( P ) reference.get();
    }
  }

  private void removeParent( @NotNull Object child ) {
    parents.remove( child );
  }

  private void removeIndex( @NotNull Object child ) {
    indicies.remove( child );
  }

  public void remove( @NotNull Object child ) {
    removeParent( child );
    removeIndex( child );
  }

  public void removeAll( @NotNull List children ) {
    for ( Object child : children ) {
      remove( child );
    }
  }

  private  void storeParent( @NotNull C child, @NotNull P parent ) {
    Object oldParent = findParent( child );
    if ( oldParent != null ) {
      if ( parent == oldParent ) {
        return;
      } else {
        throw new IllegalStateException( "Child <" + child + "> still has a parent: <" + oldParent + ">. New parent is: <" + parent + '>' );
      }
    }
    parents.put( child, new WeakReference( parent ) );
  }

  /**
   * Stores a parent
   *
   * @param child  the child
   * @param parent the parent
   */
  public  void store( @NotNull C child, @NotNull P parent, int index ) {
    storeParent( child, parent );
    storeIndex( child, index );
  }


  public  void storeIndex( @NotNull C child, int index ) {
    indicies.put( child, index );
  }

  @Nullable
  public  Integer findIndex( @NotNull C child ) {
    return indicies.get( child );
  }

  /**
   * Returns a list containing all predecessors for the given child.
   * The first entry is the root - the last the parent of the child
   *
   * @param child the child
   * @return all predecessors for the given child
   */
  @NotNull
  public List getPredecessors( @NotNull Object child ) {
    List list = new ArrayList();

    Object parent = findParent( child );
    while ( parent != null ) {
      list.add( parent );
      parent = findParent( parent );
    }

    Collections.reverse( list );
    return list;
  }

  /**
   * Returns the path (child an all its predecessors).
   * The first entry is the root - the last the child itself.
   *
   * @param child the child
   * @return a list containing the child and all its predecessors
   */
  @NotNull
  public List getPath( @NotNull Object child ) {
    List path = new ArrayList( getPredecessors( child ) );
    path.add( child );
    return path;
  }

  /**
   * Stores several children
   *
   * @param parent   the parent
   * @param children the children
   */
  public void store( @NotNull Object parent, @NotNull List children ) {
    for ( int index = 0; index < children.size(); index++ ) {
      Object child = children.get( index );
      store( child, parent, index );
    }
  }
}