Skip to content

Commit 62f5677

Browse files
committed
replace boxing Hashtable with generic Dictionary<TKey, TValue>
1 parent 7f33b6e commit 62f5677

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/AdornerPresentationContext.cs

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//
66
// Description:
7-
// AdornerPresentationContext knows that annotation comonents are wrapped
7+
// AdornerPresentationContext knows that annotation components are wrapped
88
// in an AnnotationAdorner and hosted in the AdornerLayer. Note, implementation-wise
99
// a new PresentationContext is created for every annotation component. Executing
1010
// operations on a presentation context for a different annotation component
@@ -24,7 +24,7 @@
2424
namespace MS.Internal.Annotations.Component
2525
{
2626
/// <summary>
27-
/// AdornerPresentationContext knows that annotation comonents are wrapped in an AnnotationAdorner and hosted in the AdornerLayer.
27+
/// AdornerPresentationContext knows that annotation components are wrapped in an AnnotationAdorner and hosted in the AdornerLayer.
2828
/// Note, implementation-wise a new PresentationContext is created for every annotation component. Executing operations on a presentation context
2929
/// for a different annotation component (located in the same adorner layer) works, but is slower than using the presentation context stored in the
3030
/// annotation component.
@@ -108,13 +108,13 @@ internal static void SetTypeZLevel(Type type, int level)
108108

109109
Invariant.Assert(type != null, "type is null");
110110

111-
if (_ZLevel.ContainsKey(type))
111+
if (s_ZLevel.ContainsKey(type))
112112
{
113-
_ZLevel[type] = level;
113+
s_ZLevel[type] = level;
114114
}
115115
else
116116
{
117-
_ZLevel.Add(type, level);
117+
s_ZLevel.Add(type, level);
118118
}
119119
}
120120

@@ -128,9 +128,9 @@ internal static void SetTypeZLevel(Type type, int level)
128128
/// <param name="max">max Z-order value for this level</param>
129129
internal static void SetZLevelRange(int level, int min, int max)
130130
{
131-
if (_ZRanges[level] == null)
131+
if (s_ZRanges.ContainsKey(level) == false)
132132
{
133-
_ZRanges.Add(level, new ZRange(min, max));
133+
s_ZRanges.Add(level, new ZRange(min, max));
134134
}
135135
}
136136

@@ -512,12 +512,9 @@ private AnnotationAdorner GetAnnotationAdorner(IAnnotationComponent component)
512512
/// <returns>ZLevel</returns>
513513
private static int GetComponentLevel(IAnnotationComponent component)
514514
{
515-
int level = 0;
516515
Type type = component.GetType();
517-
if (_ZLevel.ContainsKey(type))
518-
level = (int)_ZLevel[type];
519516

520-
return level;
517+
return s_ZLevel.TryGetValue(type, out int value) ? value : 0;
521518
}
522519

523520
/// <summary>
@@ -531,18 +528,21 @@ private static int GetComponentLevel(IAnnotationComponent component)
531528
private static int ComponentToAdorner(int zOrder, int level)
532529
{
533530
int res = zOrder;
534-
ZRange range = (ZRange)_ZRanges[level];
535-
if (range != null)
531+
532+
if (s_ZRanges.TryGetValue(level, out ZRange range))
536533
{
537534
//adjust the Z-order (shift it with the minimal value for this range)
538535
//that way the component does need to know the range for its type that is
539536
// set by the application. It always sets the z-order as it starts from 0
540537
res += range.Min;
538+
541539
if (res < range.Min)
542540
res = range.Min;
541+
543542
if (res > range.Max)
544543
res = range.Max;
545544
}
545+
546546
return res;
547547
}
548548

@@ -559,19 +559,17 @@ private static int ComponentToAdorner(int zOrder, int level)
559559
/// <summary>
560560
/// The adornerLayer which contains the annotation component. Basically what the presentation hides.
561561
/// </summary>
562-
private AdornerLayer _adornerLayer;
562+
private readonly AdornerLayer _adornerLayer;
563563

564564
/// <summary>
565-
/// The hashtable holds the priority level for each Component type as defined by the application
565+
/// The dictionary holds the priority level for each Component type as defined by the application
566566
/// </summary>
567-
private static Hashtable _ZLevel = new Hashtable();
567+
private static readonly Dictionary<Type, int> s_ZLevel = new();
568568

569569
/// <summary>
570570
/// The ZRanges for the ZLevels.
571571
/// </summary>
572-
private static Hashtable _ZRanges = new Hashtable();
573-
574-
572+
private static readonly Dictionary<int, ZRange> s_ZRanges = new();
575573

576574
#endregion Private Fields
577575

@@ -581,40 +579,28 @@ private static int ComponentToAdorner(int zOrder, int level)
581579
/// This is to control the relationships with TextSelection which lives in the same
582580
/// AdornerLayer. Will be removed when more flexible Z-ordering mechanism is available
583581
/// </summary>
584-
private class ZRange
582+
private sealed class ZRange
585583
{
586584
public ZRange(int min, int max)
587585
{
588-
//exchange values if needed
586+
// Swap values if needed
589587
if (min > max)
590588
{
591-
int temp = min;
589+
int num = min;
592590
min = max;
593-
max = temp;
591+
max = num;
594592
}
595-
_min = min;
596-
_max = max;
597-
}
598593

599-
public int Min
600-
{
601-
get
602-
{
603-
return _min;
604-
}
605-
}
606-
public int Max
607-
{
608-
get
609-
{
610-
return _max;
611-
}
594+
Min = min;
595+
Max = max;
612596
}
613597

614-
private int _min, _max;
598+
public int Min { get; }
599+
public int Max { get; }
600+
615601
}
616602

617-
#endregion Internal classes
603+
#endregion Private classes
618604
}
619605
}
620606

0 commit comments

Comments
 (0)