Remove Namespace.Builder.nextId()
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / utils / serializer / Namespace.java
index 1d16920a672c1829583568a184837aef5a54a6f3..6942ba27138a8274443ce925c1a62b01799ff9c1 100644 (file)
@@ -26,9 +26,9 @@ import com.esotericsoftware.kryo.pool.KryoPool;
 import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
-import org.apache.commons.lang3.tuple.Pair;
 import org.objenesis.strategy.StdInstantiatorStrategy;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
@@ -37,10 +37,11 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Objects;
 
 import static java.util.Objects.requireNonNull;
-import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Pool of Kryo instances, with classes pre-registered.
@@ -63,16 +64,16 @@ public final class Namespace implements KryoFactory, KryoPool {
   /**
    * ID to use if this KryoNamespace does not define registration id.
    */
-  public static final int FLOATING_ID = -1;
+  private static final int FLOATING_ID = -1;
 
   /**
    * Smallest ID free to use for user defined registrations.
    */
-  public static final int INITIAL_ID = 16;
+  private static final int INITIAL_ID = 16;
 
   static final String NO_NAME = "(no name)";
 
-  private static final Logger LOGGER = getLogger(Namespace.class);
+  private static final Logger LOGGER = LoggerFactory.getLogger(Namespace.class);
 
   /**
    * Default Kryo namespace.
@@ -99,7 +100,7 @@ public final class Namespace implements KryoFactory, KryoPool {
   //@NotThreadSafe
   public static final class Builder {
     private int blockHeadId = INITIAL_ID;
-    private List<Pair<Class<?>[], Serializer<?>>> types = new ArrayList<>();
+    private List<Entry<Class<?>[], Serializer<?>>> types = new ArrayList<>();
     private List<RegistrationBlock> blocks = new ArrayList<>();
     private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
     private boolean registrationRequired = true;
@@ -127,43 +128,6 @@ public final class Namespace implements KryoFactory, KryoPool {
       return new Namespace(blocks, classLoader, registrationRequired, compatible, friendlyName).populate(1);
     }
 
-    /**
-     * Sets the next Kryo registration Id for following register entries.
-     *
-     * @param id Kryo registration Id
-     * @return this
-     * @see Kryo#register(Class, Serializer, int)
-     */
-    public Builder nextId(final int id) {
-      if (!types.isEmpty()) {
-        if (id != FLOATING_ID && id < blockHeadId + types.size()) {
-
-          if (LOGGER.isWarnEnabled()) {
-            LOGGER.warn("requested nextId {} could potentially overlap "
-                    + "with existing registrations {}+{} ",
-                id, blockHeadId, types.size(), new RuntimeException());
-          }
-        }
-        blocks.add(new RegistrationBlock(this.blockHeadId, types));
-        types = new ArrayList<>();
-      }
-      this.blockHeadId = id;
-      return this;
-    }
-
-    /**
-     * Registers classes to be serialized using Kryo default serializer.
-     *
-     * @param expectedTypes list of classes
-     * @return this
-     */
-    public Builder register(final Class<?>... expectedTypes) {
-      for (Class<?> clazz : expectedTypes) {
-        types.add(Pair.of(new Class<?>[]{clazz}, null));
-      }
-      return this;
-    }
-
     /**
      * Registers serializer for the given set of classes.
      * <p>
@@ -175,42 +139,7 @@ public final class Namespace implements KryoFactory, KryoPool {
      * @return this
      */
     public Builder register(Serializer<?> serializer, final Class<?>... classes) {
-      types.add(Pair.of(classes, requireNonNull(serializer)));
-      return this;
-    }
-
-    private Builder register(RegistrationBlock block) {
-      if (block.begin() != FLOATING_ID) {
-        // flush pending types
-        nextId(block.begin());
-        blocks.add(block);
-        nextId(block.begin() + block.types().size());
-      } else {
-        // flush pending types
-        final int addedBlockBegin = blockHeadId + types.size();
-        nextId(addedBlockBegin);
-        blocks.add(new RegistrationBlock(addedBlockBegin, block.types()));
-        nextId(addedBlockBegin + block.types().size());
-      }
-      return this;
-    }
-
-    /**
-     * Registers all the class registered to given KryoNamespace.
-     *
-     * @param ns KryoNamespace
-     * @return this
-     */
-    public Builder register(final Namespace ns) {
-
-      if (blocks.containsAll(ns.registeredBlocks)) {
-        // Everything was already registered.
-        LOGGER.debug("Ignoring {}, already registered.", ns);
-        return this;
-      }
-      for (RegistrationBlock block : ns.registeredBlocks) {
-        this.register(block);
-      }
+      types.add(Map.entry(classes, serializer));
       return this;
     }
 
@@ -479,8 +408,8 @@ public final class Namespace implements KryoFactory, KryoPool {
       if (id == FLOATING_ID) {
         id = kryo.getNextRegistrationId();
       }
-      for (Pair<Class<?>[], Serializer<?>> entry : block.types()) {
-        register(kryo, entry.getLeft(), entry.getRight(), id++);
+      for (Entry<Class<?>[], Serializer<?>> entry : block.types()) {
+        register(kryo, entry.getKey(), entry.getValue(), id++);
       }
     }
     return kryo;
@@ -568,9 +497,9 @@ public final class Namespace implements KryoFactory, KryoPool {
 
   static final class RegistrationBlock {
     private final int begin;
-    private final ImmutableList<Pair<Class<?>[], Serializer<?>>> types;
+    private final ImmutableList<Entry<Class<?>[], Serializer<?>>> types;
 
-    RegistrationBlock(int begin, List<Pair<Class<?>[], Serializer<?>>> types) {
+    RegistrationBlock(int begin, List<Entry<Class<?>[], Serializer<?>>> types) {
       this.begin = begin;
       this.types = ImmutableList.copyOf(types);
     }
@@ -579,7 +508,7 @@ public final class Namespace implements KryoFactory, KryoPool {
       return begin;
     }
 
-    public ImmutableList<Pair<Class<?>[], Serializer<?>>> types() {
+    public ImmutableList<Entry<Class<?>[], Serializer<?>>> types() {
       return types;
     }