View Javadoc
1   /*
2    * Copyright 2014 Luca Burgazzoli
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  
17  package net.anyflow.lannister.serialization;
18  
19  import java.util.function.Supplier;
20  
21  import com.hazelcast.nio.serialization.Serializer;
22  
23  public class HazelcastSerializer<T, S> implements Serializer, Supplier<S> {
24  	protected final Class<T> type;
25  	protected final int typeId;
26  	protected final ThreadLocal<S> threadLocal;
27  
28  	protected HazelcastSerializer(final Class<T> type, int typeId) {
29  		this(type, typeId, null);
30  	}
31  
32  	protected HazelcastSerializer(final Class<T> type, int typeId, Supplier<? extends S> supplier) {
33  		this.type = type;
34  		this.typeId = typeId;
35  		this.threadLocal = supplier != null ? ThreadLocal.withInitial(supplier) : null;
36  	}
37  
38  	@Override
39  	public int getTypeId() {
40  		return typeId;
41  	}
42  
43  	@Override
44  	public void destroy() {
45  		// Did nothing in original Source(escape from code inspection error)
46  	}
47  
48  	public Class<T> getType() {
49  		return this.type;
50  	}
51  
52  	@Override
53  	public S get() {
54  		return threadLocal.get();
55  	}
56  }