|
using System;
using System.Collections.Generic;
using System.Text;
namespace AutoDiagramer
{
#region NameSpaces CLASS
/// <summary>
/// Provides methods for dealing with the allocation of individual
/// <see cref="DrawableClass">DrawableClass</see> objects to
/// namespaces. And also the retrieval of only those
/// <see cref="DrawableClass">DrawableClass</see> objects
/// This class implements a thread safe singleton pattern
/// </summary>
public sealed class NameSpaces
{
#region Instance Fields
//instance fields
private Dictionary<string, List<DrawableClass>> _assObjects = new Dictionary<string, List<DrawableClass>>();
private static volatile AutoDiagramer.NameSpaces instance = null;
private static object syncRoot = new object();
#endregion
#region Contrsuctor
/// <summary>
/// Creates a new NameSpaces object
/// </summary>
private NameSpaces()
{
}
#endregion
#region Public Methods
/// <summary>
/// Clear the collection of <see cref="DrawableClass">DrawableClass</see> objects
/// </summary>
public void Clear()
{
_assObjects.Clear();
}
/// <summary>
/// public property that can get the single instance of the
/// AutoDiagramer.NameSpaces object. This method uses an object lock
/// to provide synchorinzation between threads, such that it will provide
/// a thread safe AutoDiagramer.NameSpaces singleton object
/// </summary>
/// <returns>AutoDiagramer.NameSpaces which is the single instance of the
/// AutoDiagramer.NameSpaces object</returns>
public static AutoDiagramer.NameSpaces Instance
{
//get the instance
get
{
// only create a new instance if one doesn't already exist.
if (instance == null)
{
// use this lock to ensure that only one thread is accessing
// this block of code at once.
lock (syncRoot)
{
//check to see if instance exists, if not create a new one
if (instance == null)
instance = new AutoDiagramer.NameSpaces();
}
}
// return instance where it was just created or already existed.
return instance;
}
}
/// <summary>
/// Returns a list of <see cref="DrawableClass">DrawableClass</see>
/// objects which represents the list of <see cref="DrawableClass">
/// DrawableClasses</see> requested within the specified namspace
/// </summary>
/// <param name="nspace">the specified namspace</param>
/// <param name="requiredClasses">list of <see cref="DrawableClass">
/// DrawableClasses</see> requested</param>
/// <returns>A list of <see cref="DrawableClass">DrawableClass</see>
/// objects which represents the list of <see cref="DrawableClass">
/// DrawableClasses</see> requested within the specified namspace</returns>
public List<DrawableClass> getDrawableClassesForDiagram(string nspace, List<string> requiredClasses)
{
//create a new lists
List<DrawableClass> outputDrawClasses = new List<DrawableClass>();
//holding list for output list returned by TryGetValue of Dictionary
List<DrawableClass> currentDrawClasses = null;
_assObjects.TryGetValue(nspace, out currentDrawClasses);
if (currentDrawClasses != null)
{
//loop through required DrawableClass list
foreach (string s in requiredClasses)
{
//if its in the list that belongs to the requested namespace
//include it in the outputDrawClasses list
foreach (DrawableClass dc in currentDrawClasses)
{
if (dc != null)
{
if (dc.Name.ToLower().Equals(s.ToLower()))
{
outputDrawClasses.Add(dc);
}
}
}
}
return outputDrawClasses;
}
return null;
}
/// <summary>
/// Takes a nspace string and a <see cref="DrawableClass">DrawableClass
/// </see>class to add to the
/// namspace. The namespace and all its associated <see cref="DrawableClass">
/// DrawableClass </see>class objects are stored as a generic Dictionary
/// </summary>
/// <param name="nspace">the new namespace to add</param>
/// <param name="drawClass">The <see cref="DrawableClass">DrawableClass
/// </see>class to add to the namspace</param>
public void addNameSpace(string nspace, DrawableClass drawClass)
{
//check to see if the namaspace already exists
if (_assObjects.ContainsKey(nspace))
{
//if it does get the list of DrawableClass for it, and add the new DrawableClass
//and then add the whole lot back into the Dictionary (should overwrite the current entry)
List<DrawableClass> currentDrawClasses = null;
_assObjects.TryGetValue(nspace, out currentDrawClasses);
if (currentDrawClasses != null)
{
currentDrawClasses.Add(drawClass);
}
}
//no namspace like this exists yet, must be 1st one
else
{
_assObjects.Add(nspace, new List<DrawableClass>());
}
}
#endregion
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog