Skip to content

Instantly share code, notes, and snippets.

@niklaslundberg
Created March 31, 2015 08:29
Show Gist options
  • Save niklaslundberg/5d03c22b9226fcb8f3c9 to your computer and use it in GitHub Desktop.
Save niklaslundberg/5d03c22b9226fcb8f3c9 to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2015 Niklas Lundberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
public sealed class ContentType : IEquatable<ContentType>
{
public static readonly ContentType Csv = new ContentType("CSV", "text/csv", ".csv");
public static readonly ContentType Html = new ContentType("HTML", "text/html", ".html");
public static readonly ContentType Jpeg = new ContentType("JPEG", "image/jpeg", ".jpg");
public static readonly ContentType Css = new ContentType("CSS", "text/css", ".css");
public static readonly ContentType Json = new ContentType("JSON", "application/json", ".json");
static readonly IReadOnlyCollection<ContentType> AllContentTypes =
typeof (ContentType)
.GetFields()
.Where(field =>
field.IsPublic &&
field.IsStatic &&
field.FieldType == typeof (ContentType))
.Select(field => (ContentType) field.GetValue(null))
.SafeToReadOnlyCollection();
readonly string _contentType;
readonly string _displayName;
readonly string _fileExtension;
ContentType(string displayName, string contentType, string fileExtension)
{
_displayName = displayName;
_contentType = contentType;
_fileExtension = fileExtension;
}
public static IReadOnlyCollection<ContentType> All
{
get { return AllContentTypes; }
}
public string DisplayName
{
get { return _displayName; }
}
public string RegisteredContentType
{
get { return _contentType; }
}
public string FileExtension
{
get { return _fileExtension; }
}
public bool Equals(ContentType other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(_contentType, other._contentType);
}
public override string ToString()
{
return string.Format("{0} '{1}' '{2}'", DisplayName, RegisteredContentType, FileExtension);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is ContentType && Equals((ContentType) obj);
}
public override int GetHashCode()
{
return (_contentType != null ? _contentType.GetHashCode() : 0);
}
public static bool operator ==(ContentType left, ContentType right)
{
return Equals(left, right);
}
public static bool operator !=(ContentType left, ContentType right)
{
return !Equals(left, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment