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

com.alachisoft.ncache.serialization.standard.io.surrogates.AverageResultSerializationSurrogate Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alachisoft.ncache.serialization.standard.io.surrogates;

import com.alachisoft.ncache.runtime.queries.AverageResult;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.alachisoft.ncache.serialization.core.io.surrogates.*;
import com.alachisoft.ncache.serialization.standard.io.TypeSurrogateSelectorImpl;

import java.math.BigDecimal;

/**
 * @author Administrator
 */
public class AverageResultSerializationSurrogate extends SerializationSurrogateBase
        implements SerializationSurrogate, BuiltinSerializationSurrogate {

    /**
     * Creates a new instance of LongSerializationSurrogate
     */
    public AverageResultSerializationSurrogate() {
        super(AverageResult.class);
    }

    /**
     * Read and return an object. The class that implements this interface
     * defines where the object is "read" from.
     *
     * @param input the stream to read data from in order to restore the object.
     * @return the object read from the stream
     * @throws java.lang.ClassNotFoundException If the class of a serialized
     *                                          object cannot be found.
     * @throws NCacheIOException                If any of the usual Input/Output
     *                                          related exceptions occur.
     */
    public Object readObject(NCacheObjectInput input)
            throws NCacheInstantiationException, NCacheIOException {
        try {
            AverageResult result = new AverageResult();
            TypeSurrogateSelectorImpl impl = TypeSurrogateSelectorImpl.getDefault();
            SerializationSurrogate decimalSurrogate = impl.getSurrogateForType(BigDecimal.class, null);
            result.setSum((BigDecimal) decimalSurrogate.readObject(input));
            result.setCount((BigDecimal) decimalSurrogate.readObject(input));
            return result;
        } catch (Exception ex) {
            throw new NCacheIOException(ex);
        }
    }

    /**
     * Write an object to the underlying storage or stream.  The
     * class that implements this interface defines how the object is
     * written.
     *
     * @param output the stream to write the object to.
     * @param graph  the object to write .
     * @throws NCacheIOException Any of the usual Input/Output related exceptions.
     */
    public void writeObject(NCacheObjectOutput output, Object graph)
            throws NCacheIOException {
        try {
            AverageResult result = (AverageResult) graph;
            TypeSurrogateSelectorImpl impl = TypeSurrogateSelectorImpl.getDefault();
            SerializationSurrogate decimalSurrogate = impl.getSurrogateForType(BigDecimal.class, null);
            decimalSurrogate.writeObject(output, result.getSum());
            decimalSurrogate.writeObject(output, result.getCount());
        } catch (Exception ex) {
            throw new NCacheIOException(ex);
        }
    }

    public void skipObject(NCacheObjectInput input) throws NCacheInstantiationException, NCacheIOException {
        try {
            TypeSurrogateSelectorImpl impl = TypeSurrogateSelectorImpl.getDefault();
            SerializationSurrogate decimalSurrogate = impl.getSurrogateForType(BigDecimal.class, null);
            decimalSurrogate.skipObject(input);
            decimalSurrogate.skipObject(input);
        } catch (Exception ex) {
            throw new NCacheIOException(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy