2 * Copyright 2018-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils.serializer;
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);
27 public SerializerBuilder() {
31 public SerializerBuilder(String name) {
36 * Requires explicit serializable type registration for serializable types.
38 * @return the serializer builder
40 public SerializerBuilder withRegistrationRequired() {
41 return withRegistrationRequired(true);
45 * Sets whether serializable type registration is required for serializable types.
47 * @param registrationRequired whether serializable type registration is required for serializable types
48 * @return the serializer builder
50 public SerializerBuilder withRegistrationRequired(boolean registrationRequired) {
51 namespaceBuilder.setRegistrationRequired(registrationRequired);
56 * Enables compatible serialization for serializable types.
58 * @return the serializer builder
60 public SerializerBuilder withCompatibleSerialization() {
61 return withCompatibleSerialization(true);
65 * Sets whether compatible serialization is enabled for serializable types.
67 * @param compatibleSerialization whether compatible serialization is enabled for user types
68 * @return the serializer builder
70 public SerializerBuilder withCompatibleSerialization(boolean compatibleSerialization) {
71 namespaceBuilder.setCompatible(compatibleSerialization);
76 * Adds a namespace to the serializer.
78 * @param namespace the namespace to add
79 * @return the serializer builder
81 public SerializerBuilder withNamespace(Namespace namespace) {
82 namespaceBuilder.register(namespace);
87 * Sets the serializable types.
89 * @param types the types to register
90 * @return the serializer builder
92 public SerializerBuilder withTypes(Class<?>... types) {
93 namespaceBuilder.register(types);
98 * Adds a serializable type to the builder.
100 * @param type the type to add
101 * @return the serializer builder
103 public SerializerBuilder addType(Class<?> type) {
104 namespaceBuilder.register(type);
109 * Adds a serializer to the builder.
111 * @param serializer the serializer to add
112 * @param types the serializable types
113 * @return the serializer builder
115 public SerializerBuilder addSerializer(com.esotericsoftware.kryo.Serializer<?> serializer, Class<?>... types) {
116 namespaceBuilder.register(serializer, types);
121 * Build the {@link Serializer}.
123 * @return A new {@link Serializer}.
125 public Serializer build() {
126 return Serializer.using(name != null ? namespaceBuilder.build(name) : namespaceBuilder.build());