org.mule.modules.riak.mel.ExpressionMutation.java Source code

Java tutorial

Introduction

Here is the source code for org.mule.modules.riak.mel.ExpressionMutation.java

Source

/**
 * (c) 2003-2015 MuleSoft, Inc. The software in this package is
 * published under the terms of the CPAL v1.0 license, a copy of which
 * has been included with this distribution in the LICENSE.md file.
 */

package org.mule.modules.riak.mel;

import java.util.Collections;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.cap.Mutation;

/**
 * A {@link Mutation} that's implemented by a MEL expression. *
 * <p/>
 * If no serializer is configured, the <code>IRiakObject></code> to mutate is bound to the
 * <code>original</code> variable. The expression must return a mutated {@link IRiakObject}.
 * Otherwise, the deserialized {@link Object} is bound to the <code>siblings</code> variable. The
 * expression must return a resolved {@link Object} that will be serialized before storage.
 */
public class ExpressionMutation extends AbstractExpressionHelper implements Mutation<IRiakObject> {
    public IRiakObject apply(final IRiakObject original) {
        final IRiakObject mutated = hasSerializers() ? serializeAndMutateObject(original)
                : mutateRawObject(original);

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Object after mutation: " + ToStringBuilder.reflectionToString(mutated));
        }

        return mutated;
    }

    private IRiakObject mutateRawObject(final IRiakObject original) {
        return evaluate(Collections.<String, Object>singletonMap("original", original));
    }

    private IRiakObject serializeAndMutateObject(final IRiakObject original) {
        final Object object = deserialize(original);
        final Object mutatedObject = evaluate(Collections.<String, Object>singletonMap("original", object));
        original.setValue(serialize(mutatedObject));
        return original;
    }
}