Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code for StringEquivalent and tests
- Loading branch information
Showing
10 changed files
with
713 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
|
||
namespace MiscUtils | ||
{ | ||
/// <summary> | ||
/// Ensure input parameters | ||
/// </summary> | ||
public static class Ensure | ||
{ | ||
public static void ArgumentNotNull([ValidatedNotNull] object value, string name) | ||
{ | ||
if (value != null) return; | ||
|
||
throw new ArgumentNullException(name); | ||
} | ||
|
||
public static void ArgumentNonNegative(int value, string name) | ||
{ | ||
if (value > -1) return; | ||
|
||
throw new ArgumentException("The argument must be non-negative", name); | ||
} | ||
|
||
public static void ArgumentNotNullOrEmptyString([ValidatedNotNull] string value, string name) | ||
{ | ||
ArgumentNotNull(value, name); | ||
if (!string.IsNullOrWhiteSpace(value)) return; | ||
|
||
throw new ArgumentException("String cannot be empty", name); | ||
} | ||
} | ||
|
||
// Tells CodeAnalysis that this argument is being validated | ||
// to be not null. Great for guard methods. | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
internal sealed class ValidatedNotNullAttribute : Attribute | ||
{ | ||
// Internal so it doesn't conflict with the potential billion | ||
// other implementations of this attribute. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProductVersion>8.0.30703</ProductVersion> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{52D341CE-48CE-4C0C-98E7-9935B300EB2C}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>MiscUtils</RootNamespace> | ||
<AssemblyName>MiscUtils</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Ensure.cs" /> | ||
<Compile Include="PathString.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="StringEquivalent.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
|
||
namespace MiscUtils | ||
{ | ||
[Serializable] | ||
public class PathString : StringEquivalent<PathString> | ||
{ | ||
public PathString(string pathString) : base(NormalizePath(pathString)) | ||
{ | ||
} | ||
|
||
protected PathString(SerializationInfo info, StreamingContext context) : base(info) | ||
{ | ||
} | ||
|
||
// So the XmlSerializer can create this. | ||
protected PathString() | ||
{ | ||
} | ||
|
||
public override PathString Combine(string path) | ||
{ | ||
Ensure.ArgumentNotNull(path, "path"); | ||
|
||
if (path.StartsWith(@"\", StringComparison.Ordinal)) | ||
{ | ||
path = path.Substring(1); | ||
} | ||
|
||
try | ||
{ | ||
return Path.Combine(Value, path); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
return AppendPath(Value, path); | ||
} | ||
} | ||
|
||
private static string AppendPath(string source, string addition) | ||
{ | ||
if (source.Last() != Path.DirectorySeparatorChar) source += "/"; | ||
if (addition.First() == '/') addition = addition.Substring(1); | ||
return source + addition; | ||
} | ||
|
||
public static implicit operator PathString(string value) | ||
{ | ||
return value == null ? null : new PathString(value); | ||
} | ||
|
||
public static implicit operator string(PathString path) | ||
{ | ||
return path == null ? null : path.Value; | ||
} | ||
|
||
private static string NormalizePath(string path) | ||
{ | ||
return path == null ? null : path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); | ||
} | ||
|
||
public override bool Equals(string other) | ||
{ | ||
return other != null && Value.Equals(other, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("MiscUtils")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("MiscUtils")] | ||
[assembly: AssemblyCopyright("Copyright © 2012")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("18bb8ed0-6334-4eb0-b796-c5c12dae80a8")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
using System.Xml.Serialization; | ||
|
||
namespace MiscUtils | ||
{ | ||
[Serializable] | ||
public abstract class StringEquivalent<T> : ISerializable, IXmlSerializable where T : StringEquivalent<T> | ||
{ | ||
protected string Value; | ||
|
||
protected StringEquivalent(string value) | ||
{ | ||
Ensure.ArgumentNotNull(value, "value"); | ||
Value = value; | ||
} | ||
|
||
protected StringEquivalent() | ||
{ | ||
} | ||
|
||
public abstract T Combine(string addition); | ||
|
||
public static T operator +(StringEquivalent<T> a, string b) | ||
{ | ||
return a.Combine(b); | ||
} | ||
|
||
public static bool operator ==(StringEquivalent<T> a, StringEquivalent<T> b) | ||
{ | ||
// If both are null, or both are same instance, return true. | ||
if (ReferenceEquals(a, b)) | ||
{ | ||
return true; | ||
} | ||
|
||
// If one is null, but not both, return false. | ||
if (((object)a == null) || ((object)b == null)) | ||
{ | ||
return false; | ||
} | ||
|
||
// Return true if the fields match: | ||
return a.Value.Equals(b.Value, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public static bool operator !=(StringEquivalent<T> a, StringEquivalent<T> b) | ||
{ | ||
return !(a == b); | ||
} | ||
|
||
public override bool Equals(Object obj) | ||
{ | ||
return obj != null && Equals(obj as T) || Equals(obj as string); | ||
} | ||
|
||
public bool Equals(T stringEquivalent) | ||
{ | ||
return this == stringEquivalent; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return (Value ?? "").GetHashCode(); | ||
} | ||
|
||
public virtual bool Equals(string other) | ||
{ | ||
return other != null && Value == other; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return Value; | ||
} | ||
|
||
protected StringEquivalent(SerializationInfo info) : this(info.GetValue("Value", typeof (string)) as string) | ||
{ | ||
} | ||
|
||
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
info.AddValue("Value", Value); | ||
} | ||
|
||
public XmlSchema GetSchema() | ||
{ | ||
return null; | ||
} | ||
|
||
public void ReadXml(XmlReader reader) | ||
{ | ||
Value = reader.ReadString(); | ||
} | ||
|
||
public void WriteXml(XmlWriter writer) | ||
{ | ||
writer.WriteString(Value); | ||
} | ||
} | ||
} |
Oops, something went wrong.