Last active
November 26, 2015 11:55
-
-
Save createdbyx/e9eeb4ff666acb551ddf to your computer and use it in GitHub Desktop.
removed reliance on Linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <copyright> | |
// Copyright (c) 2012 Codefarts | |
// All rights reserved. | |
// [email protected] | |
// http://www.codefarts.com | |
// </copyright> | |
namespace Codefarts.IdManager | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
/// <summary> | |
/// Provides a manager for unique integer based id's. | |
/// </summary> | |
public class IdManager | |
{ | |
#region Fields | |
/// <summary> | |
/// Holds the list of generated and registered id's. | |
/// </summary> | |
private readonly List<long> idList = new List<long>(); | |
/// <summary> | |
/// Used for thread locking. | |
/// </summary> | |
private readonly object lockObject = new object(); | |
/// <summary> | |
/// Holds a list of pre-generated id values. | |
/// </summary> | |
private List<long> availableList = new List<long>(); | |
/// <summary> | |
/// The maximum id value that is currently registered. | |
/// </summary> | |
private long maxValue; | |
#endregion | |
#region Public Properties | |
/// <summary> | |
/// Gets the registered ids. | |
/// </summary> | |
public ReadOnlyCollection<long> RegisteredIds | |
{ | |
get | |
{ | |
return new ReadOnlyCollection<long>(this.idList); | |
} | |
} | |
#endregion | |
#region Public Methods and Operators | |
/// <summary>Gets a unique new identifier.</summary> | |
/// <returns>The value of the newly allocated identifier.</returns> | |
public long NewId() | |
{ | |
lock (this.lockObject) | |
{ | |
if (this.availableList.Count > 0) | |
{ | |
var item = this.availableList[0]; | |
this.availableList.RemoveAt(0); | |
this.idList.Add(item); | |
return item; | |
} | |
var max = this.idList.Count == 0 ? 1 : this.maxValue + 1; | |
this.idList.Add(max); | |
this.maxValue = max; | |
return max; | |
} | |
} | |
/// <summary> | |
/// Registers the identifier. | |
/// </summary> | |
/// <param name="id"> | |
/// A unique identifier. | |
/// </param> | |
public void RegisterId(long id) | |
{ | |
lock (this.lockObject) | |
{ | |
if (this.idList.Contains(id)) | |
{ | |
throw new Exception("Id already registered!"); | |
} | |
this.idList.Add(id); | |
this.maxValue = id > this.maxValue ? id : this.maxValue; | |
this.availableList.Remove(id); | |
} | |
} | |
/// <summary> | |
/// Tries to registers the identifier and returns true if successfull. | |
/// </summary> | |
/// <param name="id"> | |
/// A unique identifier value. | |
/// </param> | |
public bool TryRegisterId(long id) | |
{ | |
lock (this.lockObject) | |
{ | |
if (this.idList.Contains(id)) | |
{ | |
return false; | |
} | |
this.idList.Add(id); | |
this.maxValue = id > this.maxValue ? id : this.maxValue; | |
this.availableList.Remove(id); | |
return true; | |
} | |
} | |
/// <summary> | |
/// Registers the identifier. | |
/// </summary> | |
/// <param name="ids"> | |
/// The id's to be registered. | |
/// </param> | |
public void RegisterId(IEnumerable<long> ids) | |
{ | |
foreach (var id in ids) | |
{ | |
this.RegisterId(id); | |
} | |
} | |
/// <summary> | |
/// Resets the id manager by removing all registered id's and clearing the buffered available list. | |
/// </summary> | |
public void Reset() | |
{ | |
lock (this.lockObject) | |
{ | |
this.availableList.Clear(); | |
this.idList.Clear(); | |
this.maxValue = 0; | |
} | |
} | |
#endregion | |
/// <summary>Releases the identifier.</summary> | |
/// <param name="id">The identifier.</param> | |
public void ReleaseId(long id) | |
{ | |
lock (this.lockObject) | |
{ | |
this.idList.Remove(id); | |
this.availableList.Add(id); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment