Rewrote Reference class

This commit is contained in:
2014-08-30 10:24:01 +03:00
parent a3fdc31caa
commit 520eed12a6
18 changed files with 478 additions and 85 deletions

View File

@ -109,40 +109,5 @@ namespace RainmeterStudio.Core.Model
}
#endregion
#region Equals
public override bool Equals(object obj)
{
Project other = obj as Project;
if (other == null)
return false;
bool res = String.Equals(Author, other.Author);
res &= Reference.Equals(AutoLoadFile, other.AutoLoadFile);
res &= Version.Equals(MinimumRainmeter, other.MinimumRainmeter);
res &= Version.Equals(MinimumWindows, other.MinimumWindows);
res &= String.Equals(Name, other.Name);
res &= Tree<Reference>.Equals(Root, other.Root);
res &= Version.Equals(Version, other.Version);
return res;
}
public override int GetHashCode()
{
int hash = (Author == null) ? 0 : Author.GetHashCode();
hash = hash * 7 + ((AutoLoadFile == null) ? 0 : AutoLoadFile.GetHashCode());
hash = hash * 7 + ((MinimumRainmeter == null) ? 0 : MinimumRainmeter.GetHashCode());
hash = hash * 7 + ((MinimumWindows == null) ? 0 : MinimumWindows.GetHashCode());
hash = hash * 7 + ((Name == null) ? 0 : Name.GetHashCode());
hash = hash * 7 + ((Root == null) ? 0 : Root.GetHashCode());
hash = hash * 7 + ((Version == null) ? 0 : Version.GetHashCode());
return hash;
}
#endregion
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
@ -9,27 +10,89 @@ namespace RainmeterStudio.Core.Model
/// <summary>
/// Reference to a file or folder
/// </summary>
public class Reference
[DebuggerDisplay("ProjectPath = {ProjectPath}, StoragePath = {StoragePath}")]
public struct Reference
{
private string[] _projectPath;
private string _storagePath;
/// <summary>
/// Gets the name of the reference
/// </summary>
public string Name { get; private set; }
public string Name
{
get
{
// Try to get the last item from the project path
if (_projectPath != null && _projectPath.Length > 0)
return _projectPath[_projectPath.Length - 1];
// None found, return null
return null;
}
}
/// <summary>
/// Gets the path of the reference
/// Gets the path to the file on the disk. If reference is in a project, the path should be relative.
/// </summary>
public string Path { get; private set; }
public string StoragePath
{
get
{
return _storagePath;
}
}
/// <summary>
/// Gets the qualified path
/// </summary>
public string ProjectPath
{
get
{
if (_projectPath != null)
{
return _projectPath.Aggregate(String.Empty, (a, b) => a + "/" + b);
}
return null;
}
}
/// <summary>
/// Initializes the reference
/// </summary>
/// <param name="name">Name of reference</param>
/// <param name="path">Path to item referenced</param>
public Reference(string name, string path = null)
public Reference(string filePath, string projectPath = null)
{
Name = name;
Path = path;
_storagePath = filePath;
if (projectPath != null)
{
_projectPath = projectPath.Split('/').Skip(1).ToArray();
}
else
{
_projectPath = null;
}
}
/// <summary>
/// Checks if the reference points to a project item
/// </summary>
public bool IsInProject()
{
return (_projectPath != null);
}
/// <summary>
/// Checks if the reference has a file on disk
/// </summary>
/// <returns></returns>
public bool IsOnStorage()
{
return (_storagePath != null);
}
/// <summary>
@ -39,14 +102,20 @@ namespace RainmeterStudio.Core.Model
/// <returns>True if objects are equal</returns>
public override bool Equals(object obj)
{
var other = obj as Reference;
// Types are different, so not equal
if (other == null)
return false;
if (obj is Reference)
{
Reference other = (Reference)obj;
// Compare using string equals
return String.Equals(Name, other.Name) && String.Equals(Path, other.Path);
// 2 references are equal if they point to the same project item
if (_projectPath != null && other._projectPath != null)
return _projectPath.SequenceEqual(other._projectPath);
// If there is no project item, compare storage paths
if (_projectPath == null && other._projectPath == null)
return String.Equals(_storagePath, other._storagePath);
}
return false;
}
/// <summary>
@ -55,10 +124,28 @@ namespace RainmeterStudio.Core.Model
/// <returns>Hash code</returns>
public override int GetHashCode()
{
int hash = (Name == null) ? 0 : Name.GetHashCode();
hash = hash * 7 + ((Path == null) ? 0 : Path.GetHashCode());
int hash = (_projectPath == null) ? 0 : _projectPath.GetHashCode();
if (_projectPath != null)
{
foreach (var item in _projectPath)
hash = hash * 7 + item.GetHashCode();
}
else
{
hash = hash * 2113 + ((_storagePath == null) ? 0 : _storagePath.GetHashCode());
}
return hash;
}
/// <summary>
/// Gets the string representation of this reference
/// </summary>
/// <returns></returns>
public override string ToString()
{
return ProjectPath;
}
}
}