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

com.cedarsoft.utils.CachingChildDetector Maven / Gradle / Ivy

The newest version!
package com.cedarsoft.utils;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.WeakHashMap;

/**
 * Special implementation of a child detector that always returns the same list for a given parent.
 */
public abstract class CachingChildDetector extends AbstractChildDetector {
  @NotNull
  private final Cache> childrenCache = new HashedCache>( new WeakHashMap>(), new Cache.Factory>() {
    @NotNull
    public List create( @NotNull P key ) {
      return createChildren( key );
    }
  } );

  @NotNull
  protected abstract List createChildren( @NotNull P parent );

  @NotNull
  public final List findChildren( @NotNull P parent ) {
    return childrenCache.get( parent );
  }

  public void handleModified( @NotNull P parent ) {
    invalidateCache( parent );
  }

  public void invalidateCache( @NotNull P parent ) {
    childrenCache.remove( parent );
    notifyChildrenChangedFor( parent );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy