Skip to content

Serializers for unmodifiable Java collections.#260

Merged
johnynek merged 2 commits into
twitter:developfrom
chermenin:unmodifiable-collections
Oct 11, 2016
Merged

Serializers for unmodifiable Java collections.#260
johnynek merged 2 commits into
twitter:developfrom
chermenin:unmodifiable-collections

Conversation

@chermenin
Copy link
Copy Markdown
Contributor

Added serializers for collections created via
Collections.unmodifiable...() and Collections.empty...() methods.

Added serializers for collections created via
Collections.unmodifiable...() and Collections.empty...() methods.
*
* @author <a href="mailto:alex@chermenin.ru">Alex Chermenin</a>
*/
public class UnmodifiableCollectionSerializer extends Serializer<Collection<?>> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are all basically the same. Can we share the code? Something like:

public abstract class UnmodifiableJavaCollectionSerializer<T, U> extends Serializer<T> {
    protected U getInner(T t);
    protected T build(U u);

    @Override
    public Collection<?> read(Kryo kryo, Input input, Class<T> type) {
        try {
            U c = (U) kryo.readClassAndObject(input);
            return build(c);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Kryo kryo, Output output, T object) {
        try {
            U c = getInner(object);
            kryo.writeClassAndObject(output, c);
        } catch (RuntimeException e) {
            // Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write...
            // handles SerializationException specifically (resizing the buffer)...
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Then we can just implement the code to read the inner and wrap for each of them.

Also, could you possibly set your editor to 2 space indent, which is the style we use?

@chermenin
Copy link
Copy Markdown
Contributor Author

Great thought! Thanks for review.

Serializers of unmodifiable Java collections was refactored.
Deleted serializers for empty collections (Kryo has default serializators for it).
@chermenin chermenin changed the title Serializers for unmodifiable and empty collections. Serializers for unmodifiable Java collections. Aug 28, 2016
@johnynek johnynek merged commit 6f17e04 into twitter:develop Oct 11, 2016
johnynek pushed a commit that referenced this pull request Oct 11, 2016
* Serializers for unmodifiable and empty collections.

Added serializers for collections created via
Collections.unmodifiable...() and Collections.empty...() methods.

* Some refactoring.

Serializers of unmodifiable Java collections was refactored.
Deleted serializers for empty collections (Kryo has default serializators for it).
johnynek added a commit that referenced this pull request Oct 11, 2016
* Serializers for unmodifiable and empty collections.

Added serializers for collections created via
Collections.unmodifiable...() and Collections.empty...() methods.

* Some refactoring.

Serializers of unmodifiable Java collections was refactored.
Deleted serializers for empty collections (Kryo has default serializators for it).
@chermenin chermenin deleted the unmodifiable-collections branch October 12, 2016 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants