// writer a valued entity private void WriteEntity(StonTokenWriter writer, IStonValuedEntity entity) { if (entity is IStonSimpleEntity) { WriteEntity(writer, entity as IStonSimpleEntity); } else if (entity is IStonComplexEntity) { WriteEntity(writer, entity as IStonComplexEntity); } else { throw new StonImplementationException(entity.GetType(), typeof(IStonValuedEntity), typeof(IStonSimpleEntity), typeof(IStonComplexEntity)); } }
/// <summary> /// Creates a structurally equivalent valued entity from a given entity. /// </summary> /// <param name="entity">The entity to copy the structure of.</param> /// <returns>A structurally equivalent copy of the given entity.</returns> public static IStonValuedEntity Copy(IStonValuedEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity is IStonSimpleEntity) { return(StonSimpleEntity.Copy(entity as IStonSimpleEntity)); } if (entity is IStonComplexEntity) { return(StonComplexEntity.Copy(entity as IStonComplexEntity)); } throw new StonImplementationException(entity.GetType(), typeof(IStonValuedEntity), typeof(IStonSimpleEntity), typeof(IStonComplexEntity)); }
/// <summary> /// Checks a validity of a given STON valued entity. /// </summary> /// <param name="entity">The entity to check the validity of.</param> public static void ValidateEntity(IStonValuedEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } else if (entity is IStonSimpleEntity) { ValidateEntity(entity as IStonSimpleEntity); } else if (entity is IStonComplexEntity) { ValidateEntity(entity as IStonComplexEntity); } else { throw new StonImplementationException(entity.GetType(), typeof(IStonValuedEntity), typeof(IStonSimpleEntity), typeof(IStonComplexEntity)); } }
/// <summary> /// Returns a hash code for a given valued entity, applied to entities semantic equivalence. /// </summary> /// <param name="obj">The valued entity to get a hash code of.</param> /// <returns>The hash code for the entity.</returns> public int GetHashCode(IStonValuedEntity obj) { if (obj == null) { return(0); } else if (obj is IStonSimpleEntity) { return(GetHashCode(obj as IStonSimpleEntity)); } else if (obj is IStonComplexEntity) { return(GetHashCode(obj as IStonComplexEntity)); } else { return(0); } }
/// <summary> /// Determines whether two valued entities are semantically equivalent. /// </summary> /// <param name="x">The first valued entity to compare.</param> /// <param name="y">The second valued entity to compare.</param> /// <returns>True when entities are semantically equivalent, false otherwise.</returns> public bool Equals(IStonValuedEntity x, IStonValuedEntity y) { if (x == y) { return(true); } else if (x == null || y == null) { return(false); } else if (x is IStonSimpleEntity && y is IStonSimpleEntity) { return(Equals(x as IStonSimpleEntity, y as IStonSimpleEntity)); } else { return(false); } }
// reads a usable object from a STON value private static TObject FromSton <TObject>(IStonDocument document, IStonValuedEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { if (entity == null) { return(default(TObject)); } if (!builtObjects.ContainsKey(entity)) { var obj = CreateFromSton <TObject>(document, entity, builtObjects); builtObjects.Add(entity, obj); if (entity is IStonComplexEntity) { InitializeFromSton(obj, document, entity as IStonComplexEntity, builtObjects); } } return((TObject)builtObjects[entity]); }
// creates an object from a STON value private static object CreateFromSton <TObject>(IStonDocument document, IStonValuedEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { var clrType = typeof(TObject); if (clrType == typeof(string)) { return((entity as IStonSimpleEntity).Value.Content); } else if (clrType == typeof(int) || clrType == typeof(int?) || clrType == typeof(long) || clrType == typeof(long?)) { // miiiight wanna make this a built-in part of STON library at some point IStonSimpleValue value = (entity as IStonSimpleEntity).Value; long result = 0; if (value.Content != "0") { int eidx = value.Content.IndexOf('e'); string sstr = value.Content.Remove(eidx); string estr = value.Content.Substring(eidx + 1); result = long.Parse(sstr); var exponent = int.Parse(estr); while (exponent-- > 0) { result *= 10; } } if (clrType == typeof(int) || clrType == typeof(int?)) { return((int)result); } else { return(result); } } else if (clrType == typeof(IPlaylistItem)) { switch ((entity.Type as IStonNamedType).Name) { case "Playlist": return(new Playlist()); case "Track": return(new Track()); default: throw new NotSupportedException(); } } else if (clrType == typeof(IStreamProvider)) { switch ((entity.Type as IStonNamedType).Name) { case "Loop": return(new LoopStreamProvider(builtObjects[document.GetParentContext(entity)] as Track)); default: throw new NotSupportedException(); } } else { throw new NotSupportedException(); } }
/// <summary> /// Builds a STON document using a copy of a given entity as a source, with whitelists of known application extensions and rules determining valid application extension names. /// </summary> /// <param name="coreSource">The entity to copy core's structure from.</param> /// <param name="knownApplicationExtensionTypes">The whitelist of known application extension types.</param> /// <param name="knownApplicationExtensionMembers">The whitelist of known application extension members.</param> /// <param name="extensionTypesRule">The rule determining if a given name is a valid application extension type name.</param> /// <param name="extensionMembersRule">The rule determining if a given name is a valid application extension member name.</param> /// <returns>The built STON document.</returns> public IStonDocument CreateDocument(IStonValuedEntity coreSource, IEnumerable <string> knownApplicationExtensionTypes, IEnumerable <string> knownApplicationExtensionMembers, Func <string, bool> extensionTypesRule, Func <string, bool> extensionMembersRule) { return(new StonDocument(coreSource, knownApplicationExtensionTypes, knownApplicationExtensionMembers, extensionTypesRule, extensionMembersRule)); }