// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Mvc.Razor
{
///
/// Result of lookups.
///
public struct ViewLocationCacheResult : IEquatable
{
///
/// Initializes a new instance of
/// for a view that was successfully found at the specified location.
///
/// The view location.
/// Locations that were searched
/// in addition to .
public ViewLocationCacheResult(
[NotNull] string foundLocation,
[NotNull] IEnumerable searchedLocations)
: this (searchedLocations)
{
ViewLocation = foundLocation;
SearchedLocations = searchedLocations;
IsFoundResult = true;
}
///
/// Initializes a new instance of for a
/// failed view lookup.
///
/// Locations that were searched.
public ViewLocationCacheResult([NotNull] IEnumerable searchedLocations)
{
SearchedLocations = searchedLocations;
ViewLocation = null;
IsFoundResult = false;
}
///
/// A that represents a cache miss.
///
public static readonly ViewLocationCacheResult None = new ViewLocationCacheResult(Enumerable.Empty());
///
/// The location the view was found.
///
/// This is available if is true.
public string ViewLocation { get; }
///
/// The sequence of locations that were searched.
///
///
/// When is true this includes all paths that were search prior to finding
/// a view at . When is false, this includes
/// all search paths.
///
public IEnumerable SearchedLocations { get; }
///
/// Gets a value that indicates whether the view was successfully found.
///
public bool IsFoundResult { get; }
///
public bool Equals(ViewLocationCacheResult other)
{
if (IsFoundResult != other.IsFoundResult)
{
return false;
}
if (IsFoundResult)
{
return string.Equals(ViewLocation, other.ViewLocation, StringComparison.Ordinal);
}
else
{
if (SearchedLocations == other.SearchedLocations)
{
return true;
}
if (SearchedLocations == null || other.SearchedLocations == null)
{
return false;
}
return Enumerable.SequenceEqual(SearchedLocations, other.SearchedLocations, StringComparer.Ordinal);
}
}
///
public override int GetHashCode()
{
var hashCodeCombiner = HashCodeCombiner.Start()
.Add(IsFoundResult);
if (IsFoundResult)
{
hashCodeCombiner.Add(ViewLocation, StringComparer.Ordinal);
}
else if (SearchedLocations != null)
{
foreach (var location in SearchedLocations)
{
hashCodeCombiner.Add(location, StringComparer.Ordinal);
}
}
return hashCodeCombiner;
}
}
}