Skip to content

Commit

Permalink
Make broader use of IReadOnlyList
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Mar 20, 2018
1 parent 8d909a3 commit 5b71372
Show file tree
Hide file tree
Showing 34 changed files with 119 additions and 117 deletions.
18 changes: 9 additions & 9 deletions GitCommands/Config/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace GitCommands.Config
{
public class ConfigFile
{
public string FileName { get; }
private readonly List<IConfigSection> _configSections = new List<IConfigSection>();

public string FileName { get; }
public bool Local { get; }

public ConfigFile(string fileName, bool local)
{
ConfigSections = new List<IConfigSection>();
Local = local;

FileName = fileName;

try
{
Load();
Expand All @@ -31,7 +31,7 @@ public ConfigFile(string fileName, bool local)
}
}

public IList<IConfigSection> ConfigSections { get; }
public IReadOnlyList<IConfigSection> ConfigSections => _configSections;

public IEnumerable<IConfigSection> GetConfigSections(string sectionName)
{
Expand Down Expand Up @@ -214,7 +214,7 @@ public string GetPathValue(string setting)
return GetStringValue(setting);
}

public IList<string> GetValues(string setting)
public IReadOnlyList<string> GetValues(string setting)
{
var keyIndex = FindAndCheckKeyIndex(setting);

Expand Down Expand Up @@ -249,7 +249,7 @@ public IConfigSection FindOrCreateConfigSection(string name)
if (result == null)
{
result = new ConfigSection(name, true);
ConfigSections.Add(result);
_configSections.Add(result);
}

return result;
Expand All @@ -262,7 +262,7 @@ public void AddConfigSection(IConfigSection configSection)
throw new ArgumentException("Can not add a section that already exists: " + configSection.SectionName);
}

ConfigSections.Add(configSection);
_configSections.Add(configSection);
}

public void RemoveConfigSection(string configSectionName)
Expand All @@ -274,7 +274,7 @@ public void RemoveConfigSection(string configSectionName)
return;
}

ConfigSections.Remove(configSection);
_configSections.Remove(configSection);
}

public IConfigSection FindConfigSection(string name)
Expand Down Expand Up @@ -336,7 +336,7 @@ private void NewSection()
if (_section == null)
{
_section = new ConfigSection(sectionName, false);
_configFile.ConfigSections.Add(_section);
_configFile._configSections.Add(_section);
}
}

Expand Down
10 changes: 5 additions & 5 deletions GitCommands/Config/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace GitCommands.Config
/// </summary>
public class ConfigSection : IConfigSection
{
private readonly IDictionary<string, IList<string>> _configKeys;
private readonly IDictionary<string, List<string>> _configKeys;

internal ConfigSection(string name, bool forceCaseSensitive)
{
_configKeys = new Dictionary<string, IList<string>>(StringComparer.OrdinalIgnoreCase);
_configKeys = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);

if (name.Contains("\""))
{
Expand Down Expand Up @@ -73,9 +73,9 @@ public static string FixPath(string path)
return path.ToPosixPath();
}

public IDictionary<string, IList<string>> AsDictionary()
public IDictionary<string, IReadOnlyList<string>> AsDictionary()
{
return _configKeys.ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase);
return _configKeys.ToDictionary(kv => kv.Key, kv => (IReadOnlyList<string>)kv.Value, StringComparer.OrdinalIgnoreCase);
}

public bool HasValue(string key)
Expand Down Expand Up @@ -128,7 +128,7 @@ public string GetValue(string key, string defaultValue)
return defaultValue;
}

public IList<string> GetValues(string key)
public IReadOnlyList<string> GetValues(string key)
{
return _configKeys.ContainsKey(key) ? _configKeys[key] : new List<string>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IConfiguredLinkDefinitionsProvider
/// <summary>
/// Loads all persisted external link definitions across all setting layers.
/// </summary>
IList<ExternalLinkDefinition> Get(RepoDistSettings settings);
IReadOnlyList<ExternalLinkDefinition> Get(RepoDistSettings settings);
}

/// <summary>
Expand All @@ -31,7 +31,7 @@ public ConfiguredLinkDefinitionsProvider(IExternalLinksLoader externalLinksLoade
/// <summary>
/// Loads all persisted external link definitions across all setting layers.
/// </summary>
public IList<ExternalLinkDefinition> Get(RepoDistSettings settings)
public IReadOnlyList<ExternalLinkDefinition> Get(RepoDistSettings settings)
{
if (settings == null)
{
Expand Down
6 changes: 3 additions & 3 deletions GitCommands/ExternalLinks/ExternalLinkRevisionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ private static IEnumerable<GitRemote> GetMatchingRemotes(ExternalLinkDefinition

private IEnumerable<Match> ParseRemotes(ExternalLinkDefinition definition)
{
IList<Match> allMatches = new List<Match>();
var allMatches = new List<Match>();

if (definition.RemoteSearchPattern.IsNullOrWhiteSpace() || definition.RemoteSearchPatternRegex.Value == null)
{
allMatches.Add(null);
return allMatches;
}

IList<string> remoteUrls = new List<string>();
var remoteUrls = new List<string>();

var remotes = _gitRemoteManager.LoadRemotes(false);
var matchingRemotes = GetMatchingRemotes(definition, remotes);
Expand Down Expand Up @@ -128,7 +128,7 @@ private static IEnumerable<ExternalLink> ParseRevisionPart(GitRevision revision,
yield break;
}

IList<Match> allMatches = new List<Match>();
var allMatches = new List<Match>();

MatchCollection matches = definition.SearchPatternRegex.Value.Matches(part);
for (var i = 0; i < matches.Count; i++)
Expand Down
10 changes: 5 additions & 5 deletions GitCommands/ExternalLinks/ExternalLinksLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public interface IExternalLinksLoader
/// <summary>
/// Loads external link definitions from the settings.
/// </summary>
IList<ExternalLinkDefinition> Load(RepoDistSettings settings);
IReadOnlyList<ExternalLinkDefinition> Load(RepoDistSettings settings);

/// <summary>
/// Saves the provided external link definitions to the settings.
/// </summary>
void Save(RepoDistSettings settings, IList<ExternalLinkDefinition> definitions);
void Save(RepoDistSettings settings, IReadOnlyList<ExternalLinkDefinition> definitions);
}

public sealed class ExternalLinksLoader : IExternalLinksLoader
Expand All @@ -29,7 +29,7 @@ public sealed class ExternalLinksLoader : IExternalLinksLoader
/// <summary>
/// Loads external link definitions from the settings.
/// </summary>
public IList<ExternalLinkDefinition> Load(RepoDistSettings settings)
public IReadOnlyList<ExternalLinkDefinition> Load(RepoDistSettings settings)
{
var cachedSettings = new RepoDistSettings(null, settings.SettingsCache);
var xml = cachedSettings.GetString(SettingName, null);
Expand All @@ -39,7 +39,7 @@ public IList<ExternalLinkDefinition> Load(RepoDistSettings settings)
/// <summary>
/// Saves the provided external link definitions to the settings.
/// </summary>
public void Save(RepoDistSettings settings, IList<ExternalLinkDefinition> definitions)
public void Save(RepoDistSettings settings, IReadOnlyList<ExternalLinkDefinition> definitions)
{
try
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public void Save(RepoDistSettings settings, IList<ExternalLinkDefinition> defini
}

// TODO: refactor and outsource to the centralised SettingsSerialiser implementations.
private static IList<ExternalLinkDefinition> LoadFromXmlString(string xmlString)
private static IReadOnlyList<ExternalLinkDefinition> LoadFromXmlString(string xmlString)
{
if (string.IsNullOrWhiteSpace(xmlString))
{
Expand Down
11 changes: 6 additions & 5 deletions GitCommands/ExternalLinks/ExternalLinksManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public sealed class ExternalLinksManager
private readonly RepoDistSettings _cachedSettings;
private readonly ExternalLinksManager _lowerPriority;
private readonly IExternalLinksLoader _externalLinksLoader = new ExternalLinksLoader();
private readonly IList<ExternalLinkDefinition> _definitions;
private readonly List<ExternalLinkDefinition> _definitions;

public ExternalLinksManager(RepoDistSettings settings)
{
_cachedSettings = new RepoDistSettings(null, settings.SettingsCache);
_definitions = _externalLinksLoader.Load(_cachedSettings);
_definitions = _externalLinksLoader.Load(_cachedSettings).ToList();

if (settings.LowerPriority != null)
{
Expand Down Expand Up @@ -64,10 +64,11 @@ public bool Contains(string definitionName)
/// Loads all settings from all available levels.
/// </summary>
/// <returns>A collection of all available definitions.</returns>
public IList<ExternalLinkDefinition> GetEffectiveSettings()
public IReadOnlyList<ExternalLinkDefinition> GetEffectiveSettings()
{
var effective = _definitions.Union(_lowerPriority?.GetEffectiveSettings() ?? Enumerable.Empty<ExternalLinkDefinition>()).ToList();
return effective.ToList();
return _definitions
.Union(_lowerPriority?.GetEffectiveSettings() ?? Enumerable.Empty<ExternalLinkDefinition>())
.ToList();
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions GitCommands/Git/GitCommandHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ public static GitSubmoduleStatus GetSubmoduleStatus(string text, GitModule modul
/*
source: https://git-scm.com/docs/git-status
*/
public static List<GitItemStatus> GetAllChangedFilesFromString(IGitModule module, string statusString, bool fromDiff = false)
public static IReadOnlyList<GitItemStatus> GetAllChangedFilesFromString(IGitModule module, string statusString, bool fromDiff = false)
{
var diffFiles = new List<GitItemStatus>();

Expand Down Expand Up @@ -1130,7 +1130,7 @@ public static List<GitItemStatus> GetAllChangedFilesFromString(IGitModule module
}

// Doesn't work with removed submodules
IList<string> submodules = module.GetSubmodulesLocalPaths();
var submodules = module.GetSubmodulesLocalPaths();

// Split all files on '\0' (WE NEED ALL COMMANDS TO BE RUN WITH -z! THIS IS ALSO IMPORTANT FOR ENCODING ISSUES!)
var files = trimmedStatus.Split(new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
Expand Down
2 changes: 1 addition & 1 deletion GitCommands/Git/GitGpgController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private string GetTagVerificationMessage(IGitRef tagRef, bool raw = true)
return module.RunGitCmd($"verify-tag {rawFlag} {tagName}");
}

private string EvaluateTagVerifyMessage(IList<IGitRef> usefulTagRefs)
private string EvaluateTagVerifyMessage(IReadOnlyList<IGitRef> usefulTagRefs)
{
if (usefulTagRefs.Count == 0)
{
Expand Down

0 comments on commit 5b71372

Please sign in to comment.