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

org.diirt.datasource.sample.services.math.AddServiceMethod Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-18 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.datasource.sample.services.math;

import java.util.HashMap;
import java.util.Map;
import org.diirt.service.ServiceDescription;
import org.diirt.service.ServiceMethod;
import org.diirt.service.ServiceMethodDescription;
import org.diirt.vtype.VNumber;
import org.diirt.vtype.ValueFactory;

/**
 * An example service method for the addition of {@link VNumber}s.
 *
 * @author carcassi
 */
public class AddServiceMethod extends ServiceMethod {

    /**
     * Creates a service method for adding {@code VNumber}s together.
     *
     * @param serviceMethodDescription the description of the add service
     * method; can't be null
     * @param serviceDescription the description of the math service; can't be
     * null
     */
    public AddServiceMethod(ServiceMethodDescription serviceMethodDescription, ServiceDescription serviceDescription) {
        super(serviceMethodDescription, serviceDescription);
    }

    @Override
    public Map syncExecImpl(Map parameters) {
        // Method implementation: this is what the service method actually does
        // Here we are choosing a synchronous implementation, that is the
        // calculation is done on the thread inside this method call.

        // Extract data from parameters
        VNumber arg1 = (VNumber) parameters.get("arg1");
        VNumber arg2 = (VNumber) parameters.get("arg2");

        // Check for nulls
        if (arg1 == null || arg2 == null) {
            return new HashMap<>();
        }

        // Perform calculation
        VNumber result = ValueFactory.newVDouble(arg1.getValue().doubleValue() + arg2.getValue().doubleValue());

        // Prepare the result
        Map resultMap = new HashMap<>();
        resultMap.put("result", result);
        return resultMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy