forked from paulirwin/JavaToCSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
105 lines (91 loc) · 3.38 KB
/
Copy pathExtensions.cs
File metadata and controls
105 lines (91 loc) · 3.38 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
using System.Diagnostics.CodeAnalysis;
using java.util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using JavaAst = com.github.javaparser.ast;
namespace JavaToCSharp;
public static class Extensions
{
/// <param name="iterable">The java Iterable to be enumerated.</param>
extension(java.lang.Iterable iterable)
{
/// <summary>
/// Converts a Java Iterable to a .NET IEnumerable<T> and filters the elements of type T.
/// </summary>
/// <typeparam name="T">Type of items to be returned.</typeparam>
/// <returns>A filtered enumeration of items of type T</returns>
public IEnumerable<T> OfType<T>()
{
var iterator = iterable.iterator();
while (iterator.hasNext())
{
if (iterator.next() is T item)
{
yield return item;
}
}
}
}
extension(java.util.List? list)
{
public List<T>? ToList<T>()
{
if (list is null)
{
return null;
}
var newList = new List<T>();
for (int i = 0; i < list.size(); i++)
{
newList.Add((T)list.get(i));
}
return newList;
}
}
extension(java.util.EnumSet values)
{
public bool HasFlag<T>(T flag) => values.contains(flag);
}
extension(CompilationUnitSyntax syntax)
{
public CompilationUnitSyntax WithPackageFileComments(ConversionContext context,
JavaAst.CompilationUnit compilationUnit,
JavaAst.PackageDeclaration? packageDeclaration)
=> context.Options.IncludeComments
? CommentsHelper.AddPackageComments(syntax, compilationUnit, packageDeclaration)
: syntax;
}
extension(Optional optional)
{
public T? FromOptional<T>()
where T : class
=> optional.isPresent()
? optional.get() as T ?? throw new InvalidOperationException($"Optional did not convert to {typeof(T)}")
: null;
public T FromRequiredOptional<T>()
where T : class
=> optional.isPresent()
? optional.get() as T ?? throw new InvalidOperationException($"Optional did not convert to {typeof(T)}")
: throw new InvalidOperationException("Required optional did not have a value");
}
extension(JavaAst.NodeList nodeList)
{
public ISet<JavaAst.Modifier.Keyword> ToModifierKeywordSet()
=> nodeList.ToList<JavaAst.Modifier>()?.Select(i => i.getKeyword()).ToHashSet() ?? [];
}
extension<TSyntax>(TSyntax syntax) where TSyntax : SyntaxNode
{
public TSyntax WithLeadingNewLines(int count = 1)
=> syntax.WithLeadingTrivia(Enumerable.Repeat(Whitespace.NewLine, count));
public TSyntax WithTrailingNewLines(int count = 1)
=> syntax.WithTrailingTrivia(Enumerable.Repeat(Whitespace.NewLine, count));
}
extension<TSyntax>(TSyntax? syntax) where TSyntax : SyntaxNode
{
[return: NotNullIfNotNull(nameof(node))]
public TSyntax? WithJavaComments(ConversionContext context, JavaAst.Node? node)
=> context.Options.IncludeComments
? CommentsHelper.AddCommentsTrivias(syntax, node)
: syntax;
}
}