Skip to content

Commit

Permalink
Refactoring and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JeevanJames committed May 5, 2018
1 parent d9bb92d commit fab266d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Id3.Net/Id3.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net40</TargetFrameworks>
<RootNamespace>Id3</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageId>ID3</PackageId>
<Version>0.5.0-beta.1</Version>
<Authors>Jeevan James</Authors>
Expand Down
57 changes: 40 additions & 17 deletions src/Id3.Net/Id3/Id3Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Id3
{
/// <summary>
/// Represents an ID3 tag.
/// <para />
/// This class is agnostic of any ID3 tag version. It contains all the possible properties that can be assigned across
/// all ID3 tag versions.
/// </summary>
Expand Down Expand Up @@ -83,7 +84,7 @@ public Id3Tag ConvertTo(Id3Version version)
#region Frame operations
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<Id3Frame>) this).GetEnumerator();
return ((IEnumerable<Id3Frame>)this).GetEnumerator();
}

IEnumerator<Id3Frame> IEnumerable<Id3Frame>.GetEnumerator()
Expand All @@ -95,7 +96,7 @@ IEnumerator<Id3Frame> IEnumerable<Id3Frame>.GetEnumerator()
foreach (Id3Frame frame in list)
yield return frame;
} else
yield return (Id3Frame) kvp.Value;
yield return (Id3Frame)kvp.Value;
}
}

Expand All @@ -113,7 +114,7 @@ public void Cleanup()
{
for (int i = frameList.Count - 1; i >= 0; i--)
{
var frame = (Id3Frame) frameList[i];
var frame = (Id3Frame)frameList[i];
if (!frame.IsAssigned)
frameList.RemoveAt(i);
}
Expand All @@ -124,7 +125,7 @@ public void Cleanup()
} else
{
//Get the item value as a frame and mark it for removal if it is unassigned
var frame = (Id3Frame) kvp.Value;
var frame = (Id3Frame)kvp.Value;
if (!frame.IsAssigned)
keysToDelete.Add(kvp.Key);
}
Expand All @@ -145,23 +146,43 @@ public int Clear()
return clearedCount;
}

public bool Contains<TFrame>(Expression<Func<Id3Tag, TFrame>> frameProperty) where TFrame : Id3Frame
public bool Contains<TFrame>(Expression<Func<Id3Tag, TFrame>> frameProperty)
where TFrame : Id3Frame
{
if (frameProperty == null)
throw new ArgumentNullException(nameof(frameProperty));

var lambda = (LambdaExpression) frameProperty;
var memberExpression = (MemberExpression) lambda.Body;
var property = (PropertyInfo) memberExpression.Member;
var lambda = (LambdaExpression)frameProperty;
var memberExpression = (MemberExpression)lambda.Body;
var property = (PropertyInfo)memberExpression.Member;
return this.Any(f => f.GetType() == property.PropertyType && f.IsAssigned);
}

/// <summary>
/// Returns the total number of frames in this tag.
/// </summary>
/// <param name="onlyAssignedFrames">If true, counts only assigned frames.</param>
/// <returns>Total number of frames in the tag.</returns>
public int GetCount(bool onlyAssignedFrames = true)
{
var count = 0;
foreach (KeyValuePair<Type, object> kvp in _frames)
{
if (kvp.Value is IList list)
count += onlyAssignedFrames ? list.Cast<Id3Frame>().Count(frame => frame.IsAssigned) : list.Count;
else
count += onlyAssignedFrames ? (((Id3Frame)kvp.Value).IsAssigned ? 1 : 0) : 1;
}
return count;
}

/// <summary>
/// Removes any frames of the specified type from the tag.
/// </summary>
/// <typeparam name="TFrame">Type of frame to remove</typeparam>
/// <returns>True, if matching frames were removed, otherwise false.</returns>
public bool Remove<TFrame>() where TFrame : Id3Frame
public bool Remove<TFrame>()
where TFrame : Id3Frame
{
Type frameType = typeof(TFrame);

Expand Down Expand Up @@ -193,7 +214,7 @@ public int RemoveWhere<TFrame>(Func<TFrame, bool> predicate)
{
for (int i = list.Count - 1; i >= 0; i--)
{
if (predicate((TFrame) list[i]))
if (predicate((TFrame)list[i]))
{
list.RemoveAt(i);
removalCount++;
Expand All @@ -204,7 +225,7 @@ public int RemoveWhere<TFrame>(Func<TFrame, bool> predicate)
_frames.Remove(frameType);
} else
{
var frame = (TFrame) frameObj;
var frame = (TFrame)frameObj;
if (predicate(frame))
{
_frames.Remove(frameType);
Expand Down Expand Up @@ -408,7 +429,7 @@ public YearFrame Year
/// This method is meant to be called by <see cref="Id3Handler" /> instances when they are reading the ID3 data and
/// populating this object.
/// </summary>
/// <param name="frame">The <see cref="Id3Frame"/> instance to add.</param>
/// <param name="frame">The <see cref="Id3Frame" /> instance to add.</param>
internal void AddUntypedFrame(Id3Frame frame)
{
Type frameType = frame.GetType();
Expand All @@ -417,7 +438,7 @@ internal void AddUntypedFrame(Id3Frame frame)
{
IList list;
if (containsKey)
list = (IList) _frames[frameType];
list = (IList)_frames[frameType];
else
{
list = MultiInstanceFrameTypes[frameType]();
Expand All @@ -435,16 +456,18 @@ internal void AddUntypedFrame(Id3Frame frame)
}
}

private TFrame GetSingleFrame<TFrame>() where TFrame : Id3Frame, new()
private TFrame GetSingleFrame<TFrame>()
where TFrame : Id3Frame, new()
{
if (_frames.TryGetValue(typeof(TFrame), out object frameObj))
return (TFrame) frameObj;
return (TFrame)frameObj;
var frame = new TFrame();
_frames.Add(typeof(TFrame), frame);
return frame;
}

private void SetSingleFrame<TFrame>(TFrame frame) where TFrame : Id3Frame
private void SetSingleFrame<TFrame>(TFrame frame)
where TFrame : Id3Frame
{
Type frameType = typeof(TFrame);
bool containsKey = _frames.ContainsKey(frameType);
Expand All @@ -466,7 +489,7 @@ private TFrameList GetMultipleFrames<TFrame, TFrameList>()
where TFrameList : IList<TFrame>, new()
{
if (_frames.TryGetValue(typeof(TFrame), out object frameListObj))
return (TFrameList) frameListObj;
return (TFrameList)frameListObj;
var framesList = new TFrameList();
_frames.Add(typeof(TFrame), framesList);
return framesList;
Expand Down
14 changes: 7 additions & 7 deletions src/Id3.Net/Id3/Id3TagFamily.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ limitations under the License.
namespace Id3
{
/// <summary>
/// Specifies families of ID3 tag versions that are mutually-inclusive.
/// Specifies families of ID3 tag versions that are mutually-inclusive.
/// </summary>
public enum Id3TagFamily
{
/// <summary>
/// Indicates ID3 tags in the v2 range (currently v2.2, v2.3 and v2.4). These tags appear at
/// the beginning of the MP3 file.
/// Indicates ID3 tags in the v2 range (currently v2.2, v2.3 and v2.4). These tags appear at
/// the beginning of the MP3 file.
/// </summary>
Version2X,

/// <summary>
/// Indicates ID3 tags in the v1 range (currently v1.0 and v1.1). These tags appear at the end
/// of the MP3 file.
/// Indicates ID3 tags in the v1 range (currently v1.0 and v1.1). These tags appear at the end
/// of the MP3 file.
/// </summary>
Version1X,
Version1X
}
}
}
2 changes: 0 additions & 2 deletions src/Id3.Net/Id3/Id3Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace Id3
public enum Id3Version
{
V1X = 1,
V22 = 2,
V23 = 3,
V24 = 4
}
}
16 changes: 10 additions & 6 deletions src/Id3.Net/Id3/TagComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,31 @@ limitations under the License.
namespace Id3
{
/// <summary>
/// Compares two tags for equality.
/// Compares two tags for equality.
/// </summary>
public sealed class TagComparer : IEqualityComparer<Id3Tag>
{
public bool CompareOnlyAssignedFrames { get; }

public TagComparer(bool compareOnlyAssignedFrames = false)
{
CompareOnlyAssignedFrames = compareOnlyAssignedFrames;
}

bool IEqualityComparer<Id3Tag>.Equals(Id3Tag tag1, Id3Tag tag2)
{
if (ReferenceEquals(tag1, tag2))
return true;
if (tag1 == null || tag2 == null)
return false;
if (tag1.Count() != tag2.Count())
if (tag1.GetCount(CompareOnlyAssignedFrames) != tag2.GetCount(CompareOnlyAssignedFrames))
return false;

//TODO: Compare frames

return true;
}

int IEqualityComparer<Id3Tag>.GetHashCode(Id3Tag tag)
{
return tag.GetHashCode();
}
int IEqualityComparer<Id3Tag>.GetHashCode(Id3Tag tag) => tag.GetHashCode();
}
}

0 comments on commit fab266d

Please sign in to comment.