IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
JavaPackageName.cs
Go to the documentation of this file.
1using System;
2
4
6{
7
11 readonly struct JavaPackageName
12 {
13
14 public static JavaPackageName Empty => new(ReadOnlyMemory<char>.Empty);
15
16 public static implicit operator string(JavaPackageName packageName) => packageName.ToString();
17 public static implicit operator JavaPackageName(string packageName) => new(packageName);
18
19 public static implicit operator ReadOnlyMemory<char>(JavaPackageName packageName) => packageName.value;
20 public static implicit operator JavaPackageName(ReadOnlyMemory<char> packageName) => new(packageName);
21
22 public static bool operator ==(JavaPackageName a, JavaPackageName b) => a.value.Span.Equals(b.value.Span, StringComparison.Ordinal);
23 public static bool operator !=(JavaPackageName a, JavaPackageName b) => a.value.Span.Equals(b.value.Span, StringComparison.Ordinal) == false;
24
25 readonly ReadOnlyMemory<char> value;
26
31 public JavaPackageName(string qualifiedName) :
32 this(qualifiedName.AsMemory())
33 {
34 if (qualifiedName is null)
35 throw new ArgumentNullException(nameof(qualifiedName));
36 }
37
42 public JavaPackageName(ReadOnlyMemory<char> qualifiedName)
43 {
44 this.value = qualifiedName;
45 }
46
52 public JavaPackageName(JavaPackageName parent, ReadOnlyMemory<char> name) :
53 this(parent.IsEmpty ? name : $"{parent}.{name}".AsMemory())
54 {
55
56 }
57
63 public JavaPackageName(JavaPackageName parent, string name) :
64 this(parent, name.AsMemory())
65 {
66 if (name is null)
67 throw new ArgumentNullException(nameof(name));
68 }
69
73 public ReadOnlyMemory<char> Value => value;
74
78 public bool IsEmpty => value.IsEmpty;
79
84
90 {
91 if (IsEmpty)
93
94 var index = value.Span.LastIndexOf('.');
95 return index != -1 ? new JavaSimplePackageName(value.Slice(index + 1)) : JavaSimplePackageName.Empty;
96 }
97
102
108 {
109 var i = value.Span.LastIndexOf('.');
110 return i != -1 ? new JavaPackageName(value.Slice(0, i)) : Empty;
111 }
112
118 public bool IsMemberOf(JavaPackageName packageName) => Parent == packageName;
119
125 public bool IsChildOf(JavaPackageName other)
126 {
127 // the empty package can never be a child of another package
128 if (value.IsEmpty)
129 return false;
130
131 // but everything is a child of it
132 if (other.IsEmpty)
133 return true;
134
135 // we're going to enumerate each package element in the other package and test we can advance through that in the target
136 var o = new ReadOnlySpanSeparatorEnumerator(other.value.Span, '.');
137 var e = new ReadOnlySpanSeparatorEnumerator(value.Span, '.');
138 while (o.MoveNext())
139 {
140 // for each item in the other, we should be able to advance
141 if (e.MoveNext() == false)
142 return false;
143
144 // each item must match
145 if (e.Current.Equals(o.Current, StringComparison.Ordinal) == false)
146 return false;
147 }
148
149 // there must still be more elements
150 return e.MoveNext();
151 }
152
158 public bool Equals(JavaPackageName other) => value.Span.Equals(other.value.Span, StringComparison.Ordinal);
159
165 public override bool Equals(object obj) => false;
166
171 public override int GetHashCode() => value.Span.GetHashCodeExtension();
172
177 public override string ToString() => value.Span.ToString();
178
179 }
180
181}
Provides methods to parse a Java package name.
readonly ReadOnlyMemory< char > value
override int GetHashCode()
Gets the hash code of this package name.
JavaSimplePackageName GetSimpleName()
Gets the simple name.
JavaPackageName GetParent()
Gets the package name.
JavaPackageName(string qualifiedName)
Initializes a new instance.
override bool Equals(object obj)
Returns true if the two objects are equal.
bool IsEmpty
Returns true if this package name is empty.
bool IsChildOf(JavaPackageName other)
Returns true if the given package name is a child of the other package name.
bool Equals(JavaPackageName other)
Returns true if the two objects are equal.
JavaPackageName(JavaPackageName parent, string name)
Initializes a new instance.
override string ToString()
Returns a string instance of the package name.
JavaPackageName Parent
Gets the name of the parent package, or empty.
static bool operator!=(JavaPackageName a, JavaPackageName b)
JavaPackageName(ReadOnlyMemory< char > qualifiedName)
Initializes a new instance.
ReadOnlyMemory< char > Value
Gets the underlying value of this package name.
JavaPackageName(JavaPackageName parent, ReadOnlyMemory< char > name)
Initializes a new instance.
bool IsMemberOf(JavaPackageName packageName)
Returns true if this package name is a member of the given package name.
static bool operator==(JavaPackageName a, JavaPackageName b)
JavaSimplePackageName SimpleName
Gets the simple name.
Provides methods to parse a Java class simple name.