Skip to content

Commit e238fdc

Browse files
committed
Reduce the number of classes in j.u.Collections.
Many methods of j.u.Collections wrapping collections specify that they return a serializable collection if the wrapped collection is serializable. The previous implementation would explicitly test whether it was an instance of Serializable, and return different classes inheriting from Serializable or not depending on that. That is unnecessary. To be serializable, a class need not only be an instance of Serializable. It must also contain only serializable fields. Since the wrapped collection is captured by the wrapper, it is a field, and the serializable property is carried over. We can therefore always return collections that are instances of Serializable, which reduces the number of classes in Collections. This analysis is confirmed by experimentation on the JVM. Those methods always return instances of Serializable.
1 parent 5bd1e50 commit e238fdc

File tree

1 file changed

+53
-161
lines changed

1 file changed

+53
-161
lines changed

javalib/src/main/scala/java/util/Collections.scala

Lines changed: 53 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -336,195 +336,91 @@ object Collections {
336336
}
337337
}
338338

339-
def unmodifiableCollection[T](c: Collection[_ <: T]): Collection[T] = {
340-
class BasicImmutableCollection
341-
extends ImmutableCollection[T, Collection[T]](c.asInstanceOf[Collection[T]]) {
339+
def unmodifiableCollection[T](c: Collection[_ <: T]): Collection[T] =
340+
new ImmutableCollection[T, Collection[T]](c.asInstanceOf[Collection[T]])
342341

343-
override def equals(obj: Any): Boolean =
344-
this eq obj.asInstanceOf[AnyRef]
342+
def unmodifiableSet[T](a: Set[_ <: T]): Set[T] =
343+
new ImmutableSet[T, Set[T]](a.asInstanceOf[Set[T]])
345344

346-
override def hashCode(): Int =
347-
System.identityHashCode(this)
348-
}
349-
c match {
350-
case _: Serializable => new BasicImmutableCollection with Serializable
351-
case _ => new BasicImmutableCollection
352-
}
353-
}
354-
355-
def unmodifiableSet[T](a: Set[_ <: T]): Set[T] = {
356-
class BasicImmutableSet extends ImmutableSet[T, Set[T]](a.asInstanceOf[Set[T]])
357-
a match {
358-
case _: Serializable => new BasicImmutableSet with Serializable
359-
case _ => new BasicImmutableSet
360-
}
361-
}
362-
363-
def unmodifiableSortedSet[T](s: SortedSet[T]): SortedSet[T] = {
364-
class BasicImmutableSortedSet extends ImmutableSortedSet[T](s)
365-
s match {
366-
case _: Serializable => new BasicImmutableSortedSet with Serializable
367-
case _ => new BasicImmutableSortedSet
368-
}
369-
}
345+
def unmodifiableSortedSet[T](s: SortedSet[T]): SortedSet[T] =
346+
new ImmutableSortedSet[T](s)
370347

371348
def unmodifiableList[T](list: List[_ <: T]): List[T] = {
372-
class BasicImmutableList extends ImmutableList[T](list.asInstanceOf[List[T]])
373349
list match {
374-
case _: Serializable with RandomAccess =>
375-
new BasicImmutableList with Serializable with RandomAccess
376-
377-
case _: Serializable => new BasicImmutableList with Serializable
378-
case _: RandomAccess => new BasicImmutableList with RandomAccess
379-
case _ => new BasicImmutableList
350+
case _: RandomAccess =>
351+
new ImmutableList[T](list.asInstanceOf[List[T]]) with RandomAccess
352+
case _ =>
353+
new ImmutableList[T](list.asInstanceOf[List[T]])
380354
}
381355
}
382356

383-
def unmodifiableMap[K, V](m: Map[_ <: K, _ <: V]): Map[K, V] = {
384-
class BasicImmutableMap
385-
extends ImmutableMap[K, V, Map[K, V]](m.asInstanceOf[Map[K, V]])
386-
m match {
387-
case _: Serializable => new BasicImmutableMap with Serializable
388-
case _ => new BasicImmutableMap
389-
}
390-
}
357+
def unmodifiableMap[K, V](m: Map[_ <: K, _ <: V]): Map[K, V] =
358+
new ImmutableMap[K, V, Map[K, V]](m.asInstanceOf[Map[K, V]])
391359

392-
def unmodifiableSortedMap[K, V](m: SortedMap[K, _ <: V]): SortedMap[K, V] = {
393-
class BasicImmutableSortedMap
394-
extends ImmutableSortedMap[K, V](m.asInstanceOf[SortedMap[K, V]])
395-
m match {
396-
case _: Serializable => new BasicImmutableSortedMap with Serializable
397-
case _ => new BasicImmutableSortedMap
398-
}
399-
}
360+
def unmodifiableSortedMap[K, V](m: SortedMap[K, _ <: V]): SortedMap[K, V] =
361+
new ImmutableSortedMap[K, V](m.asInstanceOf[SortedMap[K, V]])
400362

401363
def synchronizedCollection[T](c: Collection[T]): Collection[T] = {
402-
class BasicSynchronizedCollection extends WrappedCollection[T, Collection[T]] {
364+
new WrappedCollection[T, Collection[T]] {
403365
override protected val inner: Collection[T] = c
404-
405-
override def equals(obj: Any): Boolean =
406-
this eq obj.asInstanceOf[AnyRef]
407-
408-
override def hashCode(): Int =
409-
System.identityHashCode(this)
410-
}
411-
c match {
412-
case _: Serializable => new BasicSynchronizedCollection with Serializable
413-
case _ => new BasicSynchronizedCollection
414366
}
415367
}
416368

417369
def synchronizedSet[T](s: Set[T]): Set[T] = {
418-
class BasicSynchronizedSet extends WrappedSet[T, Set[T]] {
370+
new WrappedSet[T, Set[T]] {
419371
override protected val inner: Set[T] = s
420372
}
421-
s match {
422-
case _: Serializable => new BasicSynchronizedSet with Serializable
423-
case _ => new BasicSynchronizedSet
424-
}
425373
}
426374

427375
def synchronizedSortedSet[T](s: SortedSet[T]): SortedSet[T] = {
428-
class BasicSynchronizedSortedSet extends WrappedSortedSet[T] {
376+
new WrappedSortedSet[T] {
429377
override protected val inner: SortedSet[T] = s
430378
}
431-
s match {
432-
case _: Serializable => new BasicSynchronizedSortedSet with Serializable
433-
case _ => new BasicSynchronizedSortedSet
434-
}
435379
}
436380

437381
def synchronizedList[T](list: List[T]): List[T] = {
438382
class BasicSynchronizedList extends WrappedList[T] {
439383
override protected val inner: List[T] = list
440384
}
441385
list match {
442-
case _: Serializable with RandomAccess =>
443-
new BasicSynchronizedList with Serializable with RandomAccess
444-
445-
case _: Serializable => new BasicSynchronizedList with Serializable
446386
case _: RandomAccess => new BasicSynchronizedList with RandomAccess
447387
case _ => new BasicSynchronizedList
448388
}
449389
}
450390

451391
def synchronizedMap[K, V](m: Map[K, V]): Map[K, V] = {
452-
class BasicSynchronizedMap extends WrappedMap[K, V, Map[K, V]] {
392+
new WrappedMap[K, V, Map[K, V]] {
453393
override protected val inner: Map[K, V] = m
454394
}
455-
m match {
456-
case _: Serializable => new BasicSynchronizedMap with Serializable
457-
case _ => new BasicSynchronizedMap
458-
}
459395
}
460396

461397
def synchronizedSortedMap[K, V](m: SortedMap[K, V]): SortedMap[K, V] = {
462-
class BasicSynchronizedSortedMap extends WrappedSortedMap[K, V] {
398+
new WrappedSortedMap[K, V] {
463399
override protected val inner: SortedMap[K, V] = m
464400
}
465-
m match {
466-
case _: Serializable => new BasicSynchronizedSortedMap with Serializable
467-
case _ => new BasicSynchronizedSortedMap
468-
}
469401
}
470402

471-
def checkedCollection[E](c: Collection[E], typ: Class[E]): Collection[E] = {
472-
class BasicCheckedCollection extends CheckedCollection[E, Collection[E]](c, typ) {
473-
override def equals(obj: Any): Boolean =
474-
this eq obj.asInstanceOf[AnyRef]
403+
def checkedCollection[E](c: Collection[E], typ: Class[E]): Collection[E] =
404+
new CheckedCollection[E, Collection[E]](c, typ)
475405

476-
override def hashCode(): Int =
477-
System.identityHashCode(this)
478-
}
479-
c match {
480-
case _: Serializable => new BasicCheckedCollection with Serializable
481-
case _ => new BasicCheckedCollection
482-
}
483-
}
484-
485-
def checkedSet[E](s: Set[E], typ: Class[E]): Set[E] = {
486-
class BasicCheckedSet extends CheckedSet[E, Set[E]](s, typ)
487-
s match {
488-
case _: Serializable => new BasicCheckedSet with Serializable
489-
case _ => new BasicCheckedSet
490-
}
491-
}
406+
def checkedSet[E](s: Set[E], typ: Class[E]): Set[E] =
407+
new CheckedSet[E, Set[E]](s, typ)
492408

493-
def checkedSortedSet[E](s: SortedSet[E], typ: Class[E]): SortedSet[E] = {
494-
class BasicCheckedSortedSet extends CheckedSortedSet[E](s, typ)
495-
s match {
496-
case _: Serializable => new BasicCheckedSortedSet with Serializable
497-
case _ => new BasicCheckedSortedSet
498-
}
499-
}
409+
def checkedSortedSet[E](s: SortedSet[E], typ: Class[E]): SortedSet[E] =
410+
new CheckedSortedSet[E](s, typ)
500411

501412
def checkedList[E](list: List[E], typ: Class[E]): List[E] = {
502-
class BasicCheckedList extends CheckedList[E](list, typ)
503413
list match {
504-
case _: Serializable with RandomAccess =>
505-
new BasicCheckedList with Serializable with RandomAccess
506-
507-
case _: Serializable => new BasicCheckedList with Serializable
508-
case _: RandomAccess => new BasicCheckedList with RandomAccess
509-
case _ => new BasicCheckedList
414+
case _: RandomAccess => new CheckedList[E](list, typ) with RandomAccess
415+
case _ => new CheckedList[E](list, typ)
510416
}
511417
}
512418

513-
def checkedMap[K, V](m: Map[K, V], keyType: Class[K], valueType: Class[V]): Map[K, V] = {
514-
class BasicCheckedMap extends CheckedMap[K, V, Map[K, V]](m, keyType, valueType)
515-
m match {
516-
case _: Serializable => new BasicCheckedMap with Serializable
517-
case _ => new BasicCheckedMap
518-
}
519-
}
419+
def checkedMap[K, V](m: Map[K, V], keyType: Class[K], valueType: Class[V]): Map[K, V] =
420+
new CheckedMap[K, V, Map[K, V]](m, keyType, valueType)
520421

521-
def checkedSortedMap[K, V](m: SortedMap[K, V], keyType: Class[K], valueType: Class[V]): SortedMap[K, V] = {
522-
class BasicCheckedSortedMap extends CheckedSortedMap[K, V](m, keyType, valueType)
523-
m match {
524-
case _: Serializable => new BasicCheckedSortedMap with Serializable
525-
case _ => new BasicCheckedSortedMap
526-
}
527-
}
422+
def checkedSortedMap[K, V](m: SortedMap[K, V], keyType: Class[K], valueType: Class[V]): SortedMap[K, V] =
423+
new CheckedSortedMap[K, V](m, keyType, valueType)
528424

529425
def emptyIterator[T](): Iterator[T] =
530426
EMPTY_ITERATOR.asInstanceOf[Iterator[T]]
@@ -601,20 +497,15 @@ object Collections {
601497
}
602498

603499
def reverseOrder[T](): Comparator[T] = {
604-
val cmp = new Comparator[T] {
605-
def compare(o1: T, o2: T): Int = o1.asInstanceOf[Comparable[T]].compareTo(o2)
500+
new Comparator[T] with Serializable {
501+
def compare(o1: T, o2: T): Int = o2.asInstanceOf[Comparable[T]].compareTo(o1)
606502
}
607-
reverseOrder(cmp)
608503
}
609504

610505
def reverseOrder[T](cmp: Comparator[T]): Comparator[T] = {
611-
class ReverseComparator extends Comparator[T] {
506+
new Comparator[T] with Serializable {
612507
override def compare(o1: T, o2: T): Int = cmp.compare(o2, o1)
613508
}
614-
cmp match {
615-
case _: Serializable => new ReverseComparator with Serializable
616-
case _ => new ReverseComparator
617-
}
618509
}
619510

620511
def enumeration[T](c: Collection[T]): Enumeration[T] = {
@@ -668,7 +559,7 @@ object Collections {
668559

669560
@inline
670561
private def naturalComparator[T <: jl.Comparable[T]]: Comparator[T] = {
671-
new Comparator[T] {
562+
new Comparator[T] with Serializable {
672563
final def compare(o1: T, o2: T): Int = o1.compareTo(o2)
673564
}
674565
}
@@ -680,8 +571,18 @@ object Collections {
680571
}
681572
}
682573

574+
private trait WrappedEquals {
575+
protected def inner: AnyRef
576+
577+
override def equals(obj: Any): Boolean =
578+
inner.equals(obj)
579+
580+
override def hashCode(): Int =
581+
inner.hashCode
582+
}
583+
683584
private trait WrappedCollection[E, Coll <: Collection[E]]
684-
extends Collection[E] {
585+
extends Collection[E] with Serializable {
685586

686587
protected def inner: Coll
687588

@@ -724,21 +625,16 @@ object Collections {
724625
def clear(): Unit =
725626
inner.clear()
726627

727-
override def equals(obj: Any): Boolean =
728-
inner.equals(obj)
729-
730-
override def hashCode(): Int =
731-
inner.hashCode
732-
733628
override def toString: String =
734629
inner.toString
735630
}
736631

737632
private trait WrappedSet[E, Coll <: Set[E]]
738-
extends WrappedCollection[E, Coll] with Set[E]
633+
extends WrappedEquals with WrappedCollection[E, Coll] with Set[E]
739634

740635
private trait WrappedSortedSet[E]
741-
extends WrappedCollection[E, SortedSet[E]] with SortedSet[E] {
636+
extends WrappedSet[E, SortedSet[E]] with SortedSet[E] {
637+
742638
def comparator(): Comparator[_ >: E] =
743639
inner.comparator()
744640

@@ -759,7 +655,7 @@ object Collections {
759655
}
760656

761657
private trait WrappedList[E]
762-
extends WrappedCollection[E, List[E]] with List[E] {
658+
extends WrappedEquals with WrappedCollection[E, List[E]] with List[E] {
763659

764660
def addAll(index: Int, c: Collection[_ <: E]): Boolean =
765661
inner.addAll(index, c)
@@ -792,7 +688,9 @@ object Collections {
792688
inner.subList(fromIndex, toIndex)
793689
}
794690

795-
private trait WrappedMap[K, V, M <: Map[K, V]] extends Map[K, V] {
691+
private trait WrappedMap[K, V, M <: Map[K, V]]
692+
extends WrappedEquals with Map[K, V] {
693+
796694
protected def inner: M
797695

798696
def size(): Int =
@@ -831,12 +729,6 @@ object Collections {
831729
def entrySet(): Set[Map.Entry[K, V]] =
832730
inner.entrySet.asInstanceOf[Set[Map.Entry[K, V]]]
833731

834-
override def equals(obj: Any): Boolean =
835-
inner.equals(obj)
836-
837-
override def hashCode(): Int =
838-
inner.hashCode
839-
840732
override def toString(): String =
841733
inner.toString
842734
}

0 commit comments

Comments
 (0)