~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to collections/src/main/java/com/gs/collections/impl/set/strategy/immutable/ImmutableSetWithHashingStrategySerializationProxy.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
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 com.gs.collections.impl.set.strategy.immutable;
 
18
 
 
19
import java.io.Externalizable;
 
20
import java.io.IOException;
 
21
import java.io.ObjectInput;
 
22
import java.io.ObjectOutput;
 
23
 
 
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;
 
28
 
 
29
class ImmutableSetWithHashingStrategySerializationProxy<T> implements Externalizable
 
30
{
 
31
    private static final long serialVersionUID = 1L;
 
32
    private ImmutableSet<T> set;
 
33
    private HashingStrategy<? super T> hashingStrategy;
 
34
 
 
35
    @SuppressWarnings("UnusedDeclaration")
 
36
    public ImmutableSetWithHashingStrategySerializationProxy()
 
37
    {
 
38
        // Empty constructor for Externalizable class
 
39
    }
 
40
 
 
41
    ImmutableSetWithHashingStrategySerializationProxy(ImmutableSet<T> set, HashingStrategy<? super T> hashingStrategy)
 
42
    {
 
43
        this.set = set;
 
44
        this.hashingStrategy = hashingStrategy;
 
45
    }
 
46
 
 
47
    public void writeExternal(final ObjectOutput out) throws IOException
 
48
    {
 
49
        out.writeObject(this.hashingStrategy);
 
50
        out.writeInt(this.set.size());
 
51
        try
 
52
        {
 
53
            this.set.forEach(new CheckedProcedure<T>()
 
54
            {
 
55
                @Override
 
56
                public void safeValue(T value) throws IOException
 
57
                {
 
58
                    out.writeObject(value);
 
59
                }
 
60
            });
 
61
        }
 
62
        catch (RuntimeException e)
 
63
        {
 
64
            if (e.getCause() instanceof IOException)
 
65
            {
 
66
                throw (IOException) e.getCause();
 
67
            }
 
68
            throw e;
 
69
        }
 
70
    }
 
71
 
 
72
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
 
73
    {
 
74
        HashingStrategy<? super T> strategy = (HashingStrategy<? super T>) in.readObject();
 
75
        int size = in.readInt();
 
76
        UnifiedSetWithHashingStrategy<T> deserializedSet = UnifiedSetWithHashingStrategy.newSet(strategy);
 
77
 
 
78
        for (int i = 0; i < size; i++)
 
79
        {
 
80
            deserializedSet.put((T) in.readObject());
 
81
        }
 
82
 
 
83
        this.set = deserializedSet.toImmutable();
 
84
    }
 
85
 
 
86
    protected Object readResolve()
 
87
    {
 
88
        return this.set;
 
89
    }
 
90
}