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

Java tutorial

Introduction

Here is the source code for org.mule.modules.riak.mel.ExpressionConflictResolver.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.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

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

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

/**
 * A {@link ConflictResolver} that's implemented by a MEL expression. Note that the expression is
 * called only if there are two or more siblings to resolve.
 * <p/>
 * If no serializer is configured, the <code>Collection&lt;IRiakObject></code> to resolve is bound
 * to the <code>siblings</code> variable. The expression must return a resolved {@link IRiakObject}.
 * Otherwise, the deserialized <code>Collection&lt;Object></code> is bound to the
 * <code>siblings</code> variable. The expression must return a resolved {@link Object} that will be
 * serialized before storage.
 */
public class ExpressionConflictResolver extends AbstractExpressionHelper implements ConflictResolver<IRiakObject> {
    public IRiakObject resolve(final Collection<IRiakObject> siblings) {
        if (siblings.size() > 1) {
            // We have siblings, need to resolve them
            final IRiakObject resolved = hasSerializers() ? serializeAndResolveSiblings(siblings)
                    : resolveRawSiblings(siblings);

            if (getLogger().isDebugEnabled()) {
                getLogger()
                        .debug("Object after conflict resolution: " + ToStringBuilder.reflectionToString(resolved));
            }

            return resolved;
        } else if (siblings.size() == 1) {
            // Only one object - just return it
            return siblings.iterator().next();
        } else {
            // No object returned - return null
            return null;
        }
    }

    private IRiakObject resolveRawSiblings(final Collection<IRiakObject> siblings) {
        return evaluate(Collections.<String, Object>singletonMap("siblings", siblings));
    }

    private IRiakObject serializeAndResolveSiblings(final Collection<IRiakObject> siblings) {
        final IRiakObject resolved = siblings.iterator().next();

        final List<Object> transformedSiblings = new ArrayList<Object>();
        for (final IRiakObject sibling : siblings) {
            final Object transformedSibling = deserialize(sibling);
            transformedSiblings.add(transformedSibling);
        }

        final Object transformedResolved = evaluate(
                Collections.<String, Object>singletonMap("siblings", transformedSiblings));

        resolved.setValue(serialize(transformedResolved));
        return resolved;
    }
}