/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Kafka.Client.Cluster
{
using System;
using System.Globalization;
///
/// Represents broker partition
///
internal class Partition : IComparable
{
///
/// Factory method that instantiates object based on configuration given as string
///
///
/// The partition info.
///
///
/// Instantiated object
///
public static Partition ParseFrom(string partition)
{
var pieces = partition.Split('-');
if (pieces.Length != 2)
{
throw new ArgumentException("Expected name in the form x-y");
}
return new Partition(int.Parse(pieces[0], CultureInfo.InvariantCulture), int.Parse(pieces[1], CultureInfo.InvariantCulture));
}
///
/// Initializes a new instance of the class.
///
///
/// The broker ID.
///
///
/// The partition ID.
///
public Partition(int brokerId, int partId)
{
this.BrokerId = brokerId;
this.PartId = partId;
}
///
/// Gets the broker Dd.
///
public int BrokerId { get; private set; }
///
/// Gets the partition ID.
///
public int PartId { get; private set; }
///
/// Gets broker name as concatanate broker ID and partition ID
///
public string Name
{
get { return this.BrokerId + "-" + this.PartId; }
}
///
/// Compares current object with another object of type
///
///
/// The other object.
///
///
/// 0 if equals, positive number if greater and negative otherwise
///
public int CompareTo(Partition other)
{
if (this.BrokerId == other.BrokerId)
{
return this.PartId - other.PartId;
}
return this.BrokerId - other.BrokerId;
}
///
/// Gets string representation of current object
///
///
/// String that represents current object
///
public override string ToString()
{
return "(" + this.BrokerId + "," + this.PartId + ")";
}
///
/// Determines whether a given object is equal to the current object
///
///
/// The other object.
///
///
/// Equality of given and current objects
///
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
var other = obj as Partition;
if (other == null)
{
return false;
}
return this.BrokerId == other.BrokerId && this.PartId == other.PartId;
}
///
/// Gets hash code of current object
///
///
/// Hash code
///
public override int GetHashCode()
{
return (31 * (17 + this.BrokerId)) + this.PartId;
}
}
}