<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Contacts;
using Google.GData.Contacts;

namespace CalDavSynchronizer.Implementation.GoogleContacts
{
  public class GoogleGroupCache
  {
    readonly Dictionary<string, Group> _groupsByName = new Dictionary<string, Group>();
    private readonly IGoogleApiOperationExecutor _apiOperationExecutor;

    private string _defaultGroupIdOrNull;

    public GoogleGroupCache (IGoogleApiOperationExecutor apiOperationExecutor)
    {
      if (apiOperationExecutor == null)
        throw new ArgumentNullException (nameof (apiOperationExecutor));

      _apiOperationExecutor = apiOperationExecutor;
    }

    public IEnumerable<Group> Groups => _groupsByName.Values;

    public string DefaultGroupIdOrNull
    {
      get { return _defaultGroupIdOrNull; }
    }

    public void Fill ()
    {
      SetGroups (_apiOperationExecutor.Execute (f => f.GetGroups().Entries));
    }

    void SetGroups (IEnumerable<Group> existingGroups)
    {
      foreach (var group in existingGroups)
      {
        Group existingGroup;
        Group winningGroup;
        if (_groupsByName.TryGetValue (group.Title, out existingGroup))
        {
          if (!string.IsNullOrEmpty (existingGroup.SystemGroup))
          {
            winningGroup = existingGroup;
          }
          else if (!string.IsNullOrEmpty (group.SystemGroup))
          {
            winningGroup = group;
          }
          else
          {
            if (string.CompareOrdinal (group.Id, existingGroup.Id) > 0)
              winningGroup = group;
            else
              winningGroup = existingGroup;
          }
        }
        else
        {<commit_msg>Ticket #511 System.Collections.Generic.KeyNotFoundException: Consider Paging when fetching groups<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;

namespace CalDavSynchronizer.Implementation.GoogleContacts
{
  public class GoogleGroupCache
  {
    readonly Dictionary<string, Group> _groupsByName = new Dictionary<string, Group>();
    private readonly IGoogleApiOperationExecutor _apiOperationExecutor;

    private string _defaultGroupIdOrNull;

    public GoogleGroupCache (IGoogleApiOperationExecutor apiOperationExecutor)
    {
      if (apiOperationExecutor == null)
        throw new ArgumentNullException (nameof (apiOperationExecutor));

      _apiOperationExecutor = apiOperationExecutor;
    }

    public IEnumerable<Group> Groups => _groupsByName.Values;

    public string DefaultGroupIdOrNull
    {
      get { return _defaultGroupIdOrNull; }
    }

    public void Fill ()
    {
      var groups = new List<Group>();

      for (
       Feed<Group> feed = _apiOperationExecutor.Execute (f => f.GetGroups());
       feed != null;
       feed = _apiOperationExecutor.Execute (f => f.Get (feed, FeedRequestType.Next)))
      {
        groups.AddRange(feed.Entries);
      }

      SetGroups (groups);
    }

    void SetGroups (IEnumerable<Group> existingGroups)
    {
      foreach (var group in existingGroups)
      {
        Group existingGroup;
        Group winningGroup;
        if (_groupsByName.TryGetValue (group.Title, out existingGroup))
        {
          if (!string.IsNullOrEmpty (existingGroup.SystemGroup))
          {
            winningGroup = existingGroup;
          }
          else if (!string.IsNullOrEmpty (group.SystemGroup))
          {
            winningGroup = group;
          }
          else
          {
            if (string.CompareOrdinal (group.Id, existingGroup.Id) > 0)
              winningGroup = group;
            else
              winningGroup = existingGroup;
          }
        }
        else
        {
{'fraction_non_alphanumeric': 0.07952796305797845, 'fraction_numerical': 0.0012827090815802976, 'mean_word_length': 2.560730593607306, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, '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': '4333507', 'n_tokens_mistral': 1017, 'n_tokens_neox': 1004, 'n_words': 236}