Perhaps `Container container = resource is Container ? resource : resource.parent;`?