/// <summary> /// Returns whether the property dictionary contains the given key and /// it is not null. /// </summary> /// <param name="key">The key to check.</param> /// <returns>Whether the given key maps to a non-null value in the /// dictionary.</returns> bool HaveNonNullKey(string key) { if (!RawProperties.ContainsKey(key)) { return(false); } return(RawProperties [key] != null); }
/// <summary> /// Returns a dictionary from parsing a value in the format /// string:number[,string:number,...]. If the value is unset (i.e. not /// contained in the dictionary), returns unsetDefault. If the value is /// empty (i.e. maps to null), returns emptyDefault. /// defaultValue is used if no number is specified after a colon; if /// defaultValue is null, this method returns null. /// </summary> IDictionary <string, int> ParseStringNumberPairs(string key, IDictionary <string, int> unsetDefault, IDictionary <string, int> emptyDefault, int?defaultValue) { if (!RawProperties.ContainsKey(key)) { return(unsetDefault); } var valstr = RawProperties [key]; if (valstr == null) { return(emptyDefault); } var valmap = new Dictionary <string, int>(); // comma splits the specs foreach (string limit in valstr.Split(',')) { // colon splits keys and value var split = limit.Split(':'); if (split.Length != 2) { // invalid spec; don't trust the whole thing return(null); } var chantypes = split [0]; var valuestr = split [1]; int value; if (valuestr == string.Empty) { if (defaultValue.HasValue) { value = defaultValue.Value; } return(null); } else if (!int.TryParse(valuestr, out value)) { // invalid integer; don't trust the whole thing return(null); } valmap [chantypes] = value; } return(valmap); }
/// <summary> /// Returns a numeric value. If the value is unset (i.e. not contained /// in the dictionary), returns unsetDefault. If the value is empty /// (i.e. maps to null), returns emptyDefault. On parse failure, returns /// null. Otherwise, returns the parsed value. /// </summary> int?ParseNumber(string key, int?unsetDefault, int?emptyDefault) { if (!RawProperties.ContainsKey(key)) { return(unsetDefault); } var numstr = RawProperties [key]; if (numstr == null) { return(emptyDefault); } int num; if (!int.TryParse(numstr, out num)) { return(null); } return(num); }