You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by li...@apache.org on 2022/03/08 07:13:48 UTC

[rocketmq-client-csharp] branch develop updated: Polish code (#15)

This is an automated email from the ASF dual-hosted git repository.

lizhanhui pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-csharp.git


The following commit(s) were added to refs/heads/develop by this push:
     new a654b4d  Polish code (#15)
a654b4d is described below

commit a654b4d8b7d9d9cbf0968b0d3c5b69d4e3bde235
Author: aaron ai <ya...@alibaba-inc.com>
AuthorDate: Tue Mar 8 15:13:14 2022 +0800

    Polish code (#15)
---
 rocketmq-client-csharp/Address.cs       |  24 +++-----
 rocketmq-client-csharp/AddressScheme.cs |  13 ++--
 rocketmq-client-csharp/Broker.cs        | 104 ++++++++++++++++++++------------
 rocketmq-client-csharp/Client.cs        |   4 +-
 rocketmq-client-csharp/ClientManager.cs |   8 +--
 rocketmq-client-csharp/Partition.cs     | 100 +++++++++++++++++-------------
 rocketmq-client-csharp/Producer.cs      |   2 +-
 rocketmq-client-csharp/Topic.cs         |  74 ++++++++++++++---------
 8 files changed, 195 insertions(+), 134 deletions(-)

diff --git a/rocketmq-client-csharp/Address.cs b/rocketmq-client-csharp/Address.cs
index a51a0ba..bd862b5 100644
--- a/rocketmq-client-csharp/Address.cs
+++ b/rocketmq-client-csharp/Address.cs
@@ -14,22 +14,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-namespace Org.Apache.Rocketmq {
-    public class Address {
-        public Address(string host, int port) {
-            this.host = host;
-            this.port = port;
-        }
-
-        private string host;
-        public string Host {
-            get { return host; }
-        }
 
-        private int port;
-        public int Port {
-            get { return port; }
+namespace Org.Apache.Rocketmq
+{
+    public class Address
+    {
+        public Address(string host, int port)
+        {
+            Host = host;
+            Port = port;
         }
 
+        public string Host { get; }
+        public int Port { get; }
     }
 }
\ No newline at end of file
diff --git a/rocketmq-client-csharp/AddressScheme.cs b/rocketmq-client-csharp/AddressScheme.cs
index e3cb4c3..822ee4a 100644
--- a/rocketmq-client-csharp/AddressScheme.cs
+++ b/rocketmq-client-csharp/AddressScheme.cs
@@ -14,10 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-namespace Org.Apache.Rocketmq {
-    public enum AddressScheme {
-        IPv4,
-        IPv6,
-        DOMAIN_NAME,
+
+namespace Org.Apache.Rocketmq
+{
+    public enum AddressScheme
+    {
+        Ipv4,
+        Ipv6,
+        DomainName,
     }
 }
\ No newline at end of file
diff --git a/rocketmq-client-csharp/Broker.cs b/rocketmq-client-csharp/Broker.cs
index a3b448d..b0c11d1 100644
--- a/rocketmq-client-csharp/Broker.cs
+++ b/rocketmq-client-csharp/Broker.cs
@@ -17,61 +17,89 @@
 
 using System;
 
-namespace Org.Apache.Rocketmq {
-    public class Broker : IComparable<Broker>, IEquatable<Broker> {
-
-        public Broker(string name, int id, ServiceAddress address) {
-            this.name = name;
-            this.id = id;
-            this.address = address;
-        }
-
-        private string name;
-        public string Name {
-            get { return name; }
+namespace Org.Apache.Rocketmq
+{
+    public class Broker : IComparable<Broker>, IEquatable<Broker>
+    {
+        public Broker(string name, int id, ServiceAddress address)
+        {
+            Name = name;
+            Id = id;
+            Address = address;
         }
 
-        private int id;
-        public int Id {
-            get { return id; }
-        }
+        public string Name { get; }
+        public int Id { get; }
+        public ServiceAddress Address { get; }
 
-        private ServiceAddress address;
-        public ServiceAddress Address {
-            get { return address; }
-        }
-
-        /**
-         * Context aware primary target URL.
-         */
-        public string targetUrl()
+        /// <summary>
+        /// Calculate context aware primary target URL.
+        /// </summary>
+        /// <returns>Context aware primary target URL.</returns>
+        public string TargetUrl()
         {
-            var addr = address.Addresses[0];
-            return string.Format("https://{0}:{1}", addr.Host, addr.Port);
+            var address = Address.Addresses[0];
+            return $"https://{address.Host}:{address.Port}";
         }
 
-        public int CompareTo(Broker other) {
-            if (0 != name.CompareTo(other.name)) {
-                return name.CompareTo(other.name);
+        /// <summary>
+        /// Judge whether equals to other or not, ignore <see cref="Address"/> on purpose.
+        /// </summary>
+        public bool Equals(Broker other)
+        {
+            if (ReferenceEquals(null, other))
+            {
+                return false;
             }
 
-            return id.CompareTo(other.id);
-        }
+            if (ReferenceEquals(this, other))
+            {
+                return true;
+            }
 
-        public bool Equals(Broker other) {
-            return name.Equals(other.name) && id.Equals(other.id);
+            return Name == other.Name && Id == other.Id;
         }
 
-        public override bool Equals(Object other) {
-            if (!(other is Broker)) {
+        public override bool Equals(object obj)
+        {
+            if (ReferenceEquals(null, obj) || obj.GetType() != GetType())
+            {
                 return false;
             }
-            return Equals(other as Broker);
+
+            if (ReferenceEquals(this, obj))
+            {
+                return true;
+            }
+
+            return Equals((Broker)obj);
         }
 
+        /// <summary>
+        /// Return the hash code, ignore <see cref="Address"/> on purpose.
+        /// </summary>
         public override int GetHashCode()
         {
-            return HashCode.Combine(name, id);
+            return HashCode.Combine(Name, Id);
+        }
+
+        /// <summary>
+        /// Compare with other, ignore <see cref="Address"/> on purpose.
+        /// </summary>
+        public int CompareTo(Broker other)
+        {
+            if (ReferenceEquals(null, other))
+            {
+                return -1;
+            }
+
+            var compareTo = String.CompareOrdinal(Name, other.Name);
+            if (0 == compareTo)
+            {
+                compareTo = Id.CompareTo(other.Id);
+            }
+
+            return compareTo;
         }
     }
 }
\ No newline at end of file
diff --git a/rocketmq-client-csharp/Client.cs b/rocketmq-client-csharp/Client.cs
index 04a4ed3..29774e7 100644
--- a/rocketmq-client-csharp/Client.cs
+++ b/rocketmq-client-csharp/Client.cs
@@ -78,7 +78,7 @@ namespace Org.Apache.Rocketmq
             {
                 foreach (var partition in item.Value.Partitions)
                 {
-                    string target = partition.Broker.targetUrl();
+                    string target = partition.Broker.TargetUrl();
                     if (acceptor(target))
                     {
                         return target;
@@ -98,7 +98,7 @@ namespace Org.Apache.Rocketmq
             {
                 foreach (var partition in item.Value.Partitions)
                 {
-                    string endpoint = partition.Broker.targetUrl();
+                    string endpoint = partition.Broker.TargetUrl();
                     if (!endpoints.Contains(endpoint))
                     {
                         endpoints.Add(endpoint);
diff --git a/rocketmq-client-csharp/ClientManager.cs b/rocketmq-client-csharp/ClientManager.cs
index 8640d93..960e556 100644
--- a/rocketmq-client-csharp/ClientManager.cs
+++ b/rocketmq-client-csharp/ClientManager.cs
@@ -116,22 +116,22 @@ namespace Org.Apache.Rocketmq
                     }
                 }
 
-                AddressScheme scheme = AddressScheme.IPv4;
+                AddressScheme scheme = AddressScheme.Ipv4;
                 switch (partition.Broker.Endpoints.Scheme)
                 {
                     case rmq::AddressScheme.Ipv4:
                     {
-                        scheme = AddressScheme.IPv4;
+                        scheme = AddressScheme.Ipv4;
                         break;
                     }
                     case rmq::AddressScheme.Ipv6:
                     {
-                        scheme = AddressScheme.IPv6;
+                        scheme = AddressScheme.Ipv6;
                         break;
                     }
                     case rmq::AddressScheme.DomainName:
                     {
-                        scheme = AddressScheme.DOMAIN_NAME;
+                        scheme = AddressScheme.DomainName;
                         break;
                     }
                 }
diff --git a/rocketmq-client-csharp/Partition.cs b/rocketmq-client-csharp/Partition.cs
index 5c9d0bb..5c2e748 100644
--- a/rocketmq-client-csharp/Partition.cs
+++ b/rocketmq-client-csharp/Partition.cs
@@ -14,72 +14,88 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-using System;
 
-namespace Org.Apache.Rocketmq {
-    
-    public class Partition : IEquatable<Partition>, IComparable<Partition> {
+using System;
 
-        public Partition(Topic topic, Broker broker, int id, Permission permission) {
-            this.topic = topic;
-            this.broker = broker;
-            this.id = id;
-            this.permission = permission;
+namespace Org.Apache.Rocketmq
+{
+    public class Partition : IEquatable<Partition>, IComparable<Partition>
+    {
+        public Partition(Topic topic, Broker broker, int id, Permission permission)
+        {
+            Topic = topic;
+            Broker = broker;
+            Id = id;
+            Permission = permission;
         }
 
-        private Topic topic;
-        public Topic Topic{
-            get { return topic; }
-        }
+        public Topic Topic { get; }
+        public Broker Broker { get; }
+        public int Id { get; }
 
-        private Broker broker;
-        public Broker Broker {
-            get { return broker; }
-        }
+        public Permission Permission { get; }
 
-        private int id;
-        public int Id {
-            get { return id; }
-        }
+        public bool Equals(Partition other)
+        {
+            if (ReferenceEquals(null, other))
+            {
+                return false;
+            }
 
-        Permission permission;
-        public Permission Permission {
-            get { return permission; }
-        }
+            if (ReferenceEquals(this, other))
+            {
+                return true;
+            }
 
-        public bool Equals(Partition other) {
-            return topic.Equals(other.topic) 
-                && broker.Equals(other.broker) 
-                && id.Equals(other.id) 
-                && permission == other.permission;
+            return Equals(Topic, other.Topic) && Equals(Broker, other.Broker) && Id == other.Id &&
+                   Permission == other.Permission;
         }
 
-        public override bool Equals(Object other) {
-            if (!(other is Partition)) {
+        public override bool Equals(object obj)
+        {
+            if (ReferenceEquals(null, obj) || obj.GetType() != GetType())
+            {
                 return false;
             }
-            return Equals(other);
+
+            if (ReferenceEquals(this, obj))
+            {
+                return true;
+            }
+
+            return Equals((Partition)obj);
         }
 
+
         public override int GetHashCode()
         {
-            return HashCode.Combine(topic, broker, id, permission);
+            return HashCode.Combine(Topic, Broker, Id, (int)Permission);
         }
 
-        public int CompareTo(Partition other) {
-            if (0 != topic.CompareTo(other.topic)) {
-                return topic.CompareTo(other.topic);
+        public int CompareTo(Partition other)
+        {
+            if (ReferenceEquals(null, other))
+            {
+                return -1;
+            }
+
+            var compareTo = Topic.CompareTo(other.Topic);
+            if (0 == compareTo)
+            {
+                compareTo = Broker.CompareTo(other.Broker);
             }
 
-            if (0 != broker.CompareTo(other.broker)) {
-                return broker.CompareTo(other.broker);
+            if (0 == compareTo)
+            {
+                compareTo = Id.CompareTo(other.Id);
             }
 
-            if (0 != id.CompareTo(other.id)) {
-                return id.CompareTo(other.id);
+            if (0 == compareTo)
+            {
+                compareTo = Permission.CompareTo(other.Permission);
             }
 
-            return permission.CompareTo(other.permission);
+            return compareTo;
         }
     }
 }
\ No newline at end of file
diff --git a/rocketmq-client-csharp/Producer.cs b/rocketmq-client-csharp/Producer.cs
index e2e023d..4e750b0 100644
--- a/rocketmq-client-csharp/Producer.cs
+++ b/rocketmq-client-csharp/Producer.cs
@@ -100,7 +100,7 @@ namespace Org.Apache.Rocketmq
             List<Partition> candidates = publishLB.select(message.MaxAttemptTimes);
             foreach (var partition in candidates)
             {
-                targets.Add(partition.Broker.targetUrl());
+                targets.Add(partition.Broker.TargetUrl());
             }
 
             var metadata = new Metadata();
diff --git a/rocketmq-client-csharp/Topic.cs b/rocketmq-client-csharp/Topic.cs
index 5abc5cb..f1ae453 100644
--- a/rocketmq-client-csharp/Topic.cs
+++ b/rocketmq-client-csharp/Topic.cs
@@ -17,50 +17,68 @@
 
 using System;
 
-namespace Org.Apache.Rocketmq {
-    public class Topic : IComparable<Topic>, IEquatable<Topic> {
-        public Topic(string resourceNamespace, string name) {
-            this.resourceNamespace = resourceNamespace;
-            this.name = name;
+namespace Org.Apache.Rocketmq
+{
+    public class Topic : IComparable<Topic>, IEquatable<Topic>
+    {
+        public Topic(string resourceNamespace, string name)
+        {
+            ResourceNamespace = resourceNamespace;
+            Name = name;
         }
 
-        private readonly string resourceNamespace;
-        public string ResourceNamespace {
-            get { return resourceNamespace; }
-        }
+        public string ResourceNamespace { get; }
+        public string Name { get; }
 
-        private string name;
-        public string Name {
-            get { return name; }
-        }
-
-        public int CompareTo(Topic other) {
-            if (0 != resourceNamespace.CompareTo(other.resourceNamespace)) {
-                return resourceNamespace.CompareTo(other.resourceNamespace);
+        public bool Equals(Topic other)
+        {
+            if (ReferenceEquals(null, other))
+            {
+                return false;
             }
 
-            if (0 != name.CompareTo(other.name)) {
-                return name.CompareTo(other.name);
+            if (ReferenceEquals(this, other))
+            {
+                return true;
             }
 
-            return 0;
-        }
-
-        public bool Equals(Topic other) {
-            return resourceNamespace.Equals(other.resourceNamespace) && name.Equals(other.name);
+            return ResourceNamespace == other.ResourceNamespace && Name == other.Name;
         }
 
-        public override bool Equals(Object other) {
-            if (!(other is Topic)) {
+        public override bool Equals(object obj)
+        {
+            if (ReferenceEquals(null, obj) || obj.GetType() != GetType())
+            {
                 return false;
             }
-            return Equals(other as Topic);
+
+            if (ReferenceEquals(this, obj))
+            {
+                return true;
+            }
+
+            return Equals((Topic)obj);
         }
 
         public override int GetHashCode()
         {
-            return HashCode.Combine(resourceNamespace, name);
+            return HashCode.Combine(ResourceNamespace, Name);
         }
 
+        public int CompareTo(Topic other)
+        {
+            if (ReferenceEquals(null, other))
+            {
+                return -1;
+            }
+
+            var compareTo = String.CompareOrdinal(ResourceNamespace, other.ResourceNamespace);
+            if (0 == compareTo)
+            {
+                compareTo = String.CompareOrdinal(Name, other.Name);
+            }
+
+            return compareTo;
+        }
     }
 }
\ No newline at end of file