using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using mapper.attributes; namespace sharpwawi.models { [MappingConstraintAttribute(FieldMappingConstraint = MappingConstraints.DONTMAP,PropertyMappingConstraint = MappingConstraints.ANNOTATEDONLY)] public class PartAssembly { [Mapped] [PrimaryKey] public Part Part; [Mapped] [PrimaryKey] public int Revision; IList components = new List(); protected PartAssembly() { } internal PartAssembly(Part part) { Part = part; Revision = part.HighestAssemblyRevision + 1; } public int Count => components.Count; public AssemblyComponent this[int n] => components[n]; public AssemblyComponent this[Part component]{ get { foreach (AssemblyComponent assemblyComponent in components) { if (assemblyComponent.Component.Equals(component)) return assemblyComponent; } throw new IndexOutOfRangeException(); } } public bool ContainsComponent(Part component){ foreach (AssemblyComponent assemblyComponent in components){ if (assemblyComponent.Component.Equals(component)) return true; } return false; } public PartAssembly Clone(){ PartAssembly clone = Part.CreateAssembly(); foreach (AssemblyComponent assemblyComponent in components) clone.AddComponent(assemblyComponent.Component, assemblyComponent.Quantity); return clone; } /** * Add a qunatity of a component to the Assembly. * Returns true if the component is new to the assembly, false if just the quantity changed * **/ public bool AddComponent(Part component, double quantity) { if (ContainsComponent(component)) { this[component].Quantity += quantity; return false; } else { components.Add(new AssemblyComponent(this, component, quantity)); return true; } } public void RemoveComponent(AssemblyComponent component) { components.Remove(component); } public void RemoveComponent(Part component){ RemoveComponent(this[component]); } } }