Skip to content

Commit

Permalink
Optimize emptySet/Map/List() in Collections class
Browse files Browse the repository at this point in the history
2012-10-16  Ivan Maidanski  <ivmai@mail.ru>

	* java/util/Collections.java:
	(emptySet(), EmptySet.iterator(), emptyList(), emptyMap(),
	EmptyMap.entrySet(), EmptyMap.keySet(), EmptyMap.values()): Suppress
	"unchecked" warnings.
	(emptySet(), emptyList(), emptyMap()): Don't create new instance (use
	the corresponding immutable container instance); remove FIXME.
	(EmptySet.equals(Object), EmptyList.equals(Object),
	EmptyMap.entrySet(), EmptyMap.equals(Object), EmptyMap.keySet(),
	EmptyMap.values()): Add generic typing.
	(SingletonList.subList(int, int)): Use emptyList() instead of
	EMPTY_LIST (to eliminate "unchecked" warning).
	(SynchronizedCollection.toArray(T[])): Rename T type to E (to
	suppress compiler warning about type hiding).
  • Loading branch information
ivmai committed Oct 16, 2012
1 parent db494a9 commit ef3af4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
16 changes: 16 additions & 0 deletions ChangeLog
@@ -1,3 +1,19 @@
2012-10-16 Ivan Maidanski <ivmai@mail.ru>

* java/util/Collections.java:
(emptySet(), EmptySet.iterator(), emptyList(), emptyMap(),
EmptyMap.entrySet(), EmptyMap.keySet(), EmptyMap.values()): Suppress
"unchecked" warnings.
(emptySet(), emptyList(), emptyMap()): Don't create new instance (use
the corresponding immutable container instance); remove FIXME.
(EmptySet.equals(Object), EmptyList.equals(Object),
EmptyMap.entrySet(), EmptyMap.equals(Object), EmptyMap.keySet(),
EmptyMap.values()): Add generic typing.
(SingletonList.subList(int, int)): Use emptyList() instead of
EMPTY_LIST (to eliminate "unchecked" warning).
(SynchronizedCollection.toArray(T[])): Rename T type to E (to
suppress compiler warning about type hiding).

2012-10-15 Andrew John Hughes <gnu_andrew@member.fsf.org>

* configure.ac: Set to 0.99.1pre, as
Expand Down
32 changes: 18 additions & 14 deletions java/util/Collections.java
Expand Up @@ -120,10 +120,10 @@ private Collections()
* @return an empty parameterized set.
* @since 1.5
*/
@SuppressWarnings("unchecked")
public static final <T> Set<T> emptySet()
{
/* FIXME: Could this be optimized? */
return new EmptySet<T>();
return (Set<T>) EMPTY_SET;
}

/**
Expand Down Expand Up @@ -161,6 +161,7 @@ public int size()
* @return A non-iterating iterator.
*/
// This is really cheating! I think it's perfectly valid, though.
@SuppressWarnings("unchecked")
public Iterator<T> iterator()
{
return (Iterator<T>) EMPTY_LIST.iterator();
Expand Down Expand Up @@ -196,7 +197,7 @@ public boolean containsAll(Collection<?> c)
*/
public boolean equals(Object o)
{
return o instanceof Set && ((Set) o).isEmpty();
return o instanceof Set<?> && ((Set<?>) o).isEmpty();
}

/**
Expand Down Expand Up @@ -288,10 +289,10 @@ public String toString()
* @return an empty parameterized list.
* @since 1.5
*/
@SuppressWarnings("unchecked")
public static final <T> List<T> emptyList()
{
/* FIXME: Could this be optimized? */
return new EmptyList<T>();
return (List<T>) EMPTY_LIST;
}

/**
Expand Down Expand Up @@ -369,7 +370,7 @@ public boolean containsAll(Collection<?> c)
*/
public boolean equals(Object o)
{
return o instanceof List && ((List) o).isEmpty();
return o instanceof List<?> && ((List<?>) o).isEmpty();
}

/**
Expand Down Expand Up @@ -480,10 +481,10 @@ public String toString()
* @return an empty parameterized map.
* @since 1.5
*/
@SuppressWarnings("unchecked")
public static final <K,V> Map<K,V> emptyMap()
{
/* FIXME: Could this be optimized? */
return new EmptyMap<K,V>();
return (Map<K,V>) EMPTY_MAP;
}

/**
Expand Down Expand Up @@ -511,9 +512,10 @@ private static final class EmptyMap<K, V> extends AbstractMap<K, V>
* There are no entries.
* @return The empty set.
*/
@SuppressWarnings("unchecked")
public Set<Map.Entry<K, V>> entrySet()
{
return EMPTY_SET;
return (Set<Map.Entry<K, V>>) EMPTY_SET;
}

// The remaining methods are optional, but provide a performance
Expand Down Expand Up @@ -546,7 +548,7 @@ public boolean containsValue(Object value)
*/
public boolean equals(Object o)
{
return o instanceof Map && ((Map) o).isEmpty();
return o instanceof Map<?,?> && ((Map<?,?>) o).isEmpty();
}

/**
Expand All @@ -572,9 +574,10 @@ public int hashCode()
* No entries.
* @return The empty set.
*/
@SuppressWarnings("unchecked")
public Set<K> keySet()
{
return EMPTY_SET;
return (Set<K>) EMPTY_SET;
}

/**
Expand All @@ -601,9 +604,10 @@ public int size()
* Collection, will work. Besides, that's what the JDK uses!
* @return The empty set.
*/
@SuppressWarnings("unchecked")
public Collection<V> values()
{
return EMPTY_SET;
return (Collection<V>) EMPTY_SET;
}

/**
Expand Down Expand Up @@ -1854,7 +1858,7 @@ public int lastIndexOf(Object o)
public List<T> subList(int from, int to)
{
if (from == to && (to == 0 || to == 1))
return EMPTY_LIST;
return emptyList();
if (from == 0 && to == 1)
return this;
if (from > to)
Expand Down Expand Up @@ -2480,7 +2484,7 @@ public Object[] toArray()
* @throws ArrayStoreException if the type of any element of the
* collection is not a subtype of the element type of a.
*/
public <T> T[] toArray(T[] a)
public <E> E[] toArray(E[] a)
{
synchronized (mutex)
{
Expand Down

0 comments on commit ef3af4f

Please sign in to comment.