<commit_before>package be.digitalia.fosdem.widgets;

import android.content.Context;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Vertical LinearLayout populated by a special adapter.
 *
 * @author <NAME>
 */
public class AdapterLinearLayout extends LinearLayout {

	/**
	 * Implement this Adapter to populate the layout.
	 * Call notifyDataSetChanged() to update it.
	 */
	public static abstract class Adapter<T> {

		final DataSetObservable mDataSetObservable = new DataSetObservable();

		public void notifyDataSetChanged() {
			mDataSetObservable.notifyChanged();
		}

		public abstract int getCount();

		public abstract T getItem(int position);

		public abstract View getView(int position, View convertView, ViewGroup parent);
	}

	class AdapterLinearLayoutDataSetObserver extends DataSetObserver {
		@Override
		public void onChanged() {
			populateFromAdapter();
		}
	}

	private Adapter mAdapter;
	private AdapterLinearLayoutDataSetObserver mDataSetObserver;


	public AdapterLinearLayout(Context context) {
		this(context, null);
	}

	public AdapterLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(VERTICAL);
	}

	public void setAdapter(Adapter adapter) {
		if (mAdapter == adapter) {
			return;
		}
		if (mAdapter != null && mDataSetObserver != null) {
			mAdapter.mDataSetObservable.unregisterObserver(mDataSetObserver);
		}
		removeAllViews();
		mAdapter = adapter;
		if (adapter != null && mDataSetObserver != null) {
			populateFromAdapter();
			adapter.mDataSetObservable.registerObserver(mDataSetObserver);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		mDataSetObserver = new AdapterLinearLayoutDataSetObserver();
		if (mAdapter != null) {
			populateFromAdapter();
			mAdapter.mDataSetObservable.registerObserver(mDataSetObserver);
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		if (mAdapter != null) {
			mAdapter.mDataSetObservable.unregisterObserver(mDataSetObserver);
		}
		mDataSetObserver = null;
	}

	void populateFromAdapter() {
		final Adapter adapter = mAdapter;
		final int currentCount = getChildCount();
		final int newCount = adapter.getCount();
		final int commonCount = Math.min(currentCount, newCount);
		// 1. Update common views
		for (int i = 0; i < commonCount; ++i) {
			final View currentView = getChildAt(i);
			final View newView = adapter.getView(i, currentView, this);
			if (currentView != newView) {
				// Edge case: View is not recycled
				removeViewAt(i);
				addView(newView, i);
			}<commit_msg>Fixed rare case when trying to unregister a null observer<commit_after>package be.digitalia.fosdem.widgets;

import android.content.Context;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Vertical LinearLayout populated by a special adapter.
 *
 * @author <NAME>
 */
public class AdapterLinearLayout extends LinearLayout {

	/**
	 * Implement this Adapter to populate the layout.
	 * Call notifyDataSetChanged() to update it.
	 */
	public static abstract class Adapter<T> {

		final DataSetObservable mDataSetObservable = new DataSetObservable();

		public void notifyDataSetChanged() {
			mDataSetObservable.notifyChanged();
		}

		public abstract int getCount();

		public abstract T getItem(int position);

		public abstract View getView(int position, View convertView, ViewGroup parent);
	}

	class AdapterLinearLayoutDataSetObserver extends DataSetObserver {
		@Override
		public void onChanged() {
			populateFromAdapter();
		}
	}

	private Adapter mAdapter;
	private AdapterLinearLayoutDataSetObserver mDataSetObserver;


	public AdapterLinearLayout(Context context) {
		this(context, null);
	}

	public AdapterLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(VERTICAL);
	}

	public void setAdapter(Adapter adapter) {
		if (mAdapter == adapter) {
			return;
		}
		if (mAdapter != null && mDataSetObserver != null) {
			mAdapter.mDataSetObservable.unregisterObserver(mDataSetObserver);
		}
		removeAllViews();
		mAdapter = adapter;
		if (adapter != null && mDataSetObserver != null) {
			populateFromAdapter();
			adapter.mDataSetObservable.registerObserver(mDataSetObserver);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		mDataSetObserver = new AdapterLinearLayoutDataSetObserver();
		if (mAdapter != null) {
			populateFromAdapter();
			mAdapter.mDataSetObservable.registerObserver(mDataSetObserver);
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		if ((mAdapter != null) && (mDataSetObserver != null)) {
			mAdapter.mDataSetObservable.unregisterObserver(mDataSetObserver);
		}
		mDataSetObserver = null;
	}

	void populateFromAdapter() {
		final Adapter adapter = mAdapter;
		final int currentCount = getChildCount();
		final int newCount = adapter.getCount();
		final int commonCount = Math.min(currentCount, newCount);
		// 1. Update common views
		for (int i = 0; i < commonCount; ++i) {
			final View currentView = getChildAt(i);
			final View newView = adapter.getView(i, currentView, this);
			if (currentView != newView) {
				// Edge case: View is not recycled
				removeViewAt(i);
				addView(newView, i);
			}
{'fraction_non_alphanumeric': 0.0972470504111548, 'fraction_numerical': 0.0007150518412584912, 'mean_word_length': 4.791925465838509, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5397107', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1620, 'n_words': 415}