All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kolibrifx.plovercrest.performance.tools.serialization.TimestampOnlyMessageSerializer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
 */

package com.kolibrifx.plovercrest.performance.tools.serialization;

import java.util.OptionalLong;
import com.kolibrifx.napo.base.JavaType;
import com.kolibrifx.common.NotImplementedException;
import com.kolibrifx.napo.CollectionTypeDefinition;
import com.kolibrifx.napo.CompoundTypeDefinition;
import com.kolibrifx.napo.FieldDefinition;
import com.kolibrifx.napo.MessageReader;
import com.kolibrifx.napo.MessageSerializer;
import com.kolibrifx.napo.MessageWriter;
import com.kolibrifx.napo.TypeDefinition;
import com.kolibrifx.napo.VariantTypeDefinition;

public class TimestampOnlyMessageSerializer implements MessageSerializer {

    private static final JavaType JAVA_TYPE = JavaType.forClass(Long.class);
    private final TypeDefinition typeDefinition;

    public TimestampOnlyMessageSerializer(final TypeDefinition typeDefinition) {
        this.typeDefinition = typeDefinition;
    }

    private static OptionalLong deserializeObject(final TypeDefinition type, final MessageReader in) {
        OptionalLong result = OptionalLong.empty();
        switch (type.getTypeKind()) {
            case BOOLEAN:
                in.getBoolean();
                break;
            case BYTES:
                in.getBytes();
                break;
            case COLLECTION:
                deserializeCollection((CollectionTypeDefinition) type, in);
                break;
            case COMPOUND:
                result = deserializeCompound((CompoundTypeDefinition) type, in);
                break;
            case DOUBLE:
                in.getDouble();
                break;
            case INT:
                in.getInt();
                break;
            case LONG:
                in.getLong();
                break;
            case STRING:
                in.getString();
                break;
            case TIMESTAMP:
                result = OptionalLong.of(in.getTimestamp());
                break;
            case VARIANT:
                deserializeVariant((VariantTypeDefinition) type, in);
                break;
            default:
                throw new IllegalStateException("Unknown type kind: " + type.getTypeKind());
        }
        return result;
    }

    private static void deserializeVariant(final VariantTypeDefinition type, final MessageReader in) {
        throw new NotImplementedException();
    }

    private static OptionalLong deserializeCompound(final CompoundTypeDefinition type, final MessageReader in) {
        OptionalLong result = OptionalLong.empty();
        in.beginMessage(JAVA_TYPE, type);
        for (final FieldDefinition field : type.getFields()) {
            final OptionalLong tmp = deserializeObject(field.getDefinition(), in);
            if (tmp.isPresent() && !result.isPresent()) {
                result = tmp;
            }
        }
        in.endMessage();
        return result;
    }

    private static void deserializeCollection(final CollectionTypeDefinition type, final MessageReader in) {
        throw new NotImplementedException();
    }

    @Override
    public void serialize(final MessageWriter out, final Long m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Long deserialize(final MessageReader in) {
        return deserializeObject(typeDefinition, in).orElseThrow(() -> new IllegalStateException("Missing timestamp"));
    }

    @Override
    public JavaType getJavaType() {
        return JAVA_TYPE;
    }

    @Override
    public TypeDefinition getTypeDefinition() {
        return typeDefinition;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy