2 // Copyright (c) Microsoft Corporation. All rights reserved.
5 namespace Microsoft.Activities.Presentation.Xaml
7 using System.Collections.Generic;
9 class ViewStateIdManager
11 readonly char separatorChar = '_';
12 Dictionary<string, int> prefixToIntMap = new Dictionary<string, int>();
14 public void UpdateMap(string id)
16 int separatorLocation = id.LastIndexOf('_');
18 // If the separator is not found or if the separator is the first or last character
19 // in the id then use id value itself as the prefix.
20 if (separatorLocation == -1 || separatorLocation == 0 || separatorLocation == id.Length - 1)
22 this.prefixToIntMap[id] = 0;
26 string[] idParts = new string[2];
27 idParts[0] = id.Substring(0, separatorLocation);
28 idParts[1] = id.Substring(separatorLocation + 1, id.Length - (separatorLocation + 1));
31 if (int.TryParse(idParts[1], out suffix))
34 if (this.prefixToIntMap.TryGetValue(idParts[0], out oldValue))
36 if (suffix > oldValue)
38 this.prefixToIntMap[idParts[0]] = suffix;
43 this.prefixToIntMap[idParts[0]] = suffix;
48 this.prefixToIntMap[id] = 0;
53 public string GetNewId(string prefix)
56 this.prefixToIntMap.TryGetValue(prefix, out suffix);
58 while (suffix == int.MaxValue)
60 prefix = prefix + this.separatorChar + suffix;
61 this.prefixToIntMap.TryGetValue(prefix, out suffix);
64 this.prefixToIntMap[prefix] = ++suffix;
65 return prefix + this.separatorChar + suffix;