<commit_before>/*
 * Copyright 2017 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.matthewtamlin.mixtape.library.mixtape_body;

import android.view.MenuItem;

import com.matthewtamlin.mixtape.library.base_mvp.BaseDataSource;
import com.matthewtamlin.mixtape.library.base_mvp.ListDataSource;
import com.matthewtamlin.mixtape.library.data.LibraryItem;

import java.util.List;

/**
 * A simple implementation of the BodyContract.Presenter interface where the list returned by the
 * data source is directly reflected in the view.
 *
 * @param <D>
 * 		the type of data to present
 * @param <S>
 * 		the type of data source to present from
 * @param <V>
 * 		the type of view to present to
 */
public class DirectBodyPresenter<
		D extends LibraryItem,
		S extends ListDataSource<D>,
		V extends BodyContract.View>
		implements BodyContract.Presenter<D, S, V> {
	/**
	 * The data source to present from.
	 */
	private S dataSource;

	/**
	 * The view to present to.
	 */
	private V view;

	@Override
	public void setDataSource(final S dataSource) {
		unsubscribeFromDataSourceCallbacks(this.dataSource);
		this.dataSource = dataSource;
		subscribeToDataSourceCallbacks(this.dataSource);

		if (dataSource != null) {
			dataSource.loadData(false, this);
		}
	}

	@Override
	public S getDataSource() {
		return dataSource;
	}

	@Override
	public void setView(final V view) {
		removeViewPresenter(this.view);
		this.view = view;
		setSelfAsViewPresenter(this.view);

		if (dataSource != null) {
			dataSource.loadData(false, this);
		}
	}

	@Override
	public V getView() {
		return view;
	}

	@Override
	public void onDataLoaded(BaseDataSource<List<D>> source, List<D> data) {
		if (view != null) {
			view.setItems(data);
		}
	}

	@Override
	public void onLoadDataFailed(final BaseDataSource source) {
		if (view != null) {
			view.setItems(null);
		}
	}

	@Override
	public void onDataReplaced(final BaseDataSource<List<D>> source, final List<D> oldData,
			final List<D> newData) {
		if (view != null) {
			view.setItems(newData);
		}
	}

	@Override
	public void onDataModified(final BaseDataSource<List<D>> source, final List<D> data) {
		if (view != null) {
			view.notifyItemsChanged();
		}
	}

	@Override
	public void onLongOperationStarted(final BaseDataSource source) {
		if (view != null) {
			view.showLoadingIndicator(true);
		}
	}

	@Override
	public void onLongOperationFinished(final BaseDataSource source) {
		if (view != null) {
			view.showLoadingIndicator(false);
		}
	}

	@Override
	public void onDataAdded(final ListDataSource<D> source, final D added, final int index) {
		if (view != null) {
			view.notifyItemAdded(index);
		}
	}

	@Override
	public void onDataRemoved(final ListDataSource<D> source, final D removed, final int index) {
		if (view != null) {
			view.notifyItemRemoved(index);
		}
	}

	@Override
	public void onItemModified(final ListDataSource<D> source, final D modified,
			final int index) {
		if (view != null) {
			view.notifyItemModified(index);
		}
	}

	@Override
	public void onDataMoved(final ListDataSource<D> source, final D moved, final int initialIndex,
			final int finalIndex) {
		if (view != null) {
			view.notifyItemMoved(initialIndex, finalIndex);
		}
	}

	@Override
	public void onLibraryItemSelected(final BodyContract.View bodyView, final LibraryItem item) {

	}

	@Override
	public void onContextualMenuItemSelected(final BodyContract.View bodyView,
			final LibraryItem libraryItem,
			final MenuItem menuItem) {

	}

	/**
	 * Unsubscribes this presenter from all callbacks delivered by the supplied data source.
	 *
	 * @param dataSource
	 * 		the data source to unsubscribe from, may be null
	 */
	protected void unsubscribeFromDataSourceCallbacks(final S dataSource) {
		if (dataSource != null) {
			dataSource.unregisterDataReplacedListener(this);<commit_msg>Added methods for registering/unregistering listeners<commit_after>/*
 * Copyright 2017 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.matthewtamlin.mixtape.library.mixtape_body;

import android.view.MenuItem;

import com.matthewtamlin.mixtape.library.base_mvp.BaseDataSource;
import com.matthewtamlin.mixtape.library.base_mvp.ListDataSource;
import com.matthewtamlin.mixtape.library.data.LibraryItem;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * A simple implementation of the BodyContract.Presenter interface where the list returned by the
 * data source is directly reflected in the view.
 *
 * @param <D>
 * 		the type of data to present
 * @param <S>
 * 		the type of data source to present from
 * @param <V>
 * 		the type of view to present to
 */
public class DirectBodyPresenter<
		D extends LibraryItem,
		S extends ListDataSource<D>,
		V extends BodyContract.View>
		implements BodyContract.Presenter<D, S, V> {
	private final Set<LibraryItemSelectedListener<D, S, V>> libraryItemSelectedListeners =
			new HashSet<>();

	private final Set<ContextualMenuItemSelectedListener<D, S, V>>
			contextualMenuItemSelectedListeners = new HashSet<>();

	/**
	 * The data source to present from.
	 */
	private S dataSource;

	/**
	 * The view to present to.
	 */
	private V view;

	@Override
	public void setDataSource(final S dataSource) {
		unsubscribeFromDataSourceCallbacks(this.dataSource);
		this.dataSource = dataSource;
		subscribeToDataSourceCallbacks(this.dataSource);

		if (dataSource != null) {
			dataSource.loadData(false, this);
		}
	}

	@Override
	public S getDataSource() {
		return dataSource;
	}

	@Override
	public void setView(final V view) {
		removeViewPresenter(this.view);
		this.view = view;
		setSelfAsViewPresenter(this.view);

		if (dataSource != null) {
			dataSource.loadData(false, this);
		}
	}

	@Override
	public V getView() {
		return view;
	}

	@Override
	public void onDataLoaded(BaseDataSource<List<D>> source, List<D> data) {
		if (view != null) {
			view.setItems(data);
		}
	}

	@Override
	public void onLoadDataFailed(final BaseDataSource source) {
		if (view != null) {
			view.setItems(null);
		}
	}

	@Override
	public void onDataReplaced(final BaseDataSource<List<D>> source, final List<D> oldData,
			final List<D> newData) {
		if (view != null) {
			view.setItems(newData);
		}
	}

	@Override
	public void onDataModified(final BaseDataSource<List<D>> source, final List<D> data) {
		if (view != null) {
			view.notifyItemsChanged();
		}
	}

	@Override
	public void onLongOperationStarted(final BaseDataSource source) {
		if (view != null) {
			view.showLoadingIndicator(true);
		}
	}

	@Override
	public void onLongOperationFinished(final BaseDataSource source) {
		if (view != null) {
			view.showLoadingIndicator(false);
		}
	}

	@Override
	public void onDataAdded(final ListDataSource<D> source, final D added, final int index) {
		if (view != null) {
			view.notifyItemAdded(index);
		}
	}

	@Override
	public void onDataRemoved(final ListDataSource<D> source, final D removed, final int index) {
		if (view != null) {
			view.notifyItemRemoved(index);
		}
	}

	@Override
	public void onItemModified(final ListDataSource<D> source, final D modified,
			final int index) {
		if (view != null) {
			view.notifyItemModified(index);
		}
	}

	@Override
	public void onDataMoved(final ListDataSource<D> source, final D moved, final int initialIndex,
			final int finalIndex) {
		if (view != null) {
			view.notifyItemMoved(initialIndex, finalIndex);
		}
	}

	@Override
	public void onLibraryItemSelected(final BodyContract.View bodyView, final LibraryItem item) {

	}

	@Override
	public void onContextualMenuItemSelected(final BodyContract.View bodyView,
			final LibraryItem libraryItem,
			final MenuItem menuItem) {

	}

	/**
	 * Registers a library item selected listener to this presenter. If the supplied listener is
	 * null or is already registered, this method exits normally.
	 *
	 * @param listener
	 * 		the listener to register
	 */
	public void registerListener(final LibraryItemSelectedListener<D, S, V> listener) {
		if (listener != null) {
			libraryItemSelectedListeners.add(listener);
		}
	}

	/**
	 * Unregisters a library item selected listener from this data source. If the supplied listener
	 * is null or is not registered, this method exits normally.
	 *
	 * @param listener
	 * 		the listener to unregister
	 */
	public void unregisterListener(final LibraryItemSelectedListener<D, S, V> listener) {
		libraryItemSelectedListeners.remove(listener);
	}

	/**
	 * Registers a contextual menu item selected listener to this presenter. If the supplied
	 * listener is null or is already registered, this method exits normally.
	 *
	 * @param listener
	 * 		the listener to register
	 */
	public void registerListener(final ContextualMenuItemSelectedListener<D, S, V> listener) {
		if (listener != null) {
			contextualMenuItemSelectedListeners.add(listener);
		}
	}

	/**
	 * Unregisters a contextual menu item selected listener from this data source. If the supplied
	 * listener is null or is not registered, this method exits normally.
	 *
	 * @param listener
	 * 		the listener to unregister
	 */
	public void unregisterListener(final ContextualMenuItemSelectedListener<D, S, V> listener) {
		contextualMenuItemSelectedListeners.remove(listener);
	}

	/**
	 * Unsubscribes this presenter from all callbacks delivered by the supplied data source.
	 *
	 * @param dataSource
	 * 		the data source to unsubscribe from, may be null
	 */
	protected void unsubscribeFromDataSourceCallbacks(final S dataSource) {
		if (dataSource != null) {
			dataSource.unregisterDataReplacedListener(this);
{'fraction_non_alphanumeric': 0.09436992253920272, 'fraction_numerical': 0.0015114301908180617, 'mean_word_length': 4.139320388349515, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3961696', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3161, 'n_words': 997}