@@ -336,195 +336,91 @@ object Collections {
336
336
}
337
337
}
338
338
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 ]])
342
341
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 ]])
345
344
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)
370
347
371
348
def unmodifiableList [T ](list : List [_ <: T ]): List [T ] = {
372
- class BasicImmutableList extends ImmutableList [T ](list.asInstanceOf [List [T ]])
373
349
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 ]])
380
354
}
381
355
}
382
356
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 ]])
391
359
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 ]])
400
362
401
363
def synchronizedCollection [T ](c : Collection [T ]): Collection [T ] = {
402
- class BasicSynchronizedCollection extends WrappedCollection [T , Collection [T ]] {
364
+ new WrappedCollection [T , Collection [T ]] {
403
365
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
414
366
}
415
367
}
416
368
417
369
def synchronizedSet [T ](s : Set [T ]): Set [T ] = {
418
- class BasicSynchronizedSet extends WrappedSet [T , Set [T ]] {
370
+ new WrappedSet [T , Set [T ]] {
419
371
override protected val inner : Set [T ] = s
420
372
}
421
- s match {
422
- case _ : Serializable => new BasicSynchronizedSet with Serializable
423
- case _ => new BasicSynchronizedSet
424
- }
425
373
}
426
374
427
375
def synchronizedSortedSet [T ](s : SortedSet [T ]): SortedSet [T ] = {
428
- class BasicSynchronizedSortedSet extends WrappedSortedSet [T ] {
376
+ new WrappedSortedSet [T ] {
429
377
override protected val inner : SortedSet [T ] = s
430
378
}
431
- s match {
432
- case _ : Serializable => new BasicSynchronizedSortedSet with Serializable
433
- case _ => new BasicSynchronizedSortedSet
434
- }
435
379
}
436
380
437
381
def synchronizedList [T ](list : List [T ]): List [T ] = {
438
382
class BasicSynchronizedList extends WrappedList [T ] {
439
383
override protected val inner : List [T ] = list
440
384
}
441
385
list match {
442
- case _ : Serializable with RandomAccess =>
443
- new BasicSynchronizedList with Serializable with RandomAccess
444
-
445
- case _ : Serializable => new BasicSynchronizedList with Serializable
446
386
case _ : RandomAccess => new BasicSynchronizedList with RandomAccess
447
387
case _ => new BasicSynchronizedList
448
388
}
449
389
}
450
390
451
391
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 ]] {
453
393
override protected val inner : Map [K , V ] = m
454
394
}
455
- m match {
456
- case _ : Serializable => new BasicSynchronizedMap with Serializable
457
- case _ => new BasicSynchronizedMap
458
- }
459
395
}
460
396
461
397
def synchronizedSortedMap [K , V ](m : SortedMap [K , V ]): SortedMap [K , V ] = {
462
- class BasicSynchronizedSortedMap extends WrappedSortedMap [K , V ] {
398
+ new WrappedSortedMap [K , V ] {
463
399
override protected val inner : SortedMap [K , V ] = m
464
400
}
465
- m match {
466
- case _ : Serializable => new BasicSynchronizedSortedMap with Serializable
467
- case _ => new BasicSynchronizedSortedMap
468
- }
469
401
}
470
402
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)
475
405
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)
492
408
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)
500
411
501
412
def checkedList [E ](list : List [E ], typ : Class [E ]): List [E ] = {
502
- class BasicCheckedList extends CheckedList [E ](list, typ)
503
413
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)
510
416
}
511
417
}
512
418
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)
520
421
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)
528
424
529
425
def emptyIterator [T ](): Iterator [T ] =
530
426
EMPTY_ITERATOR .asInstanceOf [Iterator [T ]]
@@ -601,20 +497,15 @@ object Collections {
601
497
}
602
498
603
499
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 )
606
502
}
607
- reverseOrder(cmp)
608
503
}
609
504
610
505
def reverseOrder [T ](cmp : Comparator [T ]): Comparator [T ] = {
611
- class ReverseComparator extends Comparator [T ] {
506
+ new Comparator [T ] with Serializable {
612
507
override def compare (o1 : T , o2 : T ): Int = cmp.compare(o2, o1)
613
508
}
614
- cmp match {
615
- case _ : Serializable => new ReverseComparator with Serializable
616
- case _ => new ReverseComparator
617
- }
618
509
}
619
510
620
511
def enumeration [T ](c : Collection [T ]): Enumeration [T ] = {
@@ -668,7 +559,7 @@ object Collections {
668
559
669
560
@ inline
670
561
private def naturalComparator [T <: jl.Comparable [T ]]: Comparator [T ] = {
671
- new Comparator [T ] {
562
+ new Comparator [T ] with Serializable {
672
563
final def compare (o1 : T , o2 : T ): Int = o1.compareTo(o2)
673
564
}
674
565
}
@@ -680,8 +571,18 @@ object Collections {
680
571
}
681
572
}
682
573
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
+
683
584
private trait WrappedCollection [E , Coll <: Collection [E ]]
684
- extends Collection [E ] {
585
+ extends Collection [E ] with Serializable {
685
586
686
587
protected def inner : Coll
687
588
@@ -724,21 +625,16 @@ object Collections {
724
625
def clear (): Unit =
725
626
inner.clear()
726
627
727
- override def equals (obj : Any ): Boolean =
728
- inner.equals(obj)
729
-
730
- override def hashCode (): Int =
731
- inner.hashCode
732
-
733
628
override def toString : String =
734
629
inner.toString
735
630
}
736
631
737
632
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 ]
739
634
740
635
private trait WrappedSortedSet [E ]
741
- extends WrappedCollection [E , SortedSet [E ]] with SortedSet [E ] {
636
+ extends WrappedSet [E , SortedSet [E ]] with SortedSet [E ] {
637
+
742
638
def comparator (): Comparator [_ >: E ] =
743
639
inner.comparator()
744
640
@@ -759,7 +655,7 @@ object Collections {
759
655
}
760
656
761
657
private trait WrappedList [E ]
762
- extends WrappedCollection [E , List [E ]] with List [E ] {
658
+ extends WrappedEquals with WrappedCollection [E , List [E ]] with List [E ] {
763
659
764
660
def addAll (index : Int , c : Collection [_ <: E ]): Boolean =
765
661
inner.addAll(index, c)
@@ -792,7 +688,9 @@ object Collections {
792
688
inner.subList(fromIndex, toIndex)
793
689
}
794
690
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
+
796
694
protected def inner : M
797
695
798
696
def size (): Int =
@@ -831,12 +729,6 @@ object Collections {
831
729
def entrySet (): Set [Map .Entry [K , V ]] =
832
730
inner.entrySet.asInstanceOf [Set [Map .Entry [K , V ]]]
833
731
834
- override def equals (obj : Any ): Boolean =
835
- inner.equals(obj)
836
-
837
- override def hashCode (): Int =
838
- inner.hashCode
839
-
840
732
override def toString (): String =
841
733
inner.toString
842
734
}
0 commit comments