Skip to content

Commit

Permalink
Minor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
muratcakir committed Aug 2, 2022
1 parent bca2f3a commit 80c631c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
1 change: 1 addition & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"AppVeyor",
"AzurePipelines",
"Bamboo",
"Bitbucket",
"Bitrise",
"GitHubActions",
"GitLab",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public virtual async Task RebuildAsync(XmlSitemapBuildContext ctx)
foreach (var data in languageData.Values)
{
// Create index documents (if any)
if (hasIndex && indexNodes.Any())
if (hasIndex && indexNodes.Count > 0)
{
var indexDocument = CreateSitemapIndexDocument(indexNodes[data.Language.Id]);
await SaveTempAsync(new List<string> { indexDocument }, data, 0);
Expand Down
85 changes: 61 additions & 24 deletions src/Smartstore/Collections/MultiMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,15 @@ protected virtual ICollection<TValue> CreateCollection(IEnumerable<TValue> value
/// </summary>
public int Count
{
get
{
return this._dict.Keys.Count;
}
get => _dict.Keys.Count;
}

/// <summary>
/// Gets the total count of items in all groups.
/// </summary>
public int TotalValueCount
{
get
{
return this._dict.Values.Sum(x => x.Count);
}
get => _dict.Values.Sum(x => x.Count);
}

/// <summary>
Expand All @@ -126,9 +120,13 @@ public virtual ICollection<TValue> this[TKey key]
if (!_dict.ContainsKey(key))
{
if (!_isReadonly)
{
_dict[key] = CreateCollection(null);
}
else
{
return null;
}
}

return _dict[key];
Expand All @@ -140,25 +138,25 @@ public virtual ICollection<TValue> this[TKey key]
/// </summary>
public virtual ICollection<TKey> Keys
{
get { return _dict.Keys; }
get => _dict.Keys;
}

/// <summary>
/// Gets all value collections.
/// </summary>
public virtual ICollection<ICollection<TValue>> Values
{
get { return _dict.Values; }
get => _dict.Values;
}

public IEnumerable<TValue> Find(TKey key, Func<TValue, bool> predicate)
{
Guard.NotNull(key, nameof(key));
Guard.NotNull(predicate, nameof(predicate));

if (_dict.ContainsKey(key))
if (_dict.TryGetValue(key, out var values))
{
return _dict[key].Where(predicate);
return values.Where(predicate);
}

return Enumerable.Empty<TValue>();
Expand All @@ -184,7 +182,9 @@ public virtual void Add(TKey key, TValue value)
public virtual void AddRange(TKey key, IEnumerable<TValue> values)
{
if (values == null || !values.Any())
{
return;
}

CheckNotReadonly();

Expand All @@ -196,19 +196,23 @@ public virtual void AddRange(TKey key, IEnumerable<TValue> values)
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns><c>True</c> if such a value existed and was removed; otherwise <c>false</c>.</returns>
/// <returns><c>true</c> if such a value existed and was removed; otherwise <c>false</c>.</returns>
public virtual bool Remove(TKey key, TValue value)
{
CheckNotReadonly();

if (!_dict.ContainsKey(key))
return false;
if (_dict.TryGetValue(key, out var values))
{
var removed = values.Remove(value);
if (removed && values.Count == 0)
{
_dict.Remove(key);
}

bool result = _dict[key].Remove(value);
if (_dict[key].Count == 0)
_dict.Remove(key);
return removed;
}

return result;
return false;
}

/// <summary>
Expand Down Expand Up @@ -259,9 +263,12 @@ public virtual bool TryGetValues(TKey key, out ICollection<TValue> values)
/// <returns><c>True</c> if the multimap contains such a value; otherwise, <c>false</c>.</returns>
public virtual bool ContainsValue(TKey key, TValue value)
{
return _dict.ContainsKey(key) && _dict[key].Contains(value);
return _dict.TryGetValue(key, out var values) && values.Contains(value);
}

public IDictionary<TKey, ICollection<TValue>> AsDictionary()
=> _dict;

/// <summary>
/// Returns an enumerator that iterates through the multimap.
/// </summary>
Expand All @@ -277,8 +284,7 @@ IEnumerator IEnumerable.GetEnumerator()
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the multimap.</returns>
public virtual IEnumerator<KeyValuePair<TKey, ICollection<TValue>>> GetEnumerator()
{
foreach (KeyValuePair<TKey, ICollection<TValue>> pair in _dict)
yield return pair;
return _dict.GetEnumerator();
}

private void CheckNotReadonly()
Expand All @@ -287,8 +293,6 @@ private void CheckNotReadonly()
throw new NotSupportedException("Multimap is read-only.");
}

#region Static members

public static Multimap<TKey, TValue> CreateFromLookup(ILookup<TKey, TValue> source)
{
Guard.NotNull(source, nameof(source));
Expand All @@ -303,6 +307,39 @@ public static Multimap<TKey, TValue> CreateFromLookup(ILookup<TKey, TValue> sour
return map;
}

#region Backlog

//class GroupingIterator : IEnumerator<IGrouping<TKey, TValue>>
//{
// private readonly IEnumerator<KeyValuePair<TKey, ICollection<TValue>>> _inner;

// public GroupingIterator(IEnumerator<KeyValuePair<TKey, ICollection<TValue>>> inner)
// {
// _inner = inner;
// }

// public IGrouping<TKey, TValue> Current => new GroupingWrapper(_inner.Current);
// object IEnumerator.Current => new GroupingWrapper(_inner.Current);

// public bool MoveNext() => _inner.MoveNext();
// public void Reset() => _inner.Reset();
// public void Dispose() => _inner.Dispose();
//}

//class GroupingWrapper : IGrouping<TKey, TValue>
//{
// private readonly KeyValuePair<TKey, ICollection<TValue>> _inner;

// public GroupingWrapper(KeyValuePair<TKey, ICollection<TValue>> inner)
// {
// _inner = inner;
// }

// public TKey Key => _inner.Key;
// public IEnumerator<TValue> GetEnumerator() => _inner.Value.GetEnumerator();
// IEnumerator IEnumerable.GetEnumerator() => _inner.Value.GetEnumerator();
//}

#endregion
}
}
7 changes: 4 additions & 3 deletions src/Smartstore/Domain/AttributeSelection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Newtonsoft.Json;
using Smartstore.Collections;
Expand Down Expand Up @@ -271,7 +272,7 @@ protected virtual AllAttributes GetFromXmlOrJson()
{
var attributes = JsonConvert.DeserializeObject<AllAttributes>(_rawAttributes);

if (attributes.CustomAttributes.Any())
if (attributes.CustomAttributes.Count > 0)
{
// Convert custom attributes from JObject to specific type.
var newAttributes = new Multimap<string, object>();
Expand Down Expand Up @@ -478,7 +479,7 @@ protected class AllAttributes
// prevents CustomAttributes from being serialized if empty.
// Deserialized to an empty map if missing in raw JSON string.
public bool ShouldSerializeCustomAttributes()
=> CustomAttributes?.Any() ?? false;
=> CustomAttributes?.Count > 0;
}
}
}

0 comments on commit 80c631c

Please sign in to comment.