@@ -59,7 +59,7 @@ public int Compare(Address x, Address y)
59
59
/// </summary>
60
60
public static readonly Address AllSystems = new Address ( "akka" , "all-systems" ) ;
61
61
62
- private readonly Lazy < string > _toString ;
62
+ private string _toString ;
63
63
private readonly string _host ;
64
64
private readonly int ? _port ;
65
65
private readonly string _system ;
@@ -78,7 +78,7 @@ public Address(string protocol, string system, string host = null, int? port = n
78
78
_system = system ;
79
79
_host = host ? . ToLowerInvariant ( ) ;
80
80
_port = port ;
81
- _toString = CreateLazyToString ( ) ;
81
+ _toString = null ;
82
82
}
83
83
84
84
/// <summary>
@@ -114,19 +114,14 @@ public Address(string protocol, string system, string host = null, int? port = n
114
114
/// </summary>
115
115
public bool HasGlobalScope => ! string . IsNullOrEmpty ( Host ) ;
116
116
117
- private Lazy < string > CreateLazyToString ( )
117
+ private static string CreateLazyToString ( Address addr )
118
118
{
119
- return new Lazy < string > ( ( ) =>
120
- {
121
- var sb = new StringBuilder ( ) ;
122
- sb . AppendFormat ( "{0}://{1}" , Protocol , System ) ;
123
- if ( ! string . IsNullOrWhiteSpace ( Host ) )
124
- sb . AppendFormat ( "@{0}" , Host ) ;
125
- if ( Port . HasValue )
126
- sb . AppendFormat ( ":{0}" , Port . Value ) ;
127
-
128
- return sb . ToString ( ) ;
129
- } , true ) ;
119
+ if ( ! string . IsNullOrWhiteSpace ( addr . Host ) && addr . Port . HasValue )
120
+ return $ "{ addr . Protocol } ://{ addr . System } @{ addr . Host } :{ addr . Port } ";
121
+ if ( ! string . IsNullOrWhiteSpace ( addr . Host ) ) // host, but no port - rare case
122
+ return $ "{ addr . Protocol } ://{ addr . System } @{ addr . Host } ";
123
+
124
+ return $ "{ addr . Protocol } ://{ addr . System } ";
130
125
}
131
126
132
127
/// <summary>
@@ -140,7 +135,15 @@ public int CompareTo(Address other)
140
135
}
141
136
142
137
/// <inheritdoc/>
143
- public override string ToString ( ) => _toString . Value ;
138
+ public override string ToString ( )
139
+ {
140
+ if ( _toString == null )
141
+ {
142
+ _toString = CreateLazyToString ( this ) ;
143
+ }
144
+
145
+ return _toString ;
146
+ }
144
147
145
148
/// <inheritdoc/>
146
149
public bool Equals ( Address other )
@@ -279,7 +282,7 @@ public static Address Parse(string address)
279
282
if ( string . IsNullOrEmpty ( uri . UserInfo ) )
280
283
{
281
284
var systemName = uri . Host ;
282
-
285
+
283
286
return new Address ( protocol , systemName ) ;
284
287
}
285
288
else
0 commit comments