package ru.rsdn.patterns.behavioral.observer.problems.leak;

import java.util.concurrent.CopyOnWriteArrayList;

import ru.rsdn.patterns.behavioral.observer.AbstractPublisher;
import ru.rsdn.patterns.behavioral.observer.ISubscriber;

/**
 * Подписчик, использующий финализатор для снятия регистрации
 */
public final class NonTrivialFinalizeSubscriber implements ISubscriber {
	private final AbstractPublisher<CopyOnWriteArrayList<ISubscriber>> publisher;

	/**
	 * Конструктор
	 * @param _publisher издатель
	 */
	public NonTrivialFinalizeSubscriber(
			AbstractPublisher<CopyOnWriteArrayList<ISubscriber>> _publisher) {
		if (_publisher == null)
			throw new NullPointerException("Пустой параметр");
		publisher = _publisher;
		publisher.subscribe(this);
	}

	@Override
	protected void finalize() throws Throwable {
		publisher.unsubscribe(this);
		super.finalize();
	}

	public void eventHappen() {
	}
}
