Is the collection contain only item?

Comparisons are done with == Return true if the collection is empty.

assert [1,1,1].has_only(1)         == true
assert [1,2,3].has_only(1)         == false
assert [1..1].has_only(1)          == true
assert [1..3].has_only(1)          == false
assert [3..3[.has_only(1)          == true

ENSURE is_empty implies result == true

Property definitions

core $ Collection :: has_only
	# Is the collection contain only `item`?
	# Comparisons are done with ==
	# Return true if the collection is empty.
	#
	#     assert [1,1,1].has_only(1)         == true
	#     assert [1,2,3].has_only(1)         == false
	#     assert [1..1].has_only(1)          == true
	#     assert [1..3].has_only(1)          == false
	#     assert [3..3[.has_only(1)          == true # empty collection
	#
	# ENSURE `is_empty implies result == true`
	fun has_only(item: nullable Object): Bool
	do
		for i in self do if i != item then return false
		return true
	end
lib/core/collection/abstract_collection.nit:101,2--116,4

core $ Range :: has_only
	#     assert [1..1].has_only(1)
	#     assert not [1..10].has_only(1)
	redef fun has_only(item) do return first == item and item == last or is_empty
lib/core/collection/range.nit:35,2--37,78

pthreads $ ConcurrentCollection :: has_only
	redef fun has_only(e)
	do
		mutex.lock
		var r = real_collection.has_only(e)
		mutex.unlock
		return r
	end
lib/pthreads/concurrent_collections.nit:116,2--122,4

core $ Ref :: has_only
	redef fun has_only(an_item) do return item == an_item
lib/core/collection/abstract_collection.nit:366,2--54

core $ AbstractArrayRead :: has_only
	redef fun has_only(item)
	do
		var i = 0
		var l = length
		while i < l do
			if self[i] != item then return false
			i += 1
		end
		return true
	end
lib/core/collection/array.nit:41,2--50,4

core $ ArrayMapKeys :: has_only
	redef fun has_only(k) do return (self.has(k) and self.length == 1) or self.is_empty
lib/core/collection/array.nit:809,2--84

core $ ArrayMapValues :: has_only
	# O(n)
	redef fun has_only(item)
	do
		for i in self.map._items do if i.second != item then return false
		return true
	end
lib/core/collection/array.nit:838,2--843,4

core $ HashMapKeys :: has_only
	redef fun has_only(k) do return (self.has(k) and self.length == 1) or self.is_empty
lib/core/collection/hash_collection.nit:293,2--84

core $ HashMapValues :: has_only
	redef fun has_only(item)
	do
		var c = self.map._first_item
		while c != null do
			if c._value != item then return false
			c = c._next_item
		end
		return true
	end
lib/core/collection/hash_collection.nit:333,2--341,4

core $ Set :: has_only
	redef fun has_only(item)
	do
		var l = length
		if l == 1 then
			return has(item)
		else if l == 0 then
			return true
		else
			return false
		end
	end
lib/core/collection/abstract_collection.nit:460,2--470,4

core $ List :: has_only
	redef fun has_only(e)
	do
		var node = _head
		while node != null do
			if node.item != e then return false
			node = node.next
		end
		return true
	end
lib/core/collection/list.nit:51,2--59,4