-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Class_CollectionUtil.xpp
89 lines (74 loc) · 2.41 KB
/
Class_CollectionUtil.xpp
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
//
// https://github.com/mazzy-ax/SysUtil
//
//
// TODO work in progress
//
// Util-класс реализует только статические методы.
// не содержит внутри никаких значений.
// не имеет смысла создавать экземпляр этого класса.
//
abstract class CollectionUtil
{
public static anytype as(anytype value)
{
return EnumeratorUtil::asEnumerable(value);
}
public static boolean empty(anytype collection)
{
Enumerator e = CollectionUtil::getEnumerator(collection);
if( e && e.moveNext() )
{
return false;
}
return true;
}
// возвращает объект, у которого можно вызвать метод moveNext или Null
// возвращает null, если параметр не является ни коллекцией, ни энумератором
//
// используйте EnumeratorUtil::getEnumerator() чтобы получить энумератор для любого объекта
public static Enumerator getEnumerator(anytype collection)
{
Enumerator e = EnumeratorUtil::newFromCollection(collection);
return e;
}
public static boolean in(anytype value, anytype collection)
{
Enumerator e = EnumeratorUtil::getEnumerator(collection);
while( e && e.moveNext() )
{
if( value == e.current() )
{
return true;
}
}
return false;
}
// возвращает true для значения, для которого можно получить enumerator
public static boolean is(anytype value)
{
return EnumeratorUtil::isEnumerable(value);
}
// первый элемент имеет индекс 1
public static anytype item(anytype collection, int idx)
{
Enumerator e = EnumeratorUtil::getEnumerator(collection);
int i;
anytype ret;
while( idx > 0 && e && e.moveNext() )
{
i++;
if( i == idx )
{
ret = e.current();
break;
}
}
return ret;
}
public static boolean notIn(anytype value, anytype valueOrCollection)
{
boolean in = CollectionUtil::in(value, valueOrCollection);
return !in;
}
}