-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathHistoryCollection.cs
More file actions
126 lines (106 loc) · 3.15 KB
/
PathHistoryCollection.cs
File metadata and controls
126 lines (106 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace AutoUpdate {
/// <summary>
/// 最近访问的历史记录
/// </summary>
public class PathHistoryCollection : ICollection<string> {
private List<string> _items = new List<string>();
public int Count {
get {
return Math.Min(_items.Count,_maxCapacity);
}
}
public bool IsReadOnly {
get { return false; }
}
private int _maxCapacity = 10;
public int MaxCapacity {
get { return _maxCapacity; }
set {
if (_maxCapacity != value) {
if (value < 0) {
value = 0;
}
_maxCapacity = value;
}
}
}
public void Add(string path) {
if (string.IsNullOrEmpty(path)) {
return;
}
path = RepairPath(path);
var index = IndexOf(path);
if (index < 0) {
_items.Insert(0, path);
if (_items.Count > _maxCapacity) {
_items.RemoveAt(_items.Count - 1);
}
}
else {
//重复
if (index > 0) {
_items.RemoveAt(index);
_items.Insert(0, path);
}
}
}
public string this[int index] {
get { return _items[index]; }
}
private string RepairPath(string path) {
path = path.Trim();
if (path.EndsWith(@"\")) {
path = path.Substring(0, path.Length - 1);
}
return path;
}
private int IndexOf(string path) {
for (int i = 0; i < _items.Count; i++) {
if (string.Equals(_items[i],path, StringComparison.CurrentCultureIgnoreCase)) {
return i;
}
}
return -1;
}
public void Clear() {
_items.Clear();
}
public bool Contains(string item) {
return IndexOf(item) >= 0;
}
public void CopyTo(string[] array, int arrayIndex) {
_items.CopyTo(array, arrayIndex);
}
public IEnumerator<string> GetEnumerator() {
var count = Math.Min(_items.Count, _maxCapacity);
for (int i = 0; i < count; i++) {
yield return _items[i];
}
}
public bool Remove(string item) {
var index = IndexOf(item);
if (index >= 0) {
_items.RemoveAt(index);
return true;
}
else {
return false;
}
}
IEnumerator IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}
public string[] ToArray() {
string[] array = new string[Count];
int i = 0;
foreach (var item in this) {
array[i++] = item;
}
return array;
}
}
}