<commit_before>/*
 * Copyright 2013 <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.ae.apps.randomcontact.managers;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import android.content.ContentResolver;
import android.content.res.Resources;

import com.ae.apps.common.managers.ContactManager;
import com.ae.apps.common.vo.ContactVo;

/**
 * Adds the special bit of randomness to Contact Manager. Overrides the basic random contact generation of the
 * ContactManager
 * 
 * @author Midhunhk
 * 
 */
public class RandomContactManager extends ContactManager implements FilteredContactList {

	private int	index	= 0;

	public RandomContactManager(ContentResolver contentResolver, Resources res) {
		super(contentResolver, res);
		// In order to avoid repetition of contacts, we simply start from a random point in the list of contacts
		// Hopefully the user has a very large number of contacts with phone numbers and may not notice :)
		index = new Random().nextInt(getTotalContactCount());
	}

	/**
	 * Override the default implementation
	 * @return a random contact object, null if no contacts found
	 */
	@Override
	public ContactVo getRandomContact() {
		if ( !contactsList.isEmpty() ) {
			// Increment the index - we will wrap around when we reach the end
			index = (index + 1) % contactsList.size();
			// Make sure the phone details are present
			ContactVo contactVo = getContactPhoneDetails(contactsList.get(index));
			// Update the contacts list with the updated contact item
			contactsList.set(index, contactVo);
			return contactVo;
		} else {
			return null;
		}
	}

	@Override
	public List<ContactVo> getTopFrequentlyContacted(int maxResults) {
		List<ContactVo> filteredList = contactsList;
		if(null != contactsList && !contactsList.isEmpty()){
			List<ContactVo> listToFilter = getAllContacts();
			int contactsToShow = Math.min(maxResults, filteredList.size());
			
			// Apply the filter to the contacts
			Collections.sort(listToFilter, new Comparator<ContactVo>(){
				@Override
				public int compare(ContactVo contact1, ContactVo contact2) {
					return Integer.valueOf(contact2.getTimesContacted())
							.compareTo(Integer.valueOf(contact1.getTimesContacted()));
				}
			});
			
			// We shall respect the maxResults param and return only a subset of the filtered data 
			filteredList = listToFilter.subList(0, contactsToShow);
		}
		return filteredList;
	}

}
<commit_msg>Issue #1 fix - check contacts list length before using it to generate a random number<commit_after>/*
 * Copyright 2013 <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.ae.apps.randomcontact.managers;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import android.content.ContentResolver;
import android.content.res.Resources;

import com.ae.apps.common.managers.ContactManager;
import com.ae.apps.common.vo.ContactVo;

/**
 * Adds the special bit of randomness to Contact Manager. Overrides the basic random contact generation of the
 * ContactManager
 * 
 * @author Midhunhk
 * 
 */
public class RandomContactManager extends ContactManager implements FilteredContactList {

	private int	index	= 0;

	public RandomContactManager(ContentResolver contentResolver, Resources res) {
		super(contentResolver, res);
		// In order to avoid repetition of contacts, we simply start from a random point in the list of contacts
		// Hopefully the user has a very large number of contacts with phone numbers and may not notice :)
		// This would be based on the order the contacts were added to the contacts database which should
		// be random unless it was imported in a sorted order (the chances of which are less)
		int totalContactCount = getTotalContactCount();
		
		// Handle conditions when there are no contacts. This can happen when
		// 	1. The user has no contacts yet
		//	2. Access to Contacts permission denied in Marshmallow and up 
		if(totalContactCount > 0){
			index = new Random().nextInt(getTotalContactCount());
		}
	}

	/**
	 * Override the default implementation
	 * @return a random contact object, null if no contacts found
	 */
	@Override
	public ContactVo getRandomContact() {
		if ( !contactsList.isEmpty() ) {
			// Increment the index - we will wrap around when we reach the end
			index = (index + 1) % contactsList.size();
			// Make sure the phone details are present
			ContactVo contactVo = getContactPhoneDetails(contactsList.get(index));
			// Update the contacts list with the updated contact item
			contactsList.set(index, contactVo);
			return contactVo;
		} else {
			return null;
		}
	}

	@Override
	public List<ContactVo> getTopFrequentlyContacted(int maxResults) {
		List<ContactVo> filteredList = contactsList;
		if(null != contactsList && !contactsList.isEmpty()){
			List<ContactVo> listToFilter = getAllContacts();
			int contactsToShow = Math.min(maxResults, filteredList.size());
			
			// Apply the filter to the contacts
			Collections.sort(listToFilter, new Comparator<ContactVo>(){
				@Override
				public int compare(ContactVo contact1, ContactVo contact2) {
					return Integer.valueOf(contact2.getTimesContacted())
							.compareTo(Integer.valueOf(contact1.getTimesContacted()));
				}
			});
			
			// We shall respect the maxResults param and return only a subset of the filtered data 
			filteredList = listToFilter.subList(0, contactsToShow);
		}
		return filteredList;
	}

}

{'fraction_non_alphanumeric': 0.08107302533532042, 'fraction_numerical': 0.005067064083457526, 'mean_word_length': 3.5131136516476125, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '3233114', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1755, 'n_words': 672}