com.yimidida.shards.strategy.exit.impl.ExitOperationsSelectCollector.java Source code

Java tutorial

Introduction

Here is the source code for com.yimidida.shards.strategy.exit.impl.ExitOperationsSelectCollector.java

Source

/*
 * @(#)ExitOperationsSelectCollector.java 2012-9-7 ?1:38:06
 *
 * Copyright (c) 2011-2012 Makersoft.org all rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 */
package com.yimidida.shards.strategy.exit.impl;

import java.util.Collections;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;

import com.yimidida.shards.select.SelectFactory;
import com.yimidida.shards.strategy.exit.ExitOperationsCollector;
import com.yimidida.shards.strategy.reduce.ShardReduceStrategy;

/**
 * .
 * 
 * @version 2012-9-7 ?1:38:06
 * @author Feng Kuok
 */
public class ExitOperationsSelectCollector implements ExitOperationsCollector {

    private final String statement;
    private final Object parameter;
    private final RowBounds rowBounds;

    private final ShardReduceStrategy shardReduceStrategy;

    public ExitOperationsSelectCollector(SelectFactory selectFactory, ShardReduceStrategy shardReduceStrategy) {
        this.statement = selectFactory.getStatement();
        this.parameter = selectFactory.getParameter();
        this.rowBounds = selectFactory.getRowBounds();

        this.shardReduceStrategy = shardReduceStrategy;

    }

    @Override
    public List<Object> apply(List<Object> values) {

        if (!values.isEmpty()) {
            //reduce
            List<Object> results = shardReduceStrategy.reduce(statement, parameter, rowBounds, values);

            values = (results != null) ? results : Collections.emptyList(); //null

            if (rowBounds != null && rowBounds != RowBounds.DEFAULT) {
                values = new RowBoundsExitOperation(rowBounds).apply(values);
            }

        }

        return values;
    }

    @Override
    public void setSqlSessionFactory(SqlSessionFactory sessionFactory) {
        throw new UnsupportedOperationException();
    }

}