/// <summary> /// Converts an NTS attributes table to an attributes collection. /// </summary> public static IAttributeCollection ToAttributesCollection(this IAttributesTable table) { if (table == null) { return(null); } var attributes = new AttributeCollection(); var name = table.GetNames(); var values = table.GetValues(); for (var i = 0; i < name.Length; i++) { var value = values[i]; if (value == null) { attributes.AddOrReplace(name[i], string.Empty); } else { attributes.AddOrReplace(name[i], value.ToInvariantString()); } } return(attributes); }
private static void AddAttributes(List <uint> tags, Dictionary <string, uint> keys, Dictionary <Tile.Value, uint> values, IAttributesTable attributes) { if (attributes == null || attributes.Count == 0) { return; } var aKeys = attributes.GetNames(); var aValues = attributes.GetValues(); for (var a = 0; a < aKeys.Length; a++) { var key = aKeys[a]; if (string.IsNullOrEmpty(key)) { continue; } var tileValue = ToTileValue(aValues[a]); if (tileValue == null) { continue; } tags.Add(keys.AddOrGet(key)); tags.Add(values.AddOrGet(tileValue)); } }
/// <summary> /// Returns true if the attribute table contains the given attribute and it's value. /// </summary> public static bool Contains(this IAttributesTable attributes, string key, object value) { if (attributes == null) { throw new ArgumentNullException("attributes"); } var names = attributes.GetNames(); for (var i = 0; i < names.Length; i++) { if (names[i].Equals(key)) { if (attributes.GetValues()[i] != null) { return(attributes.GetValues()[i].ToInvariantString().Equals(value)); } } } return(false); }
/// <summary> /// Returns true if the given table contains the given attribute with the given value. /// </summary> public static bool Contains(this IAttributesTable table, string name, object value) { var names = table.GetNames(); for (var i = 0; i < names.Length; i++) { if (names[i] == name) { return(value.Equals(table.GetValues()[i])); } } return(false); }
/// <summary> /// Tries to get a value from the attribute table. /// </summary> public static bool TryGetValue(this IAttributesTable table, string name, out object value) { var names = table.GetNames(); for (var i = 0; i < names.Length; i++) { if (names[i] == name) { value = table.GetValues()[i]; return(true); } } value = null; return(false); }
/// <summary> /// Returns the first element that satisfies the given predicate or throw an exception with the given message if nothing is found. /// </summary> public static object FirstOrException(this IAttributesTable attributes, Func <string, bool> isFirst, string message) { if (attributes == null) { throw new ArgumentNullException("attributes"); } var names = attributes.GetNames(); for (var i = 0; i < names.Length; i++) { if (isFirst(names[i])) { return(attributes.GetValues()[i]); } } throw new Exception(message); }
private static void AddAttributes(List <uint> tags, Dictionary <string, uint> keys, Dictionary <string, uint> values, IAttributesTable attributes) { if (attributes == null) { return; } var aKeys = attributes.GetNames(); var aValues = attributes.GetValues(); for (var a = 0; a < aKeys.Length; a++) { var key = aKeys[a]; var value = aValues[a]; tags.Add(keys.AddOrGet(key)); tags.Add(values.AddOrGet(value?.ToString())); } }
/// <summary> /// Tries to get the value for the given key. /// </summary> public static bool TryGetValue(this IAttributesTable attributes, string key, out object value) { if (attributes == null) { throw new ArgumentNullException("attributes"); } var names = attributes.GetNames(); for (var i = 0; i < names.Length; i++) { if (names[i].Equals(key)) { value = attributes.GetValues()[i]; return(true); } } value = null; return(false); }
/// <summary> /// Tries to get a value as a string from the attribute table. /// </summary> public static bool TryGetValueAsString(this IAttributesTable table, string name, out string value) { var names = table.GetNames(); for (var i = 0; i < names.Length; i++) { if (names[i] == name) { var objValue = table.GetValues()[i]; if (objValue == null) { value = null; } else { value = objValue.ToInvariantString(); } return(true); } } value = null; return(false); }