Skip to content

Commit 4409202

Browse files
committed
Add collection extensions so we can convert to dictionary/etc.
ActiveProfiles is actually a collection of strings that then need to be looked up in the resulting map.
1 parent 2cc2bc6 commit 4409202

12 files changed

+462
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
3+
using java.util;
4+
5+
namespace IKVM.Maven.Sdk.Tasks.Extensions
6+
{
7+
8+
public static class CollectionExtensions
9+
{
10+
11+
/// <summary>
12+
/// Returns a <see cref="ICollection{T}"/> that wraps the specified <see cref="Collection"/>.
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
/// <param name="collection"></param>
16+
/// <returns></returns>
17+
public static ICollection<T> AsCollection<T>(this Collection collection) => collection switch
18+
{
19+
ICollection<T> c => c,
20+
List l => l.AsList<T>(),
21+
Set s => s.AsSet<T>(),
22+
Collection c => new CollectionWrapper<T>(c),
23+
};
24+
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using java.util;
5+
6+
namespace IKVM.Maven.Sdk.Tasks.Extensions
7+
{
8+
9+
class CollectionWrapper<T> : IterableWrapper<T>, ICollection<T>
10+
{
11+
12+
readonly Collection collection;
13+
14+
/// <summary>
15+
/// Initializes a new instance.
16+
/// </summary>
17+
/// <param name="collection"></param>
18+
/// <exception cref="ArgumentNullException"></exception>
19+
public CollectionWrapper(Collection collection) : base(collection)
20+
{
21+
this.collection = collection ?? throw new ArgumentNullException(nameof(collection));
22+
}
23+
24+
public int Count => collection.size();
25+
26+
public bool IsReadOnly => false;
27+
28+
public void Add(T item) => collection.add(item);
29+
30+
public void Clear() => collection.clear();
31+
32+
public bool Contains(T item) => collection.contains(item);
33+
34+
public void CopyTo(T[] array, int arrayIndex)
35+
{
36+
foreach (var entry in this)
37+
array[arrayIndex++] = entry;
38+
}
39+
40+
public bool Remove(T item)
41+
{
42+
return collection.remove(item);
43+
}
44+
45+
}
46+
47+
}

src/IKVM.Maven.Sdk.Tasks/Extensions/IterableExtensions.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Collections.Generic;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
24

35
using java.lang;
6+
using java.util;
47

58
namespace IKVM.Maven.Sdk.Tasks.Extensions
69
{
@@ -17,12 +20,13 @@ public static class IterableExtensions
1720
/// <typeparam name="T"></typeparam>
1821
/// <param name="iterable"></param>
1922
/// <returns></returns>
20-
public static IEnumerable<T> AsEnumerable<T>(this Iterable iterable)
23+
public static IEnumerable<T> AsEnumerable<T>(this Iterable iterable) => iterable switch
2124
{
22-
var e = iterable.iterator().AsEnumerator<T>();
23-
while (e.MoveNext())
24-
yield return e.Current;
25-
}
25+
IEnumerable<T> i => i,
26+
IEnumerable i => i.Cast<T>(),
27+
Collection i => i.AsCollection<T>(),
28+
Iterable i => new IterableWrapper<T>(i),
29+
};
2630

2731
}
2832

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
using java.lang;
6+
7+
namespace IKVM.Maven.Sdk.Tasks.Extensions
8+
{
9+
10+
/// <summary>
11+
/// Wraps an <see cref="Iterable"/> as a <see cref="IEnumerable"/>.
12+
/// </summary>
13+
class IterableWrapper<T> : IEnumerable<T>
14+
{
15+
16+
readonly Iterable iterable;
17+
18+
/// <summary>
19+
/// Initializes a new instance.
20+
/// </summary>
21+
/// <param name="iterable"></param>
22+
/// <exception cref="ArgumentNullException"></exception>
23+
public IterableWrapper(Iterable iterable)
24+
{
25+
this.iterable = iterable ?? throw new ArgumentNullException(nameof(iterable));
26+
}
27+
28+
/// <summary>
29+
/// Returns an enumerator that wraps the given iterable.
30+
/// </summary>
31+
/// <returns></returns>
32+
public IEnumerator<T> GetEnumerator()
33+
{
34+
return new IteratorWrapper<T>(iterable.iterator());
35+
}
36+
37+
IEnumerator IEnumerable.GetEnumerator()
38+
{
39+
return GetEnumerator();
40+
}
41+
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
3+
using java.util;
4+
5+
namespace IKVM.Maven.Sdk.Tasks.Extensions
6+
{
7+
8+
public static class ListExtensions
9+
{
10+
11+
/// <summary>
12+
/// Returns a <see cref="IList{T}"/> that wraps the specified <see cref="List"/>.
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
/// <param name="list"></param>
16+
/// <returns></returns>
17+
public static IList<T> AsList<T>(this List list) => list switch
18+
{
19+
IList<T> c => c,
20+
List l => new ListWrapper<T>(l),
21+
};
22+
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using java.util;
5+
6+
namespace IKVM.Maven.Sdk.Tasks.Extensions
7+
{
8+
9+
class ListWrapper<T> : CollectionWrapper<T>, IList<T>
10+
{
11+
12+
readonly List list;
13+
14+
/// <summary>
15+
/// Initializes a new instance.
16+
/// </summary>
17+
/// <param name="list"></param>
18+
/// <exception cref="ArgumentNullException"></exception>
19+
public ListWrapper(List list) :
20+
base(list)
21+
{
22+
this.list = list ?? throw new ArgumentNullException(nameof(list));
23+
}
24+
25+
public T this[int index]
26+
{
27+
get => (T)list.get(index);
28+
set => list.set(index, value);
29+
}
30+
31+
public int IndexOf(T item) => list.indexOf(item);
32+
33+
public void Insert(int index, T item) => list.add(index, item);
34+
35+
public void RemoveAt(int index) => list.remove(index);
36+
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
3+
using java.util;
4+
5+
namespace IKVM.Maven.Sdk.Tasks.Extensions
6+
{
7+
8+
public static class MapExtensions
9+
{
10+
11+
/// <summary>
12+
/// Returns a <see cref="IDictionary{TKey, TValue}"/> implementation for the given <see cref="Map"/>.
13+
/// </summary>
14+
/// <typeparam name="TKey"></typeparam>
15+
/// <typeparam name="TValue"></typeparam>
16+
/// <param name="map"></param>
17+
/// <returns></returns>
18+
public static IDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this Map map) => map switch
19+
{
20+
IDictionary<TKey, TValue> i => i,
21+
Map i => new MapWrapper<TKey, TValue>(i),
22+
};
23+
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
using java.util;
7+
8+
namespace IKVM.Maven.Sdk.Tasks.Extensions
9+
{
10+
11+
class MapWrapper<TKey, TValue> : IDictionary<TKey, TValue>
12+
{
13+
14+
readonly Map map;
15+
16+
/// <summary>
17+
/// Initializes a new instance.
18+
/// </summary>
19+
/// <param name="map"></param>
20+
/// <exception cref="ArgumentNullException"></exception>
21+
public MapWrapper(Map map)
22+
{
23+
this.map = map ?? throw new ArgumentNullException(nameof(map));
24+
}
25+
26+
public TValue this[TKey key]
27+
{
28+
get => (TValue)map.get(key);
29+
set => map.put(key, value);
30+
}
31+
32+
public ICollection<TKey> Keys => map.keySet().AsCollection<TKey>();
33+
34+
public ICollection<TValue> Values => map.values().AsCollection<TValue>();
35+
36+
public int Count => map.size();
37+
38+
public bool IsReadOnly => false;
39+
40+
public void Add(TKey key, TValue value) => map.put(key, value);
41+
42+
public void Add(KeyValuePair<TKey, TValue> item) => map.put(item.Key, item.Value);
43+
44+
public void Clear() => map.clear();
45+
46+
public bool Contains(KeyValuePair<TKey, TValue> item)
47+
{
48+
return map.containsKey(item.Key) && map.get(item.Key).Equals(item.Value);
49+
}
50+
51+
public bool ContainsKey(TKey key)
52+
{
53+
return map.containsKey(key);
54+
}
55+
56+
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
57+
{
58+
foreach (var entry in this)
59+
array[arrayIndex++] = entry;
60+
}
61+
62+
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
63+
{
64+
return map.entrySet().AsEnumerable<Map.Entry>().Select(i => new KeyValuePair<TKey, TValue>((TKey)i.getKey(), (TValue)i.getValue())).GetEnumerator();
65+
}
66+
67+
public bool Remove(TKey key)
68+
{
69+
if (map.containsKey(key))
70+
{
71+
map.remove(key);
72+
return true;
73+
}
74+
75+
return false;
76+
}
77+
78+
public bool Remove(KeyValuePair<TKey, TValue> item)
79+
{
80+
return map.remove(item.Key, item.Value);
81+
}
82+
83+
public bool TryGetValue(TKey key, out TValue value)
84+
{
85+
if (map.containsKey(key))
86+
{
87+
value = (TValue)map.get(key);
88+
return true;
89+
}
90+
91+
value = default;
92+
return false;
93+
}
94+
95+
IEnumerator IEnumerable.GetEnumerator()
96+
{
97+
return GetEnumerator();
98+
}
99+
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
3+
using java.util;
4+
5+
namespace IKVM.Maven.Sdk.Tasks.Extensions
6+
{
7+
8+
public static class SetExtensions
9+
{
10+
11+
/// <summary>
12+
/// Returns a <see cref="ISet{T}"/> that wraps the specified <see cref="Set"/>.
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
/// <param name="set"></param>
16+
/// <returns></returns>
17+
public static ISet<T> AsSet<T>(this Set set) => set switch
18+
{
19+
ISet<T> i => i,
20+
TreeSet i => new TreeSetWrapper<T>(i),
21+
Set i => new SetWrapper<T>(i),
22+
};
23+
24+
}
25+
26+
}

0 commit comments

Comments
 (0)