Remove most of atomix.utils.*
[controller.git] / third-party / atomix / utils / 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 import io.atomix.utils.Builder;
19
20 /**
21  * Serializer builder.
22  */
23 public class SerializerBuilder implements Builder<Serializer> {
24   private final String name;
25   private final Namespace.Builder namespaceBuilder = Namespace.builder()
26       .register(Namespaces.BASIC)
27       .nextId(Namespaces.BEGIN_USER_CUSTOM_ID);
28
29   public SerializerBuilder() {
30     this(null);
31   }
32
33   public SerializerBuilder(String name) {
34     this.name = name;
35   }
36
37   /**
38    * Requires explicit serializable type registration for serializable types.
39    *
40    * @return the serializer builder
41    */
42   public SerializerBuilder withRegistrationRequired() {
43     return withRegistrationRequired(true);
44   }
45
46   /**
47    * Sets whether serializable type registration is required for serializable types.
48    *
49    * @param registrationRequired whether serializable type registration is required for serializable types
50    * @return the serializer builder
51    */
52   public SerializerBuilder withRegistrationRequired(boolean registrationRequired) {
53     namespaceBuilder.setRegistrationRequired(registrationRequired);
54     return this;
55   }
56
57   /**
58    * Enables compatible serialization for serializable types.
59    *
60    * @return the serializer builder
61    */
62   public SerializerBuilder withCompatibleSerialization() {
63     return withCompatibleSerialization(true);
64   }
65
66   /**
67    * Sets whether compatible serialization is enabled for serializable types.
68    *
69    * @param compatibleSerialization whether compatible serialization is enabled for user types
70    * @return the serializer builder
71    */
72   public SerializerBuilder withCompatibleSerialization(boolean compatibleSerialization) {
73     namespaceBuilder.setCompatible(compatibleSerialization);
74     return this;
75   }
76
77   /**
78    * Adds a namespace to the serializer.
79    *
80    * @param namespace the namespace to add
81    * @return the serializer builder
82    */
83   public SerializerBuilder withNamespace(Namespace namespace) {
84     namespaceBuilder.register(namespace);
85     return this;
86   }
87
88   /**
89    * Sets the serializable types.
90    *
91    * @param types the types to register
92    * @return the serializer builder
93    */
94   public SerializerBuilder withTypes(Class<?>... types) {
95     namespaceBuilder.register(types);
96     return this;
97   }
98
99   /**
100    * Adds a serializable type to the builder.
101    *
102    * @param type the type to add
103    * @return the serializer builder
104    */
105   public SerializerBuilder addType(Class<?> type) {
106     namespaceBuilder.register(type);
107     return this;
108   }
109
110   /**
111    * Adds a serializer to the builder.
112    *
113    * @param serializer the serializer to add
114    * @param types the serializable types
115    * @return the serializer builder
116    */
117   public SerializerBuilder addSerializer(com.esotericsoftware.kryo.Serializer serializer, Class<?>... types) {
118     namespaceBuilder.register(serializer, types);
119     return this;
120   }
121
122   @Override
123   public Serializer build() {
124     return Serializer.using(name != null ? namespaceBuilder.build(name) : namespaceBuilder.build());
125   }
126 }