com.heliosapm.streams.serialization.TimeValuePairSerde.java Source code

Java tutorial

Introduction

Here is the source code for com.heliosapm.streams.serialization.TimeValuePairSerde.java

Source

/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package com.heliosapm.streams.serialization;

import java.util.Map;

import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;

import com.google.common.primitives.Bytes;
import com.google.common.primitives.Longs;

/**
 * <p>Title: TimeValuePairSerde</p>
 * <p>Description: </p> 
 * @author Whitehead (nwhitehead AT heliosdev DOT org)
 * <p><code>com.heliosapm.streams.serialization.TimeValuePairSerde</code></p>
 */

public class TimeValuePairSerde implements Serde<long[]>, Serializer<long[]>, Deserializer<long[]> {

    /**
     * Creates a new TimeValuePairSerde
     */
    public TimeValuePairSerde() {
    }

    /**
     * {@inheritDoc}
     * @see org.apache.kafka.common.serialization.Deserializer#deserialize(java.lang.String, byte[])
     */
    @Override
    public long[] deserialize(final String topic, final byte[] data) {
        final long[] pair = new long[2];
        byte[] b = new byte[8];
        System.arraycopy(data, 0, b, 0, 8);
        pair[0] = Longs.fromByteArray(b);
        System.arraycopy(data, 8, b, 0, 8);
        pair[1] = Longs.fromByteArray(b);
        return pair;
    }

    /**
     * {@inheritDoc}
     * @see org.apache.kafka.common.serialization.Serializer#serialize(java.lang.String, java.lang.Object)
     */
    @Override
    public byte[] serialize(final String topic, final long[] data) {
        return Bytes.concat(Longs.toByteArray(data[0]), Longs.toByteArray(data[1]));
    }

    /**
     * {@inheritDoc}
     * @see org.apache.kafka.common.serialization.Serde#configure(java.util.Map, boolean)
     */
    @Override
    public void configure(final Map<String, ?> configs, final boolean isKey) {
        /* No Op */
    }

    /**
     * {@inheritDoc}
     * @see org.apache.kafka.common.serialization.Serde#close()
     */
    @Override
    public void close() {
        /* No Op */
    }

    /**
     * {@inheritDoc}
     * @see org.apache.kafka.common.serialization.Serde#serializer()
     */
    @Override
    public Serializer<long[]> serializer() {
        return this;
    }

    /**
     * {@inheritDoc}
     * @see org.apache.kafka.common.serialization.Serde#deserializer()
     */
    @Override
    public Deserializer<long[]> deserializer() {
        return this;
    }

}