Eliminate atomix.utils.memory
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / utils / serializer / SerializerBuilder.java
1 /*
2  * Copyright 2018-present Open Networking Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.utils.serializer;
17
18 /**
19  * Serializer builder.
20  */
21 public class SerializerBuilder {
22   private final String name;
23   private final Namespace.Builder namespaceBuilder = Namespace.builder()
24       .register(Namespaces.BASIC)
25       .nextId(Namespaces.BEGIN_USER_CUSTOM_ID);
26
27   public SerializerBuilder() {
28     this(null);
29   }
30
31   public SerializerBuilder(String name) {
32     this.name = name;
33   }
34
35   /**
36    * Requires explicit serializable type registration for serializable types.
37    *
38    * @return the serializer builder
39    */
40   public SerializerBuilder withRegistrationRequired() {
41     return withRegistrationRequired(true);
42   }
43
44   /**
45    * Sets whether serializable type registration is required for serializable types.
46    *
47    * @param registrationRequired whether serializable type registration is required for serializable types
48    * @return the serializer builder
49    */
50   public SerializerBuilder withRegistrationRequired(boolean registrationRequired) {
51     namespaceBuilder.setRegistrationRequired(registrationRequired);
52     return this;
53   }
54
55   /**
56    * Enables compatible serialization for serializable types.
57    *
58    * @return the serializer builder
59    */
60   public SerializerBuilder withCompatibleSerialization() {
61     return withCompatibleSerialization(true);
62   }
63
64   /**
65    * Sets whether compatible serialization is enabled for serializable types.
66    *
67    * @param compatibleSerialization whether compatible serialization is enabled for user types
68    * @return the serializer builder
69    */
70   public SerializerBuilder withCompatibleSerialization(boolean compatibleSerialization) {
71     namespaceBuilder.setCompatible(compatibleSerialization);
72     return this;
73   }
74
75   /**
76    * Adds a namespace to the serializer.
77    *
78    * @param namespace the namespace to add
79    * @return the serializer builder
80    */
81   public SerializerBuilder withNamespace(Namespace namespace) {
82     namespaceBuilder.register(namespace);
83     return this;
84   }
85
86   /**
87    * Sets the serializable types.
88    *
89    * @param types the types to register
90    * @return the serializer builder
91    */
92   public SerializerBuilder withTypes(Class<?>... types) {
93     namespaceBuilder.register(types);
94     return this;
95   }
96
97   /**
98    * Adds a serializable type to the builder.
99    *
100    * @param type the type to add
101    * @return the serializer builder
102    */
103   public SerializerBuilder addType(Class<?> type) {
104     namespaceBuilder.register(type);
105     return this;
106   }
107
108   /**
109    * Adds a serializer to the builder.
110    *
111    * @param serializer the serializer to add
112    * @param types the serializable types
113    * @return the serializer builder
114    */
115   public SerializerBuilder addSerializer(com.esotericsoftware.kryo.Serializer<?> serializer, Class<?>... types) {
116     namespaceBuilder.register(serializer, types);
117     return this;
118   }
119
120   /**
121    * Build the {@link Serializer}.
122    *
123    * @return A new {@link Serializer}.
124    */
125   public Serializer build() {
126     return Serializer.using(name != null ? namespaceBuilder.build(name) : namespaceBuilder.build());
127   }
128 }