private bool swap <T>(T[] buildingBlocks, IBuildingBlockSnapshot originalBuildingBlock, IBuildingBlockSnapshot newBuildingBlock)
        {
            var index = Array.IndexOf(buildingBlocks, originalBuildingBlock);

            if (index < 0)
            {
                return(false);
            }

            buildingBlocks[index] = newBuildingBlock.DowncastTo <T>();
            return(true);
        }
        public bool Swap(IBuildingBlockSnapshot newBuildingBlock)
        {
            if (newBuildingBlock == null)
            {
                return(false);
            }

            var type = newBuildingBlock.BuildingBlockType;
            var name = newBuildingBlock.Name;
            var originalBuildingBlock = BuildingBlockByTypeAndName(type, name);

            if (originalBuildingBlock == null)
            {
                throw new PKSimException(PKSimConstants.Error.CannotFindBuildingBlockInSnapshot(type.ToString(), name, Name));
            }

            switch (type)
            {
            case PKSimBuildingBlockType.Compound:
                return(swap(Compounds, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.Formulation:
                return(swap(Formulations, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.Protocol:
                return(swap(Protocols, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.Individual:
                return(swap(Individuals, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.Population:
                return(swap(Populations, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.Event:
                return(swap(Events, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.ObserverSet:
                return(swap(ObserverSets, originalBuildingBlock, newBuildingBlock));

            case PKSimBuildingBlockType.ExpressionProfile:
                return(swap(ExpressionProfiles, originalBuildingBlock, newBuildingBlock));

            default:
                return(false);
            }
        }