Index: Src/GoogleApis/Apis/Util/Utilities.cs |
=================================================================== |
--- a/Src/GoogleApis/Apis/Util/Utilities.cs |
+++ b/Src/GoogleApis/Apis/Util/Utilities.cs |
@@ -15,12 +15,11 @@ |
*/ |
using System; |
-using System.Collections; |
using System.Collections.Generic; |
using System.Collections.ObjectModel; |
using System.Linq; |
using System.Reflection; |
-using Google.Apis.Discovery; |
+using System.Text.RegularExpressions; |
namespace Google.Apis.Util |
{ |
@@ -29,76 +28,13 @@ |
/// </summary> |
public static class Utilities |
{ |
- /// <summary> |
- /// Fetches an element from a dictionary in a safe way, returning <c>default(value)</c> if there is no value |
- /// present. |
- /// </summary> |
- public static TValue GetValueAsNull<TKey, TValue>(this IDictionary<TKey, TValue> data, TKey key) |
+ /// <summary> Returns the version of the Core library.</summary> |
+ public static string GetLibraryVersion() |
{ |
- return GetValue(data, key, () => default(TValue)); |
+ return Regex.Match(typeof(Utilities).Assembly.FullName, "Version=([\\d\\.]+)").Groups[1].ToString(); |
} |
- /// <summary> |
- /// Fetches an element from a dictionary in a safe way, |
- /// returning a value specified by the input func if there is no value present. |
- /// </summary> |
- public static TReturnValue GetValue<TKey, TValue, TReturnValue>(this IDictionary<TKey, TValue> data, TKey key, |
- Func<TReturnValue> defaultFunc) where TReturnValue : TValue |
- { |
- key.ThrowIfNull("key"); |
- defaultFunc.ThrowIfNull("defaultFunc"); |
- TValue result; |
- if (!data.TryGetValue(key, out result) || !(result is TReturnValue)) |
- { |
- return defaultFunc(); |
- } |
- return (TReturnValue)result; |
- } |
- |
- |
- private static readonly IEnumerable EmptyList = new List<object>().AsReadOnly(); |
- /// <summary> |
- /// If the enumerable is <code>null</code>, returns empty list. Otherwise returns the input list. |
- /// </summary> |
- public static IEnumerable<T> NullToEmpty<T>(this IEnumerable<T> data) |
- { |
- // casting the enumerable to T is ok, because it's an empty list |
- return data ?? EmptyList.Cast<T>(); |
- } |
- |
- private static readonly IDictionary<object, object> EmptyDictionary = |
- new Dictionary<object, object>().AsReadOnly(); |
- /// <summary> |
- /// If the dictionary is <code>null</code>, returns empty dictionary. Otherwise returns the input dictionary. |
- /// </summary> |
- public static IDictionary<TKey, TValue> NullToEmpty<TKey, TValue>( |
- this IDictionary<TKey, TValue> data) |
- { |
- // casting is ok here, because it's an empty dictionary |
- return data ?? EmptyDictionary.ToDictionary(k => (TKey)k.Key, k => (TValue)k.Value); |
- } |
- |
- /// <summary> |
- /// If key exists in data and is an IEnumerable will return each element converted to a string |
- /// with ToString, or null. If key does not exist in data, is null or not an IEnumerable we will |
- /// return the empty list. |
- /// </summary> |
- public static IEnumerable<string> GetValueAsStringListOrEmpty<TKey, TValue>( |
- this IDictionary<TKey, TValue> data, TKey key) |
- { |
- data.ThrowIfNull("data"); |
- IEnumerable value = GetValueAsNull(data, key) as IEnumerable; |
- if (value == null) |
- { |
- yield break; |
- } |
- foreach (object obj in value) |
- { |
- yield return obj != null ? obj.ToString() : null; |
- } |
- } |
- |
- /// <summary>Extension method on object, which throws a ArgumentNullException if obj is null</summary> |
+ /// <summary> Throws an <c>ArgumentNullException</c> if the object is null.</summary> |
public static void ThrowIfNull(this object obj, string paramName) |
{ |
if (obj == null) |
@@ -107,108 +43,43 @@ |
} |
} |
- /// <summary> |
- /// Throws an exception if the string is null or empty |
- /// </summary> |
+ /// <summary> Throws an <c>ArgumentException</c> if the string is <c>null</c> or empty.</summary> |
public static void ThrowIfNullOrEmpty(this string str, string paramName) |
{ |
- str.ThrowIfNull(paramName); |
- if (str.Length == 0) |
+ if (string.IsNullOrEmpty(str)) |
{ |
throw new ArgumentException("Parameter was empty", paramName); |
} |
} |
- /// <summary> |
- /// Throws the collection is null or empty |
- /// </summary> |
- public static void ThrowIfNullOrEmpty<T>(this ICollection<T> coll, string paramName) |
- { |
- coll.ThrowIfNull(paramName); |
- if (coll.Count == 0) |
- { |
- throw new ArgumentException("Parameter was empty", paramName); |
- } |
- } |
- |
- /// <summary> |
- /// Formats the specified string if format arguments are specified, or returns the unmodified string otherwise. |
- /// </summary> |
- public static string FormatString(this string message, params object[] formatArgs) |
- { |
- if (message == null) |
- { |
- return null; |
- } |
- return formatArgs.Length > 0 ? string.Format(message, formatArgs) : message; |
- } |
- |
- /// <summary> Returns a readonly variant of the given dictionary. </summary> |
+ /// <summary> Returns a read-only variant of the given dictionary. </summary> |
public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dict) |
{ |
dict.ThrowIfNull("dict"); |
return new ReadOnlyDictionary<TKey, TValue>(dict); |
} |
- /// <summary> Returns a readonly variant of the given list. </summary> |
+ /// <summary> Returns a read-only variant of the given list. </summary> |
public static ReadOnlyCollection<T> AsReadOnly<T>(this IList<T> list) |
{ |
list.ThrowIfNull("list"); |
return new ReadOnlyCollection<T>(list); |
} |
- /// <summary> |
- /// Returns true when the string is null or empty |
- /// </summary> |
- public static bool IsNullOrEmpty(this string str) |
- { |
- return string.IsNullOrEmpty(str); |
- } |
- |
- /// <summary> |
- /// Returns true when the string is NOT null or empty |
- /// </summary> |
- public static bool IsNotNullOrEmpty(this string str) |
- { |
- return !string.IsNullOrEmpty(str); |
- } |
- |
- /// <summary> |
- /// Returns true when the collection is NOT null or empty |
- /// </summary> |
- public static bool IsNotNullOrEmpty<T>(this ICollection<T> coll) |
- { |
- return coll != null && coll.Count > 0; |
- } |
- |
- /// <summary> |
- /// Returns true when the enumerable is null or empty. |
- /// </summary> |
+ /// <summary> Returns <c>true</c> when the enumerable is <c>null</c> or empty. </summary> |
public static bool IsNullOrEmpty<T>(this IEnumerable<T> coll) |
{ |
return coll == null || coll.Count() == 0; |
} |
- /// <summary> |
- /// Returns true when the collection is null or empty |
- /// </summary> |
- public static bool IsNullOrEmpty<T>(this ICollection<T> coll) |
- { |
- return coll == null || coll.Count == 0; |
- } |
- |
- /// <summary> |
- /// Returns the first matching custom attribute (or null) of the specified member |
- /// </summary> |
+ /// <summary> Returns the first matching custom attribute (or <c>null</c>) of the specified member. </summary> |
public static T GetCustomAttribute<T>(this MemberInfo info) where T : Attribute |
{ |
object[] results = info.GetCustomAttributes(typeof(T), false); |
return results.Length == 0 ? null : (T)results[0]; |
} |
- /// <summary> |
- /// Returns the defined string value of an enum value |
- /// </summary> |
+ /// <summary> Returns the defined string value of an enum value. </summary> |
public static string GetStringValue(this Enum value) |
{ |
FieldInfo entry = value.GetType().GetField(value.ToString()); |
@@ -227,31 +98,6 @@ |
} |
/// <summary> |
- /// Retrieves the underlying type if the specified type is a Nullable, or the type itself otherwise. |
- /// </summary> |
- public static Type GetNonNullableType(Type type) |
- { |
- if (!type.IsGenericType || !typeof(Nullable<>).IsAssignableFrom(type.GetGenericTypeDefinition())) |
- { |
- return type; // Not a Nullable. |
- } |
- |
- // First: Find the Nullable type. |
- while (!(type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))) |
- { |
- type = type.BaseType; |
- |
- if (type == null) |
- { |
- return null; |
- } |
- } |
- |
- // Return the type which is encapsulated by the Nullable. |
- return type.GetGenericArguments()[0]; |
- } |
- |
- /// <summary> |
/// Tries to convert the specified object to a string. Uses custom type converters if available. |
/// Returns null for a null object. |
/// </summary> |
@@ -273,39 +119,5 @@ |
return o.ToString(); |
} |
- |
- /// <summary> |
- /// Parses the specified array as an ascii string. |
- /// </summary> |
- public static string GetAsciiString(IEnumerable<byte> bytes) |
- { |
- return bytes.Aggregate("", (str, b) => str + (char)b); |
- } |
- |
- /// <summary> |
- /// Please don't use this unless absolutely nessasery. |
- /// Returns if the current runtime is Mono, this is used to work around different behavior in the runtime. |
- /// </summary> |
- public static bool IsMonoRuntime() |
- { |
- return Type.GetType("Mono.Runtime") != null; |
- } |
- |
- /// <summary> |
- /// Creates a new <code>IParameter</code> by the specified values. |
- /// </summary> |
- public static IParameter CreateRuntimeParameter(string name, bool isRequired, string parameterType, |
- string defaultValue, string pattern, IEnumerable<string> enumValues) |
- { |
- return new RuntimeParameter |
- { |
- Name = name, |
- IsRequired = isRequired, |
- ParameterType = parameterType, |
- DefaultValue = defaultValue, |
- Pattern = pattern, |
- EnumValues = enumValues |
- }; |
- } |
} |
} |