Classification with the nearest neighbor rule in general finite dimensional spaces: necessary and sufficient conditions