Guest User

Untitled

a guest
Dec 17th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 12.92 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 17-December-2015
  4. ' ***********************************************************************
  5.  
  6. #Region " Imports "
  7.  
  8. Imports System
  9. Imports System.Collections.ObjectModel
  10. Imports System.ComponentModel
  11. Imports System.Diagnostics
  12. Imports System.Linq
  13.  
  14. #End Region
  15.  
  16. #Region " INI Key Collection "
  17.  
  18. Namespace Types
  19.  
  20.     ''' ----------------------------------------------------------------------------------------------------
  21.     ''' <summary>
  22.     ''' Represents a strongly typed list of <see cref="IniKey"/> that can be accessed by an index.
  23.     ''' </summary>
  24.     ''' ----------------------------------------------------------------------------------------------------
  25.     Public NotInheritable Class IniKeyCollection : Inherits Collection(Of IniKey)
  26.  
  27. #Region " Constructors "
  28.  
  29.         ''' ----------------------------------------------------------------------------------------------------
  30.         ''' <summary>
  31.         ''' Initializes a new instance of the <see cref="IniKeyCollection"/> class.
  32.         ''' </summary>
  33.         ''' ----------------------------------------------------------------------------------------------------
  34.         Public Sub New()
  35.         End Sub
  36.  
  37. #End Region
  38.  
  39. #Region " Indexers "
  40.  
  41.         ''' ----------------------------------------------------------------------------------------------------
  42.         ''' <summary>
  43.         ''' Gets or sets the <see cref="Inikey"/> that matches the specified key name.
  44.         ''' </summary>
  45.         ''' ----------------------------------------------------------------------------------------------------
  46.         ''' <param name="keyName">
  47.         ''' The key name.
  48.         ''' </param>
  49.         ''' ----------------------------------------------------------------------------------------------------
  50.         ''' <value>
  51.         ''' The <see cref="Inikey"/>.
  52.         ''' </value>
  53.         ''' ----------------------------------------------------------------------------------------------------
  54.         Default Public Overloads Property Item(ByVal keyName As String) As IniKey
  55.             <DebuggerStepThrough>
  56.             Get
  57.                 Return Me.Find(keyName)
  58.             End Get
  59.             <DebuggerStepThrough>
  60.             Set(ByVal value As IniKey)
  61.                 Me(Me.IndexOf(keyName)) = value
  62.             End Set
  63.         End Property
  64.  
  65. #End Region
  66.  
  67. #Region " Public Methods "
  68.  
  69.         ''' ----------------------------------------------------------------------------------------------------
  70.         ''' <summary>
  71.         ''' Adds a <see cref="IniKey"/> to the end of the <see cref="IniKeyCollection"/>.
  72.         ''' </summary>
  73.         ''' ----------------------------------------------------------------------------------------------------
  74.         ''' <param name="key">
  75.         ''' The key to add.
  76.         ''' </param>
  77.         ''' ----------------------------------------------------------------------------------------------------
  78.         ''' <exception cref="ArgumentException">
  79.         ''' Section already exists.;section
  80.         ''' </exception>
  81.         ''' ----------------------------------------------------------------------------------------------------
  82.         <DebuggerStepThrough>
  83.         Public Shadows Sub Add(ByVal key As IniKey)
  84.  
  85.             If Me.Contains(key.Name) Then
  86.                 Throw New ArgumentException(message:="Key already exists.", paramName:="key")
  87.  
  88.             Else
  89.                 MyBase.Add(key)
  90.  
  91.             End If
  92.  
  93.         End Sub
  94.  
  95.         ''' ----------------------------------------------------------------------------------------------------
  96.         ''' <summary>
  97.         ''' Adds a <see cref="IniKey"/> to the end of the <see cref="IniKeyCollection"/>.
  98.         ''' </summary>
  99.         ''' ----------------------------------------------------------------------------------------------------
  100.         ''' <param name="name">
  101.         ''' The name of the key to add.
  102.         ''' </param>
  103.         ''' ----------------------------------------------------------------------------------------------------
  104.         ''' <exception cref="ArgumentException">
  105.         ''' Key already exists.;section
  106.         ''' </exception>
  107.         ''' ----------------------------------------------------------------------------------------------------
  108.         <DebuggerStepThrough>
  109.         Public Shadows Sub Add(ByVal name As String, ByVal value As String, Optional ByVal comment As String = "")
  110.  
  111.             Me.Add(New IniKey(name, value, comment))
  112.  
  113.         End Sub
  114.  
  115.         ''' ----------------------------------------------------------------------------------------------------
  116.         ''' <summary>
  117.         ''' Adds the specified keys to the end of the <see cref="IniKeyCollection"/>.
  118.         ''' </summary>
  119.         ''' ----------------------------------------------------------------------------------------------------
  120.         ''' <param name="keys">
  121.         ''' The keys to add.
  122.         ''' </param>
  123.         ''' ----------------------------------------------------------------------------------------------------
  124.         ''' <exception cref="ArgumentException">
  125.         ''' Key already exists.;section
  126.         ''' </exception>
  127.         ''' ----------------------------------------------------------------------------------------------------
  128.         <DebuggerStepThrough>
  129.         Public Shadows Sub AddRange(ByVal keys As IniKey())
  130.  
  131.             For Each key As IniKey In keys
  132.  
  133.                 Me.Add(key)
  134.  
  135.             Next key
  136.  
  137.         End Sub
  138.  
  139.         ''' ----------------------------------------------------------------------------------------------------
  140.         ''' <summary>
  141.         ''' Adds the specified keys to the end of the <see cref="IniKeyCollection"/>.
  142.         ''' </summary>
  143.         ''' ----------------------------------------------------------------------------------------------------
  144.         ''' <param name="keyNames">
  145.         ''' The names of the keys to add.
  146.         ''' </param>
  147.         ''' ----------------------------------------------------------------------------------------------------
  148.         ''' <exception cref="ArgumentException">
  149.         ''' Section already exists.;section
  150.         ''' </exception>
  151.         ''' ----------------------------------------------------------------------------------------------------
  152.         <DebuggerStepThrough>
  153.         Public Shadows Sub AddRange(ByVal keyNames As String())
  154.  
  155.             For Each keyName As String In keyNames
  156.  
  157.                 Me.Add(New IniKey(keyName, String.Empty))
  158.  
  159.             Next keyName
  160.  
  161.         End Sub
  162.  
  163.         ''' ----------------------------------------------------------------------------------------------------
  164.         ''' <summary>
  165.         ''' Removes a <see cref="IniKey"/> from the <see cref="IniKeyCollection"/>.
  166.         ''' </summary>
  167.         ''' ----------------------------------------------------------------------------------------------------
  168.         ''' <param name="key">
  169.         ''' The key to remove.
  170.         ''' </param>
  171.         ''' ----------------------------------------------------------------------------------------------------
  172.         ''' <exception cref="ArgumentException">
  173.         ''' Key doesn't exists.;key
  174.         ''' </exception>
  175.         ''' ----------------------------------------------------------------------------------------------------
  176.         <DebuggerStepThrough>
  177.         Public Shadows Sub Remove(ByVal key As IniKey)
  178.  
  179.             Dim indexOf As Integer = Me.IndexOf(key)
  180.  
  181.             If (indexOf = -1) Then
  182.                 Throw New ArgumentException(message:="Key doesn't exists.", paramName:="key")
  183.  
  184.             Else
  185.                 MyBase.RemoveAt(indexOf)
  186.  
  187.             End If
  188.  
  189.         End Sub
  190.  
  191.         ''' ----------------------------------------------------------------------------------------------------
  192.         ''' <summary>
  193.         ''' Removes a <see cref="IniKey"/> from the <see cref="IniKeyCollection"/>.
  194.         ''' </summary>
  195.         ''' ----------------------------------------------------------------------------------------------------
  196.         ''' <param name="keyName">
  197.         ''' The key to remove.
  198.         ''' </param>
  199.         ''' ----------------------------------------------------------------------------------------------------
  200.         ''' <exception cref="ArgumentException">
  201.         ''' Key doesn't exists.;keyName
  202.         ''' </exception>
  203.         ''' ----------------------------------------------------------------------------------------------------
  204.         <DebuggerStepThrough>
  205.         Public Shadows Sub Remove(ByVal keyName As String)
  206.  
  207.             Dim indexOf As Integer = Me.IndexOf(keyName)
  208.  
  209.             If (indexOf = -1) Then
  210.                 Throw New ArgumentException(message:="Key doesn't exists.", paramName:="keyName")
  211.  
  212.             Else
  213.                 MyBase.RemoveAt(indexOf)
  214.  
  215.             End If
  216.  
  217.         End Sub
  218.  
  219.         ''' ----------------------------------------------------------------------------------------------------
  220.         ''' <summary>
  221.         ''' Determines whether the <see cref="IniKeyCollection"/> contains a <see cref="IniKey"/> that
  222.         ''' matches the specified key name.
  223.         ''' </summary>
  224.         ''' ----------------------------------------------------------------------------------------------------
  225.         ''' <param name="keyName">
  226.         ''' The key name.
  227.         ''' </param>
  228.         ''' ----------------------------------------------------------------------------------------------------
  229.         ''' <returns>
  230.         ''' <see langword="True"/> if the <see cref="IniKeyCollection"/> contains the <see cref="IniKey"/>,
  231.         ''' <see langword="False"/> otherwise.
  232.         ''' </returns>
  233.         ''' ----------------------------------------------------------------------------------------------------
  234.         <DebuggerStepThrough>
  235.         Public Overloads Function Contains(ByVal keyName As String) As Boolean
  236.  
  237.             Return (From key As IniKey In Me
  238.                     Where key.Name.Equals(keyName, StringComparison.OrdinalIgnoreCase)).Any
  239.  
  240.         End Function
  241.  
  242.         ''' ----------------------------------------------------------------------------------------------------
  243.         ''' <summary>
  244.         ''' Searches for an <see cref="IniKey"/> that matches the specified key name,
  245.         ''' and returns the first occurrence within the entire <see cref="IniKeyCollection"/>.
  246.         ''' </summary>
  247.         ''' ----------------------------------------------------------------------------------------------------
  248.         ''' <param name="keyName">
  249.         ''' The key name.
  250.         ''' </param>
  251.         ''' ----------------------------------------------------------------------------------------------------
  252.         ''' <returns>
  253.         ''' <see cref="IniKey"/>.
  254.         ''' </returns>
  255.         ''' ----------------------------------------------------------------------------------------------------
  256.         <DebuggerStepThrough>
  257.         Public Overloads Function Find(ByVal keyName As String) As IniKey
  258.  
  259.             Return (From key As IniKey In Me
  260.                     Where key.Name.Equals(keyName, StringComparison.OrdinalIgnoreCase)).
  261.                     DefaultIfEmpty(Nothing).
  262.                     SingleOrDefault
  263.  
  264.         End Function
  265.  
  266.         ''' ----------------------------------------------------------------------------------------------------
  267.         ''' <summary>
  268.         ''' Searches for an <see cref="IniKey"/> that matches the specified key name and
  269.         ''' returns the zero-based index of the first occurrence within the entire <see cref="IniKeyCollection"/>.
  270.         ''' </summary>
  271.         ''' ----------------------------------------------------------------------------------------------------
  272.         ''' <param name="keyName">
  273.         ''' The key name.
  274.         ''' </param>
  275.         ''' ----------------------------------------------------------------------------------------------------
  276.         ''' <returns>
  277.         ''' The zero-based index of the first occurrence of <see cref="IniKey"/> within the entire <see cref="IniKeyCollection"/>, if found;
  278.         ''' otherwise, <c>–1</c>.
  279.         ''' </returns>
  280.         ''' ----------------------------------------------------------------------------------------------------
  281.         <DebuggerStepThrough>
  282.         Public Overloads Function IndexOf(ByVal keyName As String) As Integer
  283.  
  284.             Dim index As Integer = 0
  285.             Dim found As Boolean = False
  286.  
  287.             For Each key As IniKey In Me
  288.  
  289.                 If key.Name.Equals(keyName, StringComparison.OrdinalIgnoreCase) Then
  290.                     found = True
  291.                     Exit For
  292.                 End If
  293.  
  294.                 index += 1
  295.  
  296.             Next key
  297.  
  298.             If (found) Then
  299.                 Return index
  300.  
  301.             Else
  302.                 Return -1
  303.  
  304.             End If
  305.  
  306.         End Function
  307.  
  308. #End Region
  309.  
  310.     End Class
  311.  
  312. End Namespace
  313.  
  314. #End Region
Add Comment
Please, Sign In to add comment