Make Comparators Serializable

Motivation:

Objects of java.util.TreeMap or java.util.TreeSet will become
non-Serializable if instantiated with Comparators, which are not also
 Serializable. This can result in unexpected and difficult-to-diagnose
 bugs.

Modifications:

Implements Serializable for all classes, which implements Comparator.

Result:

Proper Comparators which will not force collections to
non-Serializable mode.
This commit is contained in:
Idel Pivnitskiy 2017-10-14 07:28:54 -07:00 committed by Norman Maurer
parent 50a067a8f7
commit 4793daa589
2 changed files with 11 additions and 3 deletions

View File

@ -25,6 +25,7 @@ import io.netty.util.internal.PriorityQueueNode;
import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.UnstableApi; import io.netty.util.internal.UnstableApi;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
@ -399,7 +400,9 @@ public final class WeightedFairQueueByteDistributor implements StreamByteDistrib
* <li>Stream ID (higher stream ID is higher priority - used for tie breaker)</li> * <li>Stream ID (higher stream ID is higher priority - used for tie breaker)</li>
* </ul> * </ul>
*/ */
private static final class StateOnlyComparator implements Comparator<State> { private static final class StateOnlyComparator implements Comparator<State>, Serializable {
private static final long serialVersionUID = -4806936913002105966L;
static final StateOnlyComparator INSTANCE = new StateOnlyComparator(); static final StateOnlyComparator INSTANCE = new StateOnlyComparator();
private StateOnlyComparator() { private StateOnlyComparator() {
@ -426,7 +429,9 @@ public final class WeightedFairQueueByteDistributor implements StreamByteDistrib
} }
} }
private static final class StatePseudoTimeComparator implements Comparator<State> { private static final class StatePseudoTimeComparator implements Comparator<State>, Serializable {
private static final long serialVersionUID = -1437548640227161828L;
static final StatePseudoTimeComparator INSTANCE = new StatePseudoTimeComparator(); static final StatePseudoTimeComparator INSTANCE = new StatePseudoTimeComparator();
private StatePseudoTimeComparator() { private StatePseudoTimeComparator() {

View File

@ -17,6 +17,7 @@ package io.netty.util.internal;
import org.junit.Test; import org.junit.Test;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -245,7 +246,9 @@ public class DefaultPriorityQueueTest {
assertTrue(queue.isEmpty()); assertTrue(queue.isEmpty());
} }
private static final class TestElementComparator implements Comparator<TestElement> { private static final class TestElementComparator implements Comparator<TestElement>, Serializable {
private static final long serialVersionUID = 7930368853384760103L;
static final TestElementComparator INSTANCE = new TestElementComparator(); static final TestElementComparator INSTANCE = new TestElementComparator();
private TestElementComparator() { private TestElementComparator() {