2
* Copyright 2014 Goldman Sachs.
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.
17
package com.gs.collections.impl.set.strategy.immutable;
19
import java.io.Externalizable;
20
import java.io.IOException;
21
import java.io.ObjectInput;
22
import java.io.ObjectOutput;
24
import com.gs.collections.api.block.HashingStrategy;
25
import com.gs.collections.api.set.ImmutableSet;
26
import com.gs.collections.impl.block.procedure.checked.CheckedProcedure;
27
import com.gs.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy;
29
class ImmutableSetWithHashingStrategySerializationProxy<T> implements Externalizable
31
private static final long serialVersionUID = 1L;
32
private ImmutableSet<T> set;
33
private HashingStrategy<? super T> hashingStrategy;
35
@SuppressWarnings("UnusedDeclaration")
36
public ImmutableSetWithHashingStrategySerializationProxy()
38
// Empty constructor for Externalizable class
41
ImmutableSetWithHashingStrategySerializationProxy(ImmutableSet<T> set, HashingStrategy<? super T> hashingStrategy)
44
this.hashingStrategy = hashingStrategy;
47
public void writeExternal(final ObjectOutput out) throws IOException
49
out.writeObject(this.hashingStrategy);
50
out.writeInt(this.set.size());
53
this.set.forEach(new CheckedProcedure<T>()
56
public void safeValue(T value) throws IOException
58
out.writeObject(value);
62
catch (RuntimeException e)
64
if (e.getCause() instanceof IOException)
66
throw (IOException) e.getCause();
72
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
74
HashingStrategy<? super T> strategy = (HashingStrategy<? super T>) in.readObject();
75
int size = in.readInt();
76
UnifiedSetWithHashingStrategy<T> deserializedSet = UnifiedSetWithHashingStrategy.newSet(strategy);
78
for (int i = 0; i < size; i++)
80
deserializedSet.put((T) in.readObject());
83
this.set = deserializedSet.toImmutable();
86
protected Object readResolve()