public int GetHashCode(ref IList <LeftRightPair <object, int> > hashCodes) { if (hashCodes is null) { throw new ArgumentNullException(nameof(hashCodes)); } foreach (LeftRightPair <object, int> pair in hashCodes) { if (pair.Left == this) { return(pair.Right); } } LeftRightPair <object, int> sourceHash = new LeftRightPair <object, int>(this, 0); hashCodes.Add(sourceHash); unchecked { int hashCode = 3371; foreach (string property in _properties.Keys) { hashCode *= 2; hashCode += CyclicalMethods.GetHashCode(_properties[property], ref hashCodes); } sourceHash.Right = hashCode; return(hashCode); } }
/// <inheritdoc/> public XmlElement ToXmlElement(XmlDocument xmlDocument, ref IList <object> id_source, string name = "") { if (xmlDocument is null) { throw new ArgumentNullException(nameof(xmlDocument)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (name is null) { throw new ArgumentNullException(nameof(name)); } if (name == "") { name = GetType().Name; } XmlElement xmlThis = xmlDocument.CreateElement(name); if (id_source.Contains(this)) { xmlThis.SetAttribute("ref", id_source.IndexOf(this).ToString("x16")); } else { xmlThis.SetAttribute("id", id_source.Count.ToString("x16")); id_source.Add(this); xmlThis.SetAttribute("type", GetType().FullName); XmlElement xmlRoute = xmlDocument.CreateElement("RouteType"); xmlThis.AppendChild(xmlRoute); xmlRoute.InnerText = RouteType.ToString(); XmlElement xmlRouteMode = xmlDocument.CreateElement("RouteMode"); xmlThis.AppendChild(xmlRouteMode); xmlRouteMode.InnerText = RouteMode.ToString(); XmlElement xmlIncrease = xmlDocument.CreateElement("Increase"); xmlThis.AppendChild(xmlIncrease); xmlIncrease.InnerText = _increase.ToString(); XmlElement xmlCurrentIndex = xmlDocument.CreateElement("CurrentIndex"); xmlThis.AppendChild(xmlCurrentIndex); xmlCurrentIndex.InnerText = CurrentIndex is null ? "null" : CurrentIndex.ToString(); XmlElement xmlEntries = xmlDocument.CreateElement("Entries"); xmlThis.AppendChild(xmlEntries); foreach (T entry in this) { XmlElement xmlEntry = CyclicalMethods.ToXmlElement(entry, xmlDocument, ref id_source, "Entry"); xmlEntries.AppendChild(xmlEntry); } } return(xmlThis); }
public override XmlElement ToXmlElement(XmlDocument xmlDocument, ref IList <object> id_source, string name = "") { if (xmlDocument is null) { throw new ArgumentNullException(nameof(xmlDocument)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (name is null) { throw new ArgumentNullException(nameof(name)); } if (name == "") { name = GetType().Name; } XmlElement xmlThis = xmlDocument.CreateElement(name); if (id_source.Contains(this)) { xmlThis.SetAttribute("ref", id_source.IndexOf(this).ToString("x16")); } else { xmlThis.SetAttribute("type", GetType().FullName); XmlElement xmlName = xmlDocument.CreateElement("Name"); xmlThis.AppendChild(xmlName); xmlName.InnerText = _name; XmlElement xmlDescription = xmlDocument.CreateElement("Description"); xmlThis.AppendChild(xmlDescription); xmlDescription.InnerText = _description; XmlElement xmlRouteType = xmlDocument.CreateElement("RouteType"); xmlThis.AppendChild(xmlRouteType); xmlRouteType.InnerText = RouteType.ToString(); XmlElement xmlRouteMode = xmlDocument.CreateElement("RouteMode"); xmlThis.AppendChild(xmlRouteMode); xmlRouteMode.InnerText = RouteMode.ToString(); XmlElement xmlWorkers = xmlDocument.CreateElement("Workers"); xmlThis.AppendChild(xmlWorkers); foreach (object worker in _workers) { XmlElement xmlWorker = CyclicalMethods.ToXmlElement(worker, xmlDocument, ref id_source, "Worker"); xmlWorkers.AppendChild(xmlWorker); } } return(xmlThis); }
public object CyclicalClone(ref IDictionary <object, object> contract) { if (contract is null) { throw new ArgumentNullException(nameof(contract)); } Obj clone = new Obj(); if (!contract.ContainsKey(this)) { contract.Add(this, clone); } foreach (string property in _properties.Keys) { clone.SetProperty(property, CyclicalMethods.Clone(this[property], ref contract)); } return(clone); }
public object CyclicalClone(ref IDictionary <object, object> contract) { if (contract is null) { throw new ArgumentNullException(nameof(contract)); } IGroup <T> clone = new Group <T>(); if (!contract.ContainsKey(this)) { contract.Add(this, clone); } foreach (object entry in _entries) { clone.Include(CyclicalMethods.Clone(entry, ref contract)); } return(clone); }
public XmlElement ToXmlElement(XmlDocument xmlDocument, ref IList <object> id_source, string name = "") { if (xmlDocument is null) { throw new ArgumentNullException(nameof(xmlDocument)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (name is null) { throw new ArgumentNullException(nameof(name)); } if (name == "") { name = GetType().Name; } XmlElement xmlThis = xmlDocument.CreateElement(name); if (id_source.Contains(this)) { xmlThis.SetAttribute("ref", id_source.IndexOf(this).ToString("x16")); } else { xmlThis.SetAttribute("id", id_source.Count.ToString("x16")); id_source.Add(this); xmlThis.SetAttribute("type", GetType().FullName); XmlElement xmlProperties = xmlDocument.CreateElement("Properties"); xmlThis.AppendChild(xmlProperties); foreach (string property in _properties.Keys) { XmlElement xmlProperty = CyclicalMethods.ToXmlElement(this[property], xmlDocument, ref id_source, "Property"); xmlProperty.SetAttribute("key", property); xmlProperties.AppendChild(xmlProperty); } } return(xmlThis); }
/// <summary> /// Creates an object from its <see cref="XmlElement"/> representation. /// </summary> /// <param name="source"></param> /// <param name="id_source"></param> /// <returns></returns> public static Manager ToObject(XmlElement source, ref IDictionary <string, object> id_source) { Type type = typeof(Manager); if (source is null) { throw new ArgumentNullException(nameof(source)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (source.HasAttribute("id")) { if (id_source.ContainsKey(source.GetAttribute("id"))) { throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" has already been created."); } } if (source.HasAttribute("ref")) { if (id_source.ContainsKey(source.GetAttribute("ref"))) { if (id_source[source.GetAttribute("ref")] is Manager _source) { return(_source); } else { throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" must be \"{type.FullName}\"."); } } else { throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" must have been created earlier."); } } Manager manager = new Manager(); if (source.HasAttribute("id")) { id_source.Add(source.GetAttribute("id"), manager); } XmlNodeList xmlEntriesList = source.ChildNodes; foreach (XmlElement xmlElement in xmlEntriesList) { switch (xmlElement.Name) { case "Name": if (!string.IsNullOrEmpty(xmlElement.InnerText)) { manager.Name = xmlElement.InnerXml; } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "Description": if (xmlElement.InnerText != null) { manager.Description = xmlElement.InnerText; } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "RouteType": RouteType routeType; if (Enum.TryParse(xmlElement.InnerText, out routeType)) { manager.SetRouteType(routeType); } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "RouteMode": RouteMode routeMode; if (Enum.TryParse(xmlElement.InnerText, out routeMode)) { manager.SetRouteMode(routeMode); } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "Workers": List <IWorker> list = manager._workers; foreach (XmlElement xmlEntry in xmlElement.ChildNodes) { if (xmlEntry.Name != "Worker") { throw new XmlException("The \"Workers\" node can only contain \"Worker\" nodes."); } else if (CyclicalMethods.ToObject(xmlEntry, ref id_source) is IWorker worker) { list.Add(worker); } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } } break; } } manager.Reset(); return(manager); }
public static Obj ToObject(XmlElement source, ref IDictionary <string, object> id_source) { if (source is null) { throw new ArgumentNullException(nameof(source)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (source.HasAttribute("id")) { if (id_source.ContainsKey(source.GetAttribute("id"))) { throw new XmlException($"Object with id=\"{source.GetAttribute("id")}\" has already been created."); } } if (source.HasAttribute("ref")) { if (id_source.ContainsKey(source.GetAttribute("ref"))) { if (id_source[source.GetAttribute("ref")] is Obj _obj) { return(_obj); } else { throw new XmlException($"Object with id=\"{source.GetAttribute("id")}\" must be \"{typeof(Obj).FullName}\"."); } } } Obj obj = new Obj(); if (source.HasAttribute("id")) { id_source.Add(source.GetAttribute("id"), obj); } XmlNodeList xmlPropertiesList = source.ChildNodes; int k = -1; for (int i = 0; i < xmlPropertiesList.Count; i++) { if (xmlPropertiesList[i].Name == "Properties") { if (k == -1) { k = i; } else { throw new XmlException($"XML representation of object of type \"{typeof(Obj).FullName}\" must contain one node \"Properties\"."); } } } if (k == -1) { throw new XmlException($"XML representation of object of type \"{typeof(Obj).FullName}\" must contain one node \"Properties\"."); } XmlElement xmlProperties = (XmlElement)xmlPropertiesList[k]; foreach (XmlElement xmlProperty in xmlProperties.ChildNodes) { if (xmlProperty.Name != "Property") { throw new XmlException("The XML node \"Properties\" can only contain \"Property\" nodes."); } else if (xmlProperty.HasAttribute("key")) { if (obj.ContainsProperty(xmlProperty.GetAttribute("key"))) { throw new XmlException($"Property with key=\"{xmlProperty.GetAttribute("key")}\" has already been created."); } else { obj.SetProperty(xmlProperty.GetAttribute("key"), CyclicalMethods.ToObject(xmlProperty, ref id_source)); } } else { throw new XmlException("XML node \"Property\" must contain the attribute \"key\"."); } } return(obj); }
public static Group <T> ToObject(XmlElement source, ref IDictionary <string, object> id_source) { if (source is null) { throw new ArgumentNullException(nameof(source)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (source.HasAttribute("id")) { if (id_source.ContainsKey(source.GetAttribute("id"))) { throw new XmlException($"Object with id=\"{source.GetAttribute("id")}\" has already been created."); } } if (source.HasAttribute("ref")) { if (id_source.ContainsKey(source.GetAttribute("ref"))) { if (id_source[source.GetAttribute("ref")] is Group <T> _group) { return(_group); } else { throw new XmlException($"Object with id=\"{source.GetAttribute("id")}\" must be \"{typeof(Group<T>).FullName}\"."); } } } Group <T> group = new Group <T>(); if (source.HasAttribute("id")) { id_source.Add(source.GetAttribute("id"), group); } XmlNodeList xmlEntriesList = source.ChildNodes; int k = -1; for (int i = 0; i < xmlEntriesList.Count; i++) { if (xmlEntriesList[i].Name == "Entries") { if (k == -1) { k = i; } else { throw new XmlException($"XML representation of object of type \"{typeof(Group<T>).FullName}\" must contain one node \"Entries\"."); } } } if (k == -1) { throw new XmlException($"XML representation of object of type \"{typeof(Group<T>).FullName}\" must contain one node \"Entries\"."); } XmlElement xmlEntries = (XmlElement)xmlEntriesList[k]; foreach (XmlElement xmlEntry in xmlEntries.ChildNodes) { if (xmlEntry.Name != "Entry") { throw new XmlException("The XML node \"Entries\" can only contain \"Entry\" nodes."); } else { group.Include(CyclicalMethods.ToObject(xmlEntry, ref id_source)); } } return(group); }
/// <summary> /// Creates an object from its <see cref="XmlElement"/> representation. /// </summary> /// <param name="source"></param> /// <param name="id_source"></param> /// <returns></returns> public static RoutedList <T> ToObject(XmlElement source, ref IDictionary <string, object> id_source) { Type type = typeof(RoutedList <T>); if (source is null) { throw new ArgumentNullException(nameof(source)); } if (id_source is null) { throw new ArgumentNullException(nameof(id_source)); } if (source.HasAttribute("id")) { if (id_source.ContainsKey(source.GetAttribute("id"))) { throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" has already been created."); } } if (source.HasAttribute("ref")) { if (id_source.ContainsKey(source.GetAttribute("ref"))) { if (id_source[source.GetAttribute("ref")] is RoutedList <T> _source) { return(_source); } else { throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" must be \"{type.FullName}\"."); } } else { throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" must have been created earlier."); } } RoutedList <T> routedList = new RoutedList <T>(); if (source.HasAttribute("id")) { id_source.Add(source.GetAttribute("id"), routedList); } XmlNodeList xmlEntriesList = source.ChildNodes; foreach (XmlElement xmlElement in xmlEntriesList) { switch (xmlElement.Name) { case "RouteType": RouteType routeType; if (Enum.TryParse(xmlElement.InnerText, out routeType)) { routedList.RouteType = routeType; } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "RouteMode": RouteMode routeMode; if (Enum.TryParse(xmlElement.InnerText, out routeMode)) { routedList.RouteMode = routeMode; } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "Increase": if (xmlElement.InnerText == bool.TrueString) { routedList._increase = true; } else if (xmlElement.InnerText == bool.FalseString) { routedList._increase = false; } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "CurrentIndex": if (xmlElement.InnerText == "null") { routedList.CurrentIndex = null; } else if (int.TryParse(xmlElement.InnerText, out int currentIndex)) { routedList.CurrentIndex = currentIndex; } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } break; case "Entries": List <T> list = routedList as List <T>; foreach (XmlElement xmlEntry in xmlElement.ChildNodes) { if (xmlEntry.Name != "Entry") { throw new XmlException("The \"Entries\" node can only contain \"Entry\" nodes."); } else if (CyclicalMethods.ToObject(xmlEntry, ref id_source) is T t) { list.Add(t); } else { throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\"."); } } break; } } routedList._increase = routedList.Increase; routedList.Update(); return(routedList); }