I am working on some code and I have come across the following:
private int getPosition() {
List<IDescriptor> fragmentDescriptors = new ArrayList<IDescriptor>(mDescriptors);
int position = -1;
for (IDescriptor fragmentDescriptor : fragmentDescriptors) {
if (fragmentDescriptor instanceof VideoDescriptor) {
position = fragmentDescriptors.indexOf(fragmentDescriptor);
}
}
return position;
}
In a situation like this, how can one find an index of a specific implementation within a list without using instanceOf
?
Update:
Few points: mDesciptors
is a set. The point of the question was: The set of IDescriptor
is injected into the class, the class has no idea what the set will contain, it does however know there maybe a IDescriptor
that is meant for video.. the problem is the current method relies on VideoDescriptor
, therefore if that implementation changes, the position will no longer be found.
I'd like a general review of this. The method was really just to demonstrate the dependency on VideoDescriptor
.
mDescriptors
is aSet
, what's the meaning ofgetPosition()
? ASet
is not ordered... – Uri Agassi yesterday