crashanalysercmd/UI/CrashServer/Engine/Files/CACmdLineFSEntityList.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Text;
       
    19 using System.IO;
       
    20 using System.Collections.Generic;
       
    21 
       
    22 namespace CrashAnalyserServerExe.Engine
       
    23 {
       
    24     internal class CACmdLineFSEntityList<T> : IComparer<T>, IEnumerable<T> where T : CACmdLineFSEntity, new()
       
    25 	{
       
    26         #region Constructors
       
    27         public CACmdLineFSEntityList()
       
    28 		{
       
    29 		}
       
    30         #endregion
       
    31 
       
    32 		#region API
       
    33         public void Add( FileInfo aFile )
       
    34         {
       
    35             T entry = new T();
       
    36             entry.File = aFile;
       
    37             //
       
    38             AddInSortedOrder( entry );
       
    39         }
       
    40 
       
    41         public void Add( DirectoryInfo aDir )
       
    42         {
       
    43             T entry = new T();
       
    44             entry.Directory = aDir;
       
    45             //
       
    46             AddInSortedOrder( entry );
       
    47         }
       
    48 
       
    49         public void AddRange( FileInfo[] aFiles )
       
    50         {
       
    51             foreach ( FileInfo file in aFiles )
       
    52             {
       
    53                 Add( file );
       
    54             }
       
    55         }
       
    56 
       
    57         public bool Contains( string aFileName )
       
    58         {
       
    59             CACmdLineFSEntity ret = this[ aFileName ]; 
       
    60             return ret != null;
       
    61         }
       
    62 
       
    63         public void AddToAll( CACmdLineMessage aMessage )
       
    64         {
       
    65             foreach ( CACmdLineFSEntity file in iFiles )
       
    66             {
       
    67                 file.Add( aMessage );
       
    68             }
       
    69         }
       
    70 
       
    71         public T[] ToArray()
       
    72         {
       
    73             return iFiles.ToArray();
       
    74         }
       
    75         #endregion
       
    76 
       
    77 		#region Properties
       
    78         public int Count
       
    79         {
       
    80             get { return iFiles.Count; }
       
    81         }
       
    82 
       
    83         public T this[ int aIndex ]
       
    84         {
       
    85             get { return iFiles[ aIndex ]; }
       
    86         }
       
    87 
       
    88         public T this[ string aFileName ]
       
    89         {
       
    90             get
       
    91             {
       
    92                 T temp = new T();
       
    93                 temp.File = new FileInfo( aFileName );
       
    94                 //
       
    95                 int pos = iFiles.BinarySearch( temp, this );
       
    96                 //
       
    97                 T ret = null;
       
    98                 if ( pos >= 0 )
       
    99                 {
       
   100                     ret = iFiles[ pos ];
       
   101                 }
       
   102                 //
       
   103                 return ret;
       
   104             }
       
   105         }
       
   106         #endregion
       
   107 
       
   108         #region Internal methods
       
   109         private void AddInSortedOrder( T aEntry )
       
   110         {
       
   111             string fileName = aEntry.NameUC;
       
   112             //
       
   113             int pos = iFiles.BinarySearch( aEntry, this );
       
   114             if ( pos < 0 )
       
   115             {
       
   116                 pos = ~pos;
       
   117                 iFiles.Insert( pos, aEntry );
       
   118             }
       
   119             else
       
   120             {
       
   121                 throw new ArgumentException( "Specified file already exists: " + aEntry );
       
   122             }
       
   123         }
       
   124         #endregion
       
   125 
       
   126         #region Operators
       
   127         public static implicit operator string[]( CACmdLineFSEntityList<T> aList )
       
   128         {
       
   129             List<string> ret = new List<string>();
       
   130             foreach ( T file in aList )
       
   131             {
       
   132                 ret.Add( file.Name );
       
   133             }
       
   134             return ret.ToArray();
       
   135         }
       
   136         #endregion
       
   137 
       
   138         #region From IEnumerable<T>
       
   139         public IEnumerator<T> GetEnumerator()
       
   140         {
       
   141             foreach ( T file in iFiles )
       
   142             {
       
   143                 yield return file;
       
   144             }
       
   145         }
       
   146 
       
   147         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   148         {
       
   149             foreach ( T file in iFiles )
       
   150             {
       
   151                 yield return file;
       
   152             }
       
   153         }
       
   154         #endregion
       
   155 
       
   156         #region From IComparer<T>
       
   157         public int Compare( T aLeft, T aRight )
       
   158         {
       
   159             int ret = aLeft.NameUC.CompareTo( aRight.NameUC );
       
   160             return ret;
       
   161         }
       
   162         #endregion
       
   163 
       
   164         #region Data members
       
   165         private List<T> iFiles = new List<T>();
       
   166         #endregion
       
   167     }
       
   168 }