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

io.annot8.common.implementations.factories.NotifyingItemFactory Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
/* Annot8 (annot8.io) - Licensed under Apache-2.0. */
package io.annot8.common.implementations.factories;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

import io.annot8.core.data.Item;

public class NotifyingItemFactory extends SimpleItemFactory {

  private final Set> listeners = new HashSet<>();

  public NotifyingItemFactory(ItemCreator creator) {
    super(creator);
  }

  public void registerListener(Consumer consumer) {
    listeners.add(consumer);
  }

  public void unregisterListener(Consumer consumer) {
    listeners.remove(consumer);
  }

  @Override
  public Item create() {
    Item item = super.create();
    notifyListeners(item);
    return item;
  }

  @Override
  public Item create(Item parent) {
    Item item = super.create(parent);
    notifyListeners(item);
    return item;
  }

  private void notifyListeners(Item item) {
    listeners.forEach(l -> l.accept(item));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy