IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Util.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2008-2011 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26
27namespace IKVM.Reflection
28{
29
30 static class Util
31 {
32 internal static int[] Copy(int[] array)
33 {
34 if (array == null || array.Length == 0)
35 return Array.Empty<int>();
36
37 var copy = new int[array.Length];
38 Array.Copy(array, copy, array.Length);
39 return copy;
40 }
41
42 internal static Type[] Copy(Type[] array)
43 {
44 if (array == null || array.Length == 0)
45 return Type.EmptyTypes;
46
47 var copy = new Type[array.Length];
48 Array.Copy(array, copy, array.Length);
49 return copy;
50 }
51
52 internal static T[] ToArray<T, V>(List<V> list, T[] empty) where V : T
53 {
54 if (list == null || list.Count == 0)
55 return empty;
56
57 var array = new T[list.Count];
58 for (int i = 0; i < array.Length; i++)
59 array[i] = list[i];
60
61 return array;
62 }
63
64 internal static T[] ToArray<T>(IEnumerable<T> values)
65 {
66 return values == null ? Array.Empty<T>() : new List<T>(values).ToArray();
67 }
68
69 // note that an empty array matches a null reference
70 internal static bool ArrayEquals(Type[] t1, Type[] t2)
71 {
72 if (t1 == t2)
73 return true;
74
75 if (t1 == null)
76 return t2.Length == 0;
77 else if (t2 == null)
78 return t1.Length == 0;
79
80 if (t1.Length == t2.Length)
81 {
82 for (int i = 0; i < t1.Length; i++)
83 if (!TypeEquals(t1[i], t2[i]))
84 return false;
85
86 return true;
87 }
88
89 return false;
90 }
91
92 internal static bool TypeEquals(Type t1, Type t2)
93 {
94 if (t1 == t2)
95 return true;
96 if (t1 == null)
97 return false;
98
99 return t1.Equals(t2);
100 }
101
102 internal static int GetHashCode(Type[] types)
103 {
104 if (types == null)
105 return 0;
106
107 int h = 0;
108 foreach (var t in types)
109 {
110 if (t != null)
111 {
112 h *= 3;
113 h ^= t.GetHashCode();
114 }
115 }
116
117 return h;
118 }
119
120 internal static bool ArrayEquals(CustomModifiers[] m1, CustomModifiers[] m2)
121 {
122 if (m1 == null || m2 == null)
123 return m1 == m2;
124
125 if (m1.Length != m2.Length)
126 return false;
127
128 for (int i = 0; i < m1.Length; i++)
129 if (!m1[i].Equals(m2[i]))
130 return false;
131
132 return true;
133 }
134
135 internal static int GetHashCode(CustomModifiers[] mods)
136 {
137 int h = 0;
138 if (mods != null)
139 foreach (var mod in mods)
140 h ^= mod.GetHashCode();
141
142 return h;
143 }
144
145 internal static T NullSafeElementAt<T>(T[] array, int index)
146 {
147 return array == null ? default : array[index];
148 }
149
150 internal static int NullSafeLength<T>(T[] array)
151 {
152 return array == null ? 0 : array.Length;
153 }
154
155 }
156
157}