banshee r3303 - in trunk/musicbrainz-sharp/src/MusicBrainz: . MusicBrainz



Author: scottp
Date: Sat Feb 23 03:21:05 2008
New Revision: 3303
URL: http://svn.gnome.org/viewvc/banshee?rev=3303&view=rev

Log:
Various whitespace fixes, refactoring, and using immutable collections

* MusicBrainz/Utilities.cs, MusicBrainz/Utils.cs: Renamed Utilities
  class to Utils. It's internal, so an abriviated name is just fine.
* MusicBrainz/MusicBrainzObject.cs, MusicBrainz/Release.cs,
  MusicBrainz/MusicBrainzEntity.cs, MusicBrainz/Track.cs,
  MusicBrainz/MusicBrainzItem.cs, MusicBrainz/Artist.cs: Renamed MBID
  to Mbid, made to use ReadOnlyCollection instead of List.
* MusicBrainz/Label.cs: Renamed MBID to Mbid
* MusicBrainz/Disc.cs, MusicBrainz/LocalDisc.cs: Split off local-disc
  specific stuff into LocalDisc.
* MusicBrainz/DiscLinux.cs, MusicBrainz/DiscWin32.cs: Inherit from
  LocalDisc
* MusicBrainz/Query.cs: Added First () method which returns just the
  first result (same as the implicit conversion to T).

Added:
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/LocalDisc.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utils.cs
      - copied, changed from r3302, /trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utilities.cs
Removed:
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utilities.cs
Modified:
   trunk/musicbrainz-sharp/src/MusicBrainz/ChangeLog
   trunk/musicbrainz-sharp/src/MusicBrainz/Makefile.am
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz.mdp
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Artist.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Disc.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscLinux.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscWin32.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Event.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Label.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzEntity.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzItem.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzObject.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Query.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Release.cs
   trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Track.cs

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/Makefile.am
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/Makefile.am	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/Makefile.am	Sat Feb 23 03:21:05 2008
@@ -9,6 +9,7 @@
 	MusicBrainz/DiscWin32.cs \
 	MusicBrainz/Event.cs \
 	MusicBrainz/Label.cs \
+	MusicBrainz/LocalDisc.cs \
 	MusicBrainz/MusicBrainzEntity.cs \
 	MusicBrainz/MusicBrainzException.cs \
 	MusicBrainz/MusicBrainzItem.cs \
@@ -17,7 +18,7 @@
 	MusicBrainz/Relation.cs \
 	MusicBrainz/Release.cs \
 	MusicBrainz/Track.cs \
-	MusicBrainz/Utilities.cs \
+	MusicBrainz/Utils.cs \
 	MusicBrainz/XmlRequestEventArgs.cs
 
 instdir = $(prefix)/lib/musicbrainz-sharp
@@ -33,4 +34,3 @@
 CLEANFILES = $(ASSEMBLY) $(ASSEMBLY).mdb
 DISTCLEANFILES = *.pidb
 MAINTAINERCLEANFILES = Makefile Makefile.in
-

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz.mdp
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz.mdp	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz.mdp	Sat Feb 23 03:21:05 2008
@@ -27,9 +27,10 @@
     <File name="MusicBrainz/Relation.cs" subtype="Code" buildaction="Compile" />
     <File name="MusicBrainz/Release.cs" subtype="Code" buildaction="Compile" />
     <File name="MusicBrainz/Track.cs" subtype="Code" buildaction="Compile" />
-    <File name="MusicBrainz/Utilities.cs" subtype="Code" buildaction="Compile" />
+    <File name="MusicBrainz/Utils.cs" subtype="Code" buildaction="Compile" />
     <File name="MusicBrainz/DiscLinux.cs" subtype="Code" buildaction="Compile" />
     <File name="MusicBrainz/XmlRequestEventArgs.cs" subtype="Code" buildaction="Compile" />
+    <File name="MusicBrainz/LocalDisc.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Artist.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Artist.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Artist.cs	Sat Feb 23 03:21:05 2008
@@ -8,6 +8,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Text;
 using System.Xml;
 
@@ -35,8 +36,8 @@
         ArtistReleaseType (Enum enumeration, bool various)
         {
             str = various
-                ? "va-" + Utilities.EnumToString (enumeration)
-                : "sa-" + Utilities.EnumToString (enumeration);
+                ? "va-" + Utils.EnumToString (enumeration)
+                : "sa-" + Utils.EnumToString (enumeration);
         }
 
         public override string ToString ()
@@ -95,7 +96,7 @@
 
         protected override void HandleLoadMissingData ()
         {
-            Artist artist = new Artist (MBID, CreateInc ());
+            Artist artist = new Artist (Mbid, CreateInc ());
             type = artist.Type;
             base.HandleLoadMissingData (artist);
         }
@@ -122,9 +123,10 @@
                 switch (reader.Name) {
                 case "release-list":
                     if (reader.ReadToDescendant ("release")) {
-                        releases = new List<Release> ();
+                        List<Release> releases = new List<Release> ();
                         do releases.Add (new Release (reader.ReadSubtree ()));
                         while (reader.ReadToNextSibling ("release"));
+                        this.releases = new ReadOnlyCollection<Release> (releases);
                     }
                     break;
                 default:
@@ -140,8 +142,8 @@
         #region Properties
 
         [Queryable ("arid")]
-        public override string MBID {
-            get { return base.MBID; }
+        public override string Mbid {
+            get { return base.Mbid; }
         }
 
         [Queryable ("artist")]
@@ -152,16 +154,16 @@
         ArtistType? type;
         [Queryable ("artype")]
         public ArtistType Type {
-            get { return GetPropertyOrDefault<ArtistType> (ref type, ArtistType.Unknown); }
+            get { return GetPropertyOrDefault (ref type, ArtistType.Unknown); }
         }
 
-        List<Release> releases;
+        ReadOnlyCollection<Release> releases;
         bool have_all_releases;
-        public List<Release> Releases {
+        public ReadOnlyCollection<Release> Releases {
             get {
                 return releases ?? (have_all_releases
-                    ? releases = new List<Release> ()
-                    : new Artist (MBID, artist_release_type).Releases);
+                    ? releases = new ReadOnlyCollection<Release> (new Release [0])
+                    : new Artist (Mbid, artist_release_type).Releases);
             }
         }
 

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Disc.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Disc.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Disc.cs	Sat Feb 23 03:21:05 2008
@@ -7,8 +7,6 @@
  ****************************************************************************/
 
 using System;
-using System.Security.Cryptography;
-using System.Text;
 using System.Xml;
 
 namespace MusicBrainz
@@ -17,10 +15,6 @@
     {
         string id;
         int sectors;
-        byte first_track;
-        byte last_track;
-        int [] track_durations;
-        int [] track_offsets = new int [100];
 
         internal Disc ()
         {
@@ -35,75 +29,15 @@
             id = reader ["id"];
             reader.Close ();
         }
-        
-        protected void Init ()
-        {
-            track_durations = new int [last_track];
-            for (int i = 1; i <= last_track; i++) {
-                track_durations [i - 1] = i < last_track
-                    ? track_offsets [i + 1] - track_offsets [i]
-                    : track_offsets [0] - track_offsets [i];
-                track_durations [i - 1] /= 75; // 75 frames in a second
-            }
-            GenerateId ();
-        }
-        
-        void GenerateId ()
-        {
-            StringBuilder input_builder = new StringBuilder (804);
-            
-            input_builder.Append (string.Format ("{0:X2}", FirstTrack));
-            input_builder.Append (string.Format ("{0:X2}", LastTrack));
-            
-            for (int i = 0; i < track_offsets.Length; i++)
-                input_builder.Append (string.Format ("{0:X8}", track_offsets [i]));
 
-            // MB uses a slightly modified RFC822 for reasons of URL happiness.
-            string base64 = Convert.ToBase64String (SHA1.Create ()
-                .ComputeHash (Encoding.ASCII.GetBytes (input_builder.ToString ())));
-            StringBuilder hash_builder = new StringBuilder (base64.Length);
-            
-            foreach (char c in base64)
-                if      (c == '+')  hash_builder.Append ('.');
-                else if (c == '/')  hash_builder.Append ('_');
-                else if (c == '=')  hash_builder.Append ('-');
-                else                hash_builder.Append (c);
-            
-            id = hash_builder.ToString ();
-        }
-
-        public string ID {
+        public string Id {
             get { return id; }
+            protected set { id = value; }
         }
 
         public int Sectors {
             get { return sectors; }
             protected set { sectors = value; }
         }
-
-        protected byte FirstTrack {
-            get { return first_track; }
-            set { first_track = value; }
-        }
-
-        protected byte LastTrack {
-            get { return last_track; }
-            set { last_track = value; }
-        }
-
-        protected int [] TrackOffsets {
-            get { return track_offsets; }
-        }
-
-        public int [] TrackDurations {
-            get { return track_durations; }
-        }
-
-        public static Disc GetFromDevice (string device)
-        {
-            if (device == null) throw new ArgumentNullException ("device");
-            return Environment.OSVersion.Platform != PlatformID.Unix
-                ? (Disc)new DiscWin32 (device) : new DiscLinux (device);
-        }
     }
 }

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscLinux.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscLinux.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscLinux.cs	Sat Feb 23 03:21:05 2008
@@ -13,7 +13,7 @@
 
 namespace MusicBrainz
 {
-    internal sealed class DiscLinux : Disc
+    internal sealed class DiscLinux : LocalDisc
     {
         const int O_RDONLY = 0x0;
         const int O_NONBLOCK = 0x4000;

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscWin32.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscWin32.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/DiscWin32.cs	Sat Feb 23 03:21:05 2008
@@ -15,7 +15,7 @@
 
 namespace MusicBrainz
 {
-    internal sealed class DiscWin32 : Disc
+    internal sealed class DiscWin32 : LocalDisc
     {
         [DllImport ("winmm")]
         static extern Int32 mciSendString (String command,

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Event.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Event.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Event.cs	Sat Feb 23 03:21:05 2008
@@ -27,7 +27,7 @@
             country = reader ["country"];
             catalog_number = reader ["catalog-number"];
             barcode = reader ["barcode"];
-            format = Utilities.StringToEnum<ReleaseFormat> (reader ["format"]);
+            format = Utils.StringToEnum<ReleaseFormat> (reader ["format"]);
 			if (reader.ReadToDescendant ("label")) {
 				label = new Label (reader.ReadSubtree ());
 				reader.Read (); // FIXME this is a workaround for Mono bug 334752

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Label.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Label.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Label.cs	Sat Feb 23 03:21:05 2008
@@ -7,7 +7,6 @@
  ****************************************************************************/
 
 using System;
-using System.Collections.Generic;
 using System.Text;
 using System.Xml;
 
@@ -45,14 +44,14 @@
 
         protected override void HandleLoadMissingData ()
         {
-            Label label = new Label (MBID, CreateInc ());
+            Label label = new Label (Mbid, CreateInc ());
             type = label.Type;
             base.HandleLoadMissingData (label);
         }
 
         protected override bool HandleAttributes (XmlReader reader)
         {
-            type = Utilities.StringToEnum<LabelType> (reader ["type"]);
+            type = Utils.StringToEnum<LabelType> (reader ["type"]);
             return this.type != null;
         }
 
@@ -74,13 +73,15 @@
 
 		string country;
 		public string Country {
-			get { return GetPropertyOrNull<string> (ref country); }
+			get { return GetPropertyOrNull (ref country); }
 		}
 
         LabelType? type;
         public LabelType Type {
-            get { return GetPropertyOrDefault<LabelType> (ref type, LabelType.Unspecified); }
+            get { return GetPropertyOrDefault (ref type, LabelType.Unspecified); }
         }
+        
+        #region Static
 
         public static Label Get (string mbid)
         {
@@ -88,8 +89,6 @@
             return new Label (mbid);
         }
 
-        #region Query
-
         public static Query<Label> Query (string name)
         {
             if (name == null) throw new ArgumentNullException ("name");
@@ -102,11 +101,11 @@
             return new Query<Label> (EXTENSION, QueryLimit, CreateLuceneParameter (luceneQuery));
         }
 
-        #endregion
-
         public static implicit operator string (Label label)
         {
             return label.ToString ();
         }
+        
+        #endregion
     }
 }
\ No newline at end of file

Added: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/LocalDisc.cs
==============================================================================
--- (empty file)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/LocalDisc.cs	Sat Feb 23 03:21:05 2008
@@ -0,0 +1,87 @@
+/***************************************************************************
+ *  LocalDisc.cs
+ *
+ *  Authored by Scott Peterson <lunchtimemama gmail com>
+ * 
+ *  The author disclaims copyright to this source code.
+ ****************************************************************************/
+
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace MusicBrainz
+{
+    public abstract class LocalDisc : Disc
+    {
+        byte first_track;
+        byte last_track;
+        int [] track_durations;
+        int [] track_offsets = new int [100];
+        
+        internal LocalDisc()
+        {
+        }
+        
+        protected void Init ()
+        {
+            track_durations = new int [last_track];
+            for (int i = 1; i <= last_track; i++) {
+                track_durations [i - 1] = i < last_track
+                    ? track_offsets [i + 1] - track_offsets [i]
+                    : track_offsets [0] - track_offsets [i];
+                track_durations [i - 1] /= 75; // 75 frames in a second
+            }
+            GenerateId ();
+        }
+        
+        void GenerateId ()
+        {
+            StringBuilder input_builder = new StringBuilder (804);
+            
+            input_builder.Append (string.Format ("{0:X2}", FirstTrack));
+            input_builder.Append (string.Format ("{0:X2}", LastTrack));
+            
+            for (int i = 0; i < track_offsets.Length; i++)
+                input_builder.Append (string.Format ("{0:X8}", track_offsets [i]));
+
+            // MB uses a slightly modified RFC822 for reasons of URL happiness.
+            string base64 = Convert.ToBase64String (SHA1.Create ()
+                .ComputeHash (Encoding.ASCII.GetBytes (input_builder.ToString ())));
+            StringBuilder hash_builder = new StringBuilder (base64.Length);
+            
+            foreach (char c in base64)
+                if      (c == '+')  hash_builder.Append ('.');
+                else if (c == '/')  hash_builder.Append ('_');
+                else if (c == '=')  hash_builder.Append ('-');
+                else                hash_builder.Append (c);
+            
+            Id = hash_builder.ToString ();
+        }
+        
+        protected byte FirstTrack {
+            get { return first_track; }
+            set { first_track = value; }
+        }
+
+        protected byte LastTrack {
+            get { return last_track; }
+            set { last_track = value; }
+        }
+
+        protected int [] TrackOffsets {
+            get { return track_offsets; }
+        }
+        
+        public int [] TrackDurations {
+            get { return track_durations; }
+        }
+        
+        public static LocalDisc GetFromDevice (string device)
+        {
+            if (device == null) throw new ArgumentNullException ("device");
+            return Environment.OSVersion.Platform != PlatformID.Unix
+                ? (LocalDisc)new DiscWin32 (device) : new DiscLinux (device);
+        }
+    }
+}

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzEntity.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzEntity.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzEntity.cs	Sat Feb 23 03:21:05 2008
@@ -8,6 +8,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Text;
 using System.Xml;
 
@@ -66,12 +67,13 @@
                 break;
             case "alias-list":
                 if (reader.ReadToDescendant ("alias")) {
-                    aliases = new List<string> ();
+                    List<string> aliases = new List<string> ();
                     do {
 						reader.Read ();
 						if (reader.NodeType == XmlNodeType.Text)
 							aliases.Add (reader.ReadContentAsString ());
                     } while (reader.ReadToNextSibling ("alias"));
+                    this.aliases = new ReadOnlyCollection<string> (aliases);
                 }
                 break;
             default:
@@ -85,44 +87,44 @@
 
         string name;
         public virtual string Name {
-            get { return GetPropertyOrNull<string> (ref name); }
+            get { return GetPropertyOrNull (ref name); }
         }
 
         string sort_name;
         [Queryable]
         public virtual string SortName {
-            get { return GetPropertyOrNull<string> (ref sort_name); }
+            get { return GetPropertyOrNull (ref sort_name); }
         }
 
         string disambiguation;
         [Queryable ("comment")]
         public virtual string Disambiguation {
-            get { return GetPropertyOrNull<string> (ref disambiguation); }
+            get { return GetPropertyOrNull (ref disambiguation); }
         }
 
         string begin_date;
         [Queryable ("begin")]
         public virtual string BeginDate {
-            get { return GetPropertyOrNull<string> (ref begin_date); }
+            get { return GetPropertyOrNull (ref begin_date); }
         }
 
         string end_date;
         [Queryable ("end")]
         public virtual string EndDate {
-            get { return GetPropertyOrNull<string> (ref end_date); }
+            get { return GetPropertyOrNull (ref end_date); }
         }
 
-        List<string> aliases;
+        ReadOnlyCollection<string> aliases;
         [QueryableMember ("Contains", "alias")]
-        public virtual List<string> Aliases {
-            get { return GetPropertyOrNew<List<string>> (ref aliases); }
+        public virtual ReadOnlyCollection<string> Aliases {
+            get { return GetPropertyOrNew (ref aliases); }
         }
 
         #endregion
 
         protected static string CreateNameParameter (string name)
         {
-            return "&name=" + Utilities.PercentEncode (name);
+            return "&name=" + Utils.PercentEncode (name);
         }
 
         public override string ToString ()

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzItem.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzItem.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzItem.cs	Sat Feb 23 03:21:05 2008
@@ -7,7 +7,6 @@
  ****************************************************************************/
 
 using System;
-using System.Collections.Generic;
 using System.Text;
 using System.Xml;
 
@@ -59,11 +58,11 @@
         {
             if (title != null) {
                 builder.Append ("&title=");
-                Utilities.PercentEncode (builder, title);
+                Utils.PercentEncode (builder, title);
             }
             if (artist != null) {
                 builder.Append ("&artist=");
-                Utilities.PercentEncode (builder, artist);
+                Utils.PercentEncode (builder, artist);
             }
             if (artist_id != null) {
                 builder.Append ("&artistid=");
@@ -71,7 +70,7 @@
             }
             if (release_type != null) {
                 builder.Append ("&releasetypes=");
-                builder.Append (Utilities.EnumToString (release_type.Value));
+                builder.Append (Utils.EnumToString (release_type.Value));
             }
             if (release_status != null) {
                 builder.Append (release_type != null ? "+" : "&releasetypes=");
@@ -129,13 +128,13 @@
 
         string title;
         public virtual string Title {
-            get { return GetPropertyOrNull<string> (ref title); }
+            get { return GetPropertyOrNull (ref title); }
         }
 
         Artist artist;
         [Queryable ("artist")]
         public virtual Artist Artist {
-            get { return GetPropertyOrNull<Artist> (ref artist); }
+            get { return GetPropertyOrNull (ref artist); }
         }
 
         public override string ToString () {

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzObject.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzObject.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/MusicBrainzObject.cs	Sat Feb 23 03:21:05 2008
@@ -8,6 +8,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.IO;
 using System.Net;
 using System.Net.Cache;
@@ -105,24 +106,28 @@
                     all_rels_loaded = true;
                     switch (reader ["target-type"]) {
                     case "Artist":
-                        artist_rels = new List<Relation<Artist>> ();
-                        CreateRelation<Artist> (reader.ReadSubtree (), artist_rels);
+                        List<Relation<Artist>> artist_rels = new List<Relation<Artist>> ();
+                        CreateRelation (reader.ReadSubtree (), artist_rels);
+                        this.artist_rels = new ReadOnlyCollection<Relation<Artist>> (artist_rels);
                         break;
                     case "Release":
-                        release_rels = new List<Relation<Release>> ();
-                        CreateRelation<Release> (reader.ReadSubtree (), release_rels);
+                        List<Relation<Release>> release_rels = new List<Relation<Release>> ();
+                        CreateRelation (reader.ReadSubtree (), release_rels);
+                        this.release_rels = new ReadOnlyCollection<Relation<Release>> (release_rels);
                         break;
                     case "Track":
-                        track_rels = new List<Relation<Track>> ();
-                        CreateRelation<Track> (reader.ReadSubtree (), track_rels);
+                        List<Relation<Track>> track_rels = new List<Relation<Track>> ();
+                        CreateRelation (reader.ReadSubtree (), track_rels);
+                        this.track_rels = new ReadOnlyCollection<Relation<Track>> (track_rels);
                         break;
                     case "Label":
-                        label_rels = new List<Relation<Label>> ();
-                        CreateRelation<Label> (reader.ReadSubtree (), label_rels);
+                        List<Relation<Label>> label_rels = new List<Relation<Label>> ();
+                        CreateRelation (reader.ReadSubtree (), label_rels);
+                        this.label_rels = new ReadOnlyCollection<Relation<Label>> (label_rels);
                         break;
                     case "Url":
                         if (!reader.ReadToDescendant ("relation")) break;
-                        url_rels = new List<UrlRelation> ();
+                        List<UrlRelation> url_rels = new List<UrlRelation> ();
                         do {
                             RelationDirection direction = RelationDirection.Forward;
                             string direction_string = reader ["direction"];
@@ -139,6 +144,7 @@
                                 reader ["end"],
                                 attributes));
                         } while (reader.ReadToNextSibling ("relation"));
+                        this.url_rels = new ReadOnlyCollection<UrlRelation> (url_rels);
                         break;
                     }
                 } else
@@ -181,19 +187,19 @@
             return field_reference ?? default_value;
         }
         
-        protected T GetPropertyOrNew<T> (ref T field_reference) where T : class, new ()
+        protected ReadOnlyCollection<T> GetPropertyOrNew<T> (ref ReadOnlyCollection<T> field_reference)
         {
-            return GetPropertyOrNew<T> (ref field_reference, true);
+            return GetPropertyOrNew (ref field_reference, true);
         }
         
-        protected T GetPropertyOrNew<T> (ref T field_reference, bool condition) where T : class, new ()
+        protected ReadOnlyCollection<T> GetPropertyOrNew<T> (ref ReadOnlyCollection<T> field_reference, bool condition)
         {
             if (field_reference == null && condition) LoadMissingData ();
-            return field_reference ?? new T ();
+            return field_reference ?? new ReadOnlyCollection<T> (new T [0]);
         }
 
         string mbid;
-        public virtual string MBID {
+        public virtual string Mbid {
             get { return mbid; }
         }
 
@@ -202,45 +208,45 @@
             get { return score; }
         }
 
-        List<Relation<Artist>> artist_rels;
-        public virtual List<Relation<Artist>> ArtistRelations {
-            get { return GetPropertyOrNew<List<Relation<Artist>>> (ref artist_rels, !all_rels_loaded); }
+        ReadOnlyCollection<Relation<Artist>> artist_rels;
+        public virtual ReadOnlyCollection<Relation<Artist>> ArtistRelations {
+            get { return GetPropertyOrNew (ref artist_rels, !all_rels_loaded); }
         }
 
-        List<Relation<Release>> release_rels;
-        public virtual List<Relation<Release>> ReleaseRelations {
-            get { return GetPropertyOrNew<List<Relation<Release>>> (ref release_rels, !all_rels_loaded); }
+        ReadOnlyCollection<Relation<Release>> release_rels;
+        public virtual ReadOnlyCollection<Relation<Release>> ReleaseRelations {
+            get { return GetPropertyOrNew (ref release_rels, !all_rels_loaded); }
         }
 
-        List<Relation<Track>> track_rels;
-        public virtual List<Relation<Track>> TrackRelations {
-            get { return GetPropertyOrNew<List<Relation<Track>>> (ref track_rels, !all_rels_loaded); }
+        ReadOnlyCollection<Relation<Track>> track_rels;
+        public virtual ReadOnlyCollection<Relation<Track>> TrackRelations {
+            get { return GetPropertyOrNew (ref track_rels, !all_rels_loaded); }
         }
 
-        List<Relation<Label>> label_rels;
-        public virtual List<Relation<Label>> LabelRelations {
-            get { return GetPropertyOrNew<List<Relation<Label>>> (ref label_rels, !all_rels_loaded); }
+        ReadOnlyCollection<Relation<Label>> label_rels;
+        public virtual ReadOnlyCollection<Relation<Label>> LabelRelations {
+            get { return GetPropertyOrNew (ref label_rels, !all_rels_loaded); }
         }
 
-        List<UrlRelation> url_rels;
-        public virtual List<UrlRelation> UrlRelations {
-            get { return GetPropertyOrNew<List<UrlRelation>> (ref url_rels, !all_rels_loaded); }
+        ReadOnlyCollection<UrlRelation> url_rels;
+        public virtual ReadOnlyCollection<UrlRelation> UrlRelations {
+            get { return GetPropertyOrNew (ref url_rels, !all_rels_loaded); }
         }
 
         public override bool Equals (object obj)
         {
             MusicBrainzObject mbobj = obj as MusicBrainzObject;
-            return mbobj != null && mbobj.GetType ().Equals (GetType ()) && mbobj.MBID == MBID;
+            return mbobj != null && mbobj.GetType ().Equals (GetType ()) && mbobj.Mbid == Mbid;
         }
 
         public override int GetHashCode ()
         {
-            return (GetType ().Name + MBID).GetHashCode ();
+            return (GetType ().Name + Mbid).GetHashCode ();
         }
 
         #endregion
 
-        #region Static Methods
+        #region Static
 
         static void CreateRelation<T> (XmlReader reader, List<Relation<T>> relations) where T : MusicBrainzObject
         {
@@ -253,11 +259,11 @@
                 string begin = reader ["begin"];
                 string end = reader ["end"];
                 string attributes_string = reader ["attributes"];
-                string[] attributes = attributes_string == null
+                string [] attributes = attributes_string == null
                     ? null : attributes_string.Split (' ');
 
                 reader.Read ();
-                relations.Add (new Relation<T>(
+                relations.Add (new Relation<T> (
                     type,
                     ConstructMusicBrainzObjectFromXml<T> (reader.ReadSubtree ()),
                     direction,
@@ -364,7 +370,7 @@
 
         protected static string CreateLuceneParameter (string query)
         {
-            return "&query=" + Utilities.PercentEncode (query);
+            return "&query=" + Utils.PercentEncode (query);
         }
 
         internal static List<T> Query<T> (string url_extension, byte limit, int offset, string parameters, out int? count) where T : MusicBrainzObject

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Query.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Query.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Query.cs	Sat Feb 23 03:21:05 2008
@@ -115,14 +115,19 @@
             }
         }
         
-        public static implicit operator T (Query<T> query)
+        public T First ()
         {
-            byte limit = query.limit;
-            query.limit = 1;
-            T result = query.Count > 0 ? query [0] : null;
-            query.limit = limit;
+            byte tmp_limit = limit;
+            limit = 1;
+            T result = Count > 0 ? this [0] : null;
+            limit = tmp_limit;
             return result;
         }
+        
+        public static implicit operator T (Query<T> query)
+        {
+            return query.First ();
+        }
     }
 
     [AttributeUsage (AttributeTargets.Property)]

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Release.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Release.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Release.cs	Sat Feb 23 03:21:05 2008
@@ -8,6 +8,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Text;
 using System.Xml;
 
@@ -101,7 +102,7 @@
             }
             if (date != null) {
                 builder.Append ("&date=");
-                Utilities.PercentEncode (builder, date);
+                Utils.PercentEncode (builder, date);
             }
             if (asin != null) {
                 builder.Append ("&asin=");
@@ -153,7 +154,7 @@
 
         protected override void HandleLoadMissingData ()
         {
-            Release release = new Release (MBID, CreateInc ());
+            Release release = new Release (Mbid, CreateInc ());
             type = release.Type;
             status = release.Status;
             language = release.Language;
@@ -170,26 +171,14 @@
             // How sure am I about getting the type and status in the "Type Status" format?
             // MB really ought to specify these two things seperatly.
             string type_string = reader ["type"];
-            if (type_string != null) {
+            if (type_string != null)
                 foreach (string token in type_string.Split (' ')) {
-                    if (this.type == null) {
-                        bool found = false;
-                        foreach (ReleaseType type in Enum.GetValues (typeof (ReleaseType)) as ReleaseType [])
-                            if (type.ToString () == token) {
-                                this.type = type;
-                                found = true;
-                                break;
-                            }
-                        if (found) continue;
+                    if (type == null) {
+                        type = Utils.StringToEnumOrNull<ReleaseType> (token);
+                        if (type != null) continue;
                     }
-
-                    foreach (ReleaseStatus status in Enum.GetValues (typeof (ReleaseStatus)) as ReleaseStatus [])
-                        if (status.ToString () == token) {
-                            this.status = status;
-                            break;
-                        }
+                    this.status = Utils.StringToEnumOrNull<ReleaseStatus> (token);
                 }
-            }
             return this.type != null || this.status != null;
         }
 
@@ -211,18 +200,20 @@
                     break;
                 case "disc-list": {
                     if (reader.ReadToDescendant ("disc")) {
-                        discs = new List<Disc> ();
+                        List<Disc> discs = new List<Disc> ();
                         do discs.Add (new Disc (reader.ReadSubtree ()));
                         while (reader.ReadToNextSibling ("disc"));
+                        this.discs = new ReadOnlyCollection<Disc> (discs);
                     }
                     break;
                 }
                 case "release-event-list":
                     if (!AllDataLoaded) reader.Skip(); // FIXME this is a workaround for Mono bug 334752
                     if (reader.ReadToDescendant ("event")) {
-                        events = new List<Event> ();
+                        List<Event> events = new List<Event> ();
                         do events.Add (new Event (reader.ReadSubtree ()));
 						while (reader.ReadToNextSibling ("event"));
+                        this.events = new ReadOnlyCollection<Event> (events);
                     }
                     break;
                 case "track-list": {
@@ -230,9 +221,10 @@
                     if (offset != null)
                         track_number = int.Parse (offset) + 1;
                     if (reader.ReadToDescendant ("track")) {
-                        tracks = new List<Track> ();
+                        List<Track> tracks = new List<Track> ();
                         do tracks.Add (new Track (reader.ReadSubtree (), AllDataLoaded));
                         while (reader.ReadToNextSibling ("track"));
+                        this.tracks = new ReadOnlyCollection<Track> (tracks);
                     }
                     break;
                 }
@@ -249,8 +241,8 @@
         #region Properties
 
         [Queryable ("reid")]
-        public override string MBID {
-            get { return base.MBID; }
+        public override string Mbid {
+            get { return base.Mbid; }
         }
 
         [Queryable ("release")]
@@ -261,48 +253,48 @@
         ReleaseType? type;
         [Queryable]
         public ReleaseType Type {
-            get { return GetPropertyOrDefault<ReleaseType> (ref type, ReleaseType.None); }
+            get { return GetPropertyOrDefault (ref type, ReleaseType.None); }
         }
 
         ReleaseStatus? status;
         [Queryable]
         public ReleaseStatus Status {
-            get { return GetPropertyOrDefault<ReleaseStatus> (ref status, ReleaseStatus.None); }
+            get { return GetPropertyOrDefault (ref status, ReleaseStatus.None); }
 
         }
 
         string language;
         public string Language {
-            get { return GetPropertyOrNull<string> (ref language); }
+            get { return GetPropertyOrNull (ref language); }
         }
 
         string script;
         [Queryable]
         public string Script {
-            get { return GetPropertyOrNull<string> (ref script); }
+            get { return GetPropertyOrNull (ref script); }
         }
 
         string asin;
         [Queryable]
         public string Asin {
-            get { return GetPropertyOrNull<string> (ref asin); }
+            get { return GetPropertyOrNull (ref asin); }
         }
 
-        List<Disc> discs;
+        ReadOnlyCollection<Disc> discs;
         [QueryableMember("Count", "discids")]
-        public List<Disc> Discs {
-            get { return GetPropertyOrNew<List<Disc>> (ref discs); }
+        public ReadOnlyCollection<Disc> Discs {
+            get { return GetPropertyOrNew (ref discs); }
         }
 
-        List<Event> events;
-        public List<Event> Events {
-            get { return GetPropertyOrNew<List<Event>> (ref events); }
+        ReadOnlyCollection<Event> events;
+        public ReadOnlyCollection<Event> Events {
+            get { return GetPropertyOrNew (ref events); }
         }
 
-        List<Track> tracks;
+        ReadOnlyCollection<Track> tracks;
         [QueryableMember ("Count", "tracks")]
-        public List<Track> Tracks {
-            get { return GetPropertyOrNew<List<Track>> (ref tracks); }
+        public ReadOnlyCollection<Track> Tracks {
+            get { return GetPropertyOrNew (ref tracks); }
         }
 
         int? track_number;
@@ -311,6 +303,8 @@
         }
 
         #endregion
+        
+        #region Static
 
         public static Release Get (string mbid)
         {
@@ -318,8 +312,6 @@
             return new Release (mbid);
         }
 
-        #region Query
-
         public static Query<Release> Query (string title)
         {
             if (title == null) throw new ArgumentNullException ("title");
@@ -351,7 +343,7 @@
             if (device == null) throw new ArgumentNullException ("device");
             
             ReleaseQueryParameters parameters = new ReleaseQueryParameters ();
-            parameters.DiscID = Disc.GetFromDevice (device).ID;
+            parameters.DiscID = LocalDisc.GetFromDevice (device).Id;
             return Query (parameters);
         }
 
@@ -361,11 +353,11 @@
             return new Query<Release> (EXTENSION, QueryLimit, CreateLuceneParameter (luceneQuery));
         }
 
-        #endregion
-
         public static implicit operator string (Release release)
         {
             return release.ToString ();
         }
+        
+        #endregion
     }
 }
\ No newline at end of file

Modified: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Track.cs
==============================================================================
--- trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Track.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Track.cs	Sat Feb 23 03:21:05 2008
@@ -8,6 +8,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Collections.ObjectModel;
 using System.Text;
 using System.Xml;
 
@@ -50,7 +51,7 @@
             StringBuilder builder = new StringBuilder ();
             if (release != null) {
                 builder.Append ("&release=");
-                Utilities.PercentEncode (builder, release);
+                Utils.PercentEncode (builder, release);
             }
             if (release_id != null) {
                 builder.Append ("&releaseid=");
@@ -106,7 +107,7 @@
 
         protected override void HandleLoadMissingData ()
         {
-            Track track = new Track (MBID, CreateInc ());
+            Track track = new Track (Mbid, CreateInc ());
             duration = track.Duration;
             if (releases == null) releases = track.Releases;
             if (puids == null) puids = track.Puids;
@@ -132,16 +133,18 @@
                     break;
                 case "release-list":
                     if(reader.ReadToDescendant ("release")) {
-                        releases = new List<Release> ();
+                        List<Release> releases = new List<Release> ();
                         do releases.Add (new Release (reader.ReadSubtree ()));
                         while (reader.ReadToNextSibling ("release"));
+                        this.releases = new ReadOnlyCollection<Release> (releases);
                     }
                     break;
                 case "puid-list":
                     if(reader.ReadToDescendant ("puid")) {
-                        puids = new List<string> ();
+                        List<string> puids = new List<string> ();
                         do puids.Add (reader ["id"]);
                         while (reader.ReadToNextSibling ("puid"));
+                        this.puids = new ReadOnlyCollection<string> (puids);
                     }
                     break;
                 default:
@@ -157,8 +160,8 @@
         #region Properties
 
         [Queryable ("trid")]
-        public override string MBID {
-            get { return base.MBID; }
+        public override string Mbid {
+            get { return base.Mbid; }
         }
 
         [Queryable ("track")]
@@ -172,16 +175,16 @@
             get { return duration; }
         }
 
-        List<Release> releases;
+        ReadOnlyCollection<Release> releases;
         [QueryableMember ("Contains", "release")]
-        public List<Release> Releases {
-            get { return GetPropertyOrNew<List<Release>> (ref releases); }
+        public ReadOnlyCollection<Release> Releases {
+            get { return GetPropertyOrNew (ref releases); }
 
         }
 
-        List<string> puids;
-        public List<string> Puids {
-            get { return GetPropertyOrNew<List<string>> (ref puids); }
+        ReadOnlyCollection<string> puids;
+        public ReadOnlyCollection<string> Puids {
+            get { return GetPropertyOrNew (ref puids); }
         }
 
         public int GetTrackNumber (Release release)
@@ -195,6 +198,8 @@
         }
 
         #endregion
+        
+        #region Static
 
         public static Track Get (string mbid)
         {
@@ -202,8 +207,6 @@
             return new Track (mbid);
         }
 
-        #region Query
-
         public static Query<Track> Query (string title)
         {
             if (title == null) throw new ArgumentNullException ("title");
@@ -249,11 +252,11 @@
             return new Query<Track> (EXTENSION, QueryLimit, CreateLuceneParameter (luceneQuery));
         }
 
-        #endregion
-
         public static implicit operator string (Track track)
         {
             return track.ToString ();
         }
+        
+        #endregion
     }
 }
\ No newline at end of file

Copied: trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utils.cs (from r3302, /trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utilities.cs)
==============================================================================
--- /trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utilities.cs	(original)
+++ trunk/musicbrainz-sharp/src/MusicBrainz/MusicBrainz/Utils.cs	Sat Feb 23 03:21:05 2008
@@ -1,5 +1,5 @@
 /***************************************************************************
- *  Utilities.cs
+ *  Utils.cs
  *
  *  Authored by Scott Peterson <lunchtimemama gmail com>
  * 
@@ -11,7 +11,7 @@
 
 namespace MusicBrainz
 {
-    internal static class Utilities
+    internal static class Utils
     {
         public static string EnumToString (Enum enumeration)
         {
@@ -26,13 +26,18 @@
             return builder.ToString ();
         }
         
-        public static T StringToEnum<T> (string name)
+        public static T StringToEnum<T> (string name) where T : struct
+        {
+            return StringToEnumOrNull<T> (name) ?? default (T);
+        }
+        
+        public static T? StringToEnumOrNull<T> (string name) where T : struct
         {
             if (name != null)
                 foreach (T value in Enum.GetValues (typeof (T)))
                     if (Enum.GetName (typeof (T), value) == name)
                         return value;
-            return default (T);
+            return null;
         }
         
         public static string PercentEncode (string value)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]