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;
18 import io.atomix.utils.Builder;
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);
29 public SerializerBuilder() {
33 public SerializerBuilder(String name) {
38 * Requires explicit serializable type registration for serializable types.
40 * @return the serializer builder
42 public SerializerBuilder withRegistrationRequired() {
43 return withRegistrationRequired(true);
47 * Sets whether serializable type registration is required for serializable types.
49 * @param registrationRequired whether serializable type registration is required for serializable types
50 * @return the serializer builder
52 public SerializerBuilder withRegistrationRequired(boolean registrationRequired) {
53 namespaceBuilder.setRegistrationRequired(registrationRequired);
58 * Enables compatible serialization for serializable types.
60 * @return the serializer builder
62 public SerializerBuilder withCompatibleSerialization() {
63 return withCompatibleSerialization(true);
67 * Sets whether compatible serialization is enabled for serializable types.
69 * @param compatibleSerialization whether compatible serialization is enabled for user types
70 * @return the serializer builder
72 public SerializerBuilder withCompatibleSerialization(boolean compatibleSerialization) {
73 namespaceBuilder.setCompatible(compatibleSerialization);
78 * Adds a namespace to the serializer.
80 * @param namespace the namespace to add
81 * @return the serializer builder
83 public SerializerBuilder withNamespace(Namespace namespace) {
84 namespaceBuilder.register(namespace);
89 * Sets the serializable types.
91 * @param types the types to register
92 * @return the serializer builder
94 public SerializerBuilder withTypes(Class<?>... types) {
95 namespaceBuilder.register(types);
100 * Adds a serializable type to the builder.
102 * @param type the type to add
103 * @return the serializer builder
105 public SerializerBuilder addType(Class<?> type) {
106 namespaceBuilder.register(type);
111 * Adds a serializer to the builder.
113 * @param serializer the serializer to add
114 * @param types the serializable types
115 * @return the serializer builder
117 public SerializerBuilder addSerializer(com.esotericsoftware.kryo.Serializer serializer, Class<?>... types) {
118 namespaceBuilder.register(serializer, types);
123 public Serializer build() {
124 return Serializer.using(name != null ? namespaceBuilder.build(name) : namespaceBuilder.build());