Example usage for java.util Set clear

List of usage examples for java.util Set clear

Introduction

In this page you can find the example usage for java.util Set clear.

Prototype

void clear();

Source Link

Document

Removes all of the elements from this set (optional operation).

Usage

From source file:org.apache.asterix.test.aql.TestExecutor.java

private boolean equalStrings(String expected, String actual, boolean regexMatch) {
    String[] rowsExpected = expected.split("\n");
    String[] rowsActual = actual.split("\n");

    for (int i = 0; i < rowsExpected.length; i++) {
        String expectedRow = rowsExpected[i];
        String actualRow = rowsActual[i];

        if (regexMatch) {
            if (actualRow.matches(expectedRow)) {
                continue;
            }//from  ww w.j  a va  2 s.  c om
        } else if (actualRow.equals(expectedRow)) {
            continue;
        }

        String[] expectedFields = expectedRow.split(" ");
        String[] actualFields = actualRow.split(" ");

        boolean bagEncountered = false;
        Set<String> expectedBagElements = new HashSet<>();
        Set<String> actualBagElements = new HashSet<>();

        for (int j = 0; j < expectedFields.length; j++) {
            if (j >= actualFields.length) {
                return false;
            } else if (expectedFields[j].equals(actualFields[j])) {
                bagEncountered = expectedFields[j].equals("{{");
                if (expectedFields[j].startsWith("}}")) {
                    if (regexMatch) {
                        if (expectedBagElements.size() != actualBagElements.size()) {
                            return false;
                        }
                        int[] expectedHits = new int[expectedBagElements.size()];
                        int[] actualHits = new int[actualBagElements.size()];
                        int k = 0;
                        for (String expectedElement : expectedBagElements) {
                            int l = 0;
                            for (String actualElement : actualBagElements) {
                                if (actualElement.matches(expectedElement)) {
                                    expectedHits[k]++;
                                    actualHits[l]++;
                                }
                                l++;
                            }
                            k++;
                        }
                        for (int m = 0; m < expectedHits.length; m++) {
                            if (expectedHits[m] == 0 || actualHits[m] == 0) {
                                return false;
                            }
                        }
                    } else if (!expectedBagElements.equals(actualBagElements)) {
                        return false;
                    }
                    bagEncountered = false;
                    expectedBagElements.clear();
                    actualBagElements.clear();
                }
            } else if (expectedFields[j].indexOf('.') < 0) {
                if (bagEncountered) {
                    expectedBagElements.add(expectedFields[j].replaceAll(",$", ""));
                    actualBagElements.add(actualFields[j].replaceAll(",$", ""));
                    continue;
                }
                return false;
            } else {
                // If the fields are floating-point numbers, test them
                // for equality safely
                expectedFields[j] = expectedFields[j].split(",")[0];
                actualFields[j] = actualFields[j].split(",")[0];
                try {
                    Double double1 = Double.parseDouble(expectedFields[j]);
                    Double double2 = Double.parseDouble(actualFields[j]);
                    float float1 = (float) double1.doubleValue();
                    float float2 = (float) double2.doubleValue();

                    if (Math.abs(float1 - float2) == 0) {
                        continue;
                    } else {
                        return false;
                    }
                } catch (NumberFormatException ignored) {
                    // Guess they weren't numbers - must simply not be equal
                    return false;
                }
            }
        }
    }
    return true;
}

From source file:org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat.java

/**
 * Process all the nodes and create splits that are local to a node.
 * Generate one split per node iteration, and walk over nodes multiple times
 * to distribute the splits across nodes.
 * <p>//from  w  ww . ja va  2  s.c  o  m
 * Note: The order of processing the nodes is undetermined because the
 * implementation of nodeToBlocks is {@link java.util.HashMap} and its order
 * of the entries is undetermined.
 * @param nodeToBlocks Mapping from a node to the list of blocks that
 *                     it contains.
 * @param blockToNodes Mapping from a block to the nodes on which
 *                     it has replicas.
 * @param rackToBlocks Mapping from a rack name to the list of blocks it has.
 * @param totLength Total length of the input files.
 * @param maxSize Max size of each split.
 *                If set to 0, disable smoothing load.
 * @param minSizeNode Minimum split size per node.
 * @param minSizeRack Minimum split size per rack.
 * @param splits New splits created by this method are added to the list.
 */
@VisibleForTesting
void createSplits(Map<String, Set<OneBlockInfo>> nodeToBlocks, Map<OneBlockInfo, String[]> blockToNodes,
        Map<String, List<OneBlockInfo>> rackToBlocks, long totLength, long maxSize, long minSizeNode,
        long minSizeRack, List<InputSplit> splits) {
    ArrayList<OneBlockInfo> validBlocks = new ArrayList<OneBlockInfo>();
    long curSplitSize = 0;

    int totalNodes = nodeToBlocks.size();
    long totalLength = totLength;

    Multiset<String> splitsPerNode = HashMultiset.create();
    Set<String> completedNodes = new HashSet<String>();

    while (true) {
        for (Iterator<Map.Entry<String, Set<OneBlockInfo>>> iter = nodeToBlocks.entrySet().iterator(); iter
                .hasNext();) {
            Map.Entry<String, Set<OneBlockInfo>> one = iter.next();

            String node = one.getKey();

            // Skip the node if it has previously been marked as completed.
            if (completedNodes.contains(node)) {
                continue;
            }

            Set<OneBlockInfo> blocksInCurrentNode = one.getValue();

            // for each block, copy it into validBlocks. Delete it from
            // blockToNodes so that the same block does not appear in
            // two different splits.
            Iterator<OneBlockInfo> oneBlockIter = blocksInCurrentNode.iterator();
            while (oneBlockIter.hasNext()) {
                OneBlockInfo oneblock = oneBlockIter.next();

                // Remove all blocks which may already have been assigned to other
                // splits.
                if (!blockToNodes.containsKey(oneblock)) {
                    oneBlockIter.remove();
                    continue;
                }

                validBlocks.add(oneblock);
                blockToNodes.remove(oneblock);
                curSplitSize += oneblock.length;

                // if the accumulated split size exceeds the maximum, then
                // create this split.
                if (maxSize != 0 && curSplitSize >= maxSize) {
                    // create an input split and add it to the splits array
                    addCreatedSplit(splits, Collections.singleton(node), validBlocks);
                    totalLength -= curSplitSize;
                    curSplitSize = 0;

                    splitsPerNode.add(node);

                    // Remove entries from blocksInNode so that we don't walk these
                    // again.
                    blocksInCurrentNode.removeAll(validBlocks);
                    validBlocks.clear();

                    // Done creating a single split for this node. Move on to the next
                    // node so that splits are distributed across nodes.
                    break;
                }

            }
            if (validBlocks.size() != 0) {
                // This implies that the last few blocks (or all in case maxSize=0)
                // were not part of a split. The node is complete.

                // if there were any blocks left over and their combined size is
                // larger than minSplitNode, then combine them into one split.
                // Otherwise add them back to the unprocessed pool. It is likely
                // that they will be combined with other blocks from the
                // same rack later on.
                // This condition also kicks in when max split size is not set. All
                // blocks on a node will be grouped together into a single split.
                if (minSizeNode != 0 && curSplitSize >= minSizeNode && splitsPerNode.count(node) == 0) {
                    // haven't created any split on this machine. so its ok to add a
                    // smaller one for parallelism. Otherwise group it in the rack for
                    // balanced size create an input split and add it to the splits
                    // array
                    addCreatedSplit(splits, Collections.singleton(node), validBlocks);
                    totalLength -= curSplitSize;
                    splitsPerNode.add(node);
                    // Remove entries from blocksInNode so that we don't walk this again.
                    blocksInCurrentNode.removeAll(validBlocks);
                    // The node is done. This was the last set of blocks for this node.
                } else {
                    // Put the unplaced blocks back into the pool for later rack-allocation.
                    for (OneBlockInfo oneblock : validBlocks) {
                        blockToNodes.put(oneblock, oneblock.hosts);
                    }
                }
                validBlocks.clear();
                curSplitSize = 0;
                completedNodes.add(node);
            } else { // No in-flight blocks.
                if (blocksInCurrentNode.size() == 0) {
                    // Node is done. All blocks were fit into node-local splits.
                    completedNodes.add(node);
                } // else Run through the node again.
            }
        }

        // Check if node-local assignments are complete.
        if (completedNodes.size() == totalNodes || totalLength == 0) {
            // All nodes have been walked over and marked as completed or all blocks
            // have been assigned. The rest should be handled via rackLock assignment.
            LOG.info("DEBUG: Terminated node allocation with : CompletedNodes: " + completedNodes.size()
                    + ", size left: " + totalLength);
            break;
        }
    }

    // if blocks in a rack are below the specified minimum size, then keep them
    // in 'overflow'. After the processing of all racks is complete, these 
    // overflow blocks will be combined into splits.
    ArrayList<OneBlockInfo> overflowBlocks = new ArrayList<OneBlockInfo>();
    Set<String> racks = new HashSet<String>();

    // Process all racks over and over again until there is no more work to do.
    while (blockToNodes.size() > 0) {

        // Create one split for this rack before moving over to the next rack. 
        // Come back to this rack after creating a single split for each of the 
        // remaining racks.
        // Process one rack location at a time, Combine all possible blocks that
        // reside on this rack as one split. (constrained by minimum and maximum
        // split size).

        // iterate over all racks 
        for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = rackToBlocks.entrySet().iterator(); iter
                .hasNext();) {

            Map.Entry<String, List<OneBlockInfo>> one = iter.next();
            racks.add(one.getKey());
            List<OneBlockInfo> blocks = one.getValue();

            // for each block, copy it into validBlocks. Delete it from 
            // blockToNodes so that the same block does not appear in 
            // two different splits.
            boolean createdSplit = false;
            for (OneBlockInfo oneblock : blocks) {
                if (blockToNodes.containsKey(oneblock)) {
                    validBlocks.add(oneblock);
                    blockToNodes.remove(oneblock);
                    curSplitSize += oneblock.length;

                    // if the accumulated split size exceeds the maximum, then 
                    // create this split.
                    if (maxSize != 0 && curSplitSize >= maxSize) {
                        // create an input split and add it to the splits array
                        addCreatedSplit(splits, getHosts(racks), validBlocks);
                        createdSplit = true;
                        break;
                    }
                }
            }

            // if we created a split, then just go to the next rack
            if (createdSplit) {
                curSplitSize = 0;
                validBlocks.clear();
                racks.clear();
                continue;
            }

            if (!validBlocks.isEmpty()) {
                if (minSizeRack != 0 && curSplitSize >= minSizeRack) {
                    // if there is a minimum size specified, then create a single split
                    // otherwise, store these blocks into overflow data structure
                    addCreatedSplit(splits, getHosts(racks), validBlocks);
                } else {
                    // There were a few blocks in this rack that 
                    // remained to be processed. Keep them in 'overflow' block list. 
                    // These will be combined later.
                    overflowBlocks.addAll(validBlocks);
                }
            }
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    assert blockToNodes.isEmpty();
    assert curSplitSize == 0;
    assert validBlocks.isEmpty();
    assert racks.isEmpty();

    // Process all overflow blocks
    for (OneBlockInfo oneblock : overflowBlocks) {
        validBlocks.add(oneblock);
        curSplitSize += oneblock.length;

        // This might cause an exiting rack location to be re-added,
        // but it should be ok.
        for (int i = 0; i < oneblock.racks.length; i++) {
            racks.add(oneblock.racks[i]);
        }

        // if the accumulated split size exceeds the maximum, then 
        // create this split.
        if (maxSize != 0 && curSplitSize >= maxSize) {
            // create an input split and add it to the splits array
            addCreatedSplit(splits, getHosts(racks), validBlocks);
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    // Process any remaining blocks, if any.
    if (!validBlocks.isEmpty()) {
        addCreatedSplit(splits, getHosts(racks), validBlocks);
    }
}

From source file:org.bbreak.excella.reports.tag.ImageParamParserTest.java

/**
 * {@link org.bbreak.excella.reports.tag.ImageParamParser#parse(org.apache.poi.ss.usermodel.Sheet, org.apache.poi.ss.usermodel.Cell, java.lang.Object)} ????
 *//*from w  ww. j av  a 2 s .  c  o  m*/
@Test
public void testParseSheetCellObject() {
    // -----------------------
    // []?
    // -----------------------
    Workbook workbook = getWorkbook();

    Sheet sheet1 = workbook.getSheetAt(0);

    ImageParamParser parser = new ImageParamParser();

    ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet1, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    Set<Integer> delSheetIndexs = new TreeSet<Integer>(Collections.reverseOrder());
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != 0) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    Workbook expectedWorkbook = getExpectedWorkbook();
    Sheet expectedSheet = expectedWorkbook.getSheet("Sheet1");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet1, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());

    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest1.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest1.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    // -----------------------
    // []
    // -----------------------
    workbook = getWorkbook();

    Sheet sheet2 = workbook.getSheetAt(1);

    parser = new ImageParamParser("$Image");

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData("$Image"));

    try {
        parseSheet(parser, sheet2, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != 1) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.getSheet("Sheet2");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet2, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");;
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdir();
        }
        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest2.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest2.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    String filename = this.getClass().getSimpleName();
    if (version.equals("2007")) {
        filename = filename + "_err.xlsx";
    } else if (version.equals("2003")) {
        filename = filename + "_err.xls";
    }

    URL url = this.getClass().getResource(filename);
    String path = null;
    try {
        path = URLDecoder.decode(url.getFile(), "UTF-8");

    } catch (UnsupportedEncodingException e) {
        Assert.fail();
    }

    // -----------------------
    // []?
    // -----------------------
    workbook = getWorkbook(path);

    Sheet sheet3 = workbook.getSheetAt(2);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet3, reportsParserInfo);
        fail("?????????");
    } catch (ParseException e) {
    }

    // -----------------------
    // []????
    // -----------------------
    workbook = getWorkbook();

    Sheet sheet4 = workbook.getSheetAt(2);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet4, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.getSheet("Sheet4");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet4, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    }

    // ----------------------------------------------------------------------
    // []1 / ?? / ???
    // ----------------------------------------------------------------------
    workbook = getWorkbook();

    Sheet sheet5 = workbook.getSheetAt(INDEX_OF_SHEET5);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet5, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != INDEX_OF_SHEET5) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.getSheet("Sheet5");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet5, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest3.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest3.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    // ----------------------------------------------------------------------
    // []1 / ?? / ?1
    // ----------------------------------------------------------------------

    workbook = getWorkbook();

    Sheet sheet6 = workbook.cloneSheet(INDEX_OF_SHEET5);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet6, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != workbook.getNumberOfSheets() - 1) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET5);

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet6, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest4.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest4.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    // ----------------------------------------------------------------------
    // []  / ?? / ?
    // ----------------------------------------------------------------------

    workbook = getWorkbook();

    for (int i = 1; i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS; i++) {

        Sheet sheet = workbook.cloneSheet(INDEX_OF_SHEET5);

        parser = new ImageParamParser();

        reportsParserInfo = new ReportsParserInfo();
        reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

        try {
            parseSheet(parser, sheet, reportsParserInfo);
        } catch (ParseException e) {
            fail(e.toString());
        }
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i < workbook.getNumberOfSheets() - PLURAL_COPY_FIRST_NUM_OF_SHEETS) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    for (int i = 1; i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS; i++) {
        expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET5);
    }

    try {
        int startOfTargetSheet = expectedWorkbook.getNumberOfSheets() - PLURAL_COPY_FIRST_NUM_OF_SHEETS;

        for (int i = 0; i < PLURAL_COPY_FIRST_NUM_OF_SHEETS; i++) {
            // ?
            ReportsTestUtil.checkSheet(expectedWorkbook.getSheetAt(startOfTargetSheet + i),
                    workbook.getSheetAt(i), false);
        }
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest5.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest5.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }

    }

    // ----------------------------------------------------------------------
    // []  / ?? / (2)?
    // ----------------------------------------------------------------------

    workbook = getWorkbook();

    Sheet sheet = null;
    int totalNumOfCopies = PLURAL_COPY_FIRST_NUM_OF_SHEETS + PLURAL_COPY_SECOND_NUM_OF_SHEETS;
    for (int i = 1; i <= totalNumOfCopies; i++) {

        if (i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS) {
            sheet = workbook.cloneSheet(INDEX_OF_SHEET5);
        } else {
            sheet = workbook.cloneSheet(INDEX_OF_SHEET6);
        }

        parser = new ImageParamParser();

        reportsParserInfo = new ReportsParserInfo();
        reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

        try {
            parseSheet(parser, sheet, reportsParserInfo);
        } catch (ParseException e) {
            fail(e.toString());
        }
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i < workbook.getNumberOfSheets() - totalNumOfCopies) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    for (int i = 1; i <= totalNumOfCopies; i++) {
        if (i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS) {
            expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET5);
        } else {
            expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET6);
        }
    }

    try {
        int startOfTargetSheet = expectedWorkbook.getNumberOfSheets() - totalNumOfCopies;

        for (int i = 0; i < totalNumOfCopies; i++) {
            // ?
            ReportsTestUtil.checkSheet(expectedWorkbook.getSheetAt(startOfTargetSheet + i),
                    workbook.getSheetAt(i), false);
        }
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest6.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest6.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

}

From source file:org.ssatguru.camel.salesforce.maven.CamelSalesforceMojo.java

protected void filterObjectNames(Set<String> objectNames) throws MojoExecutionException {
    getLog().info("Looking for matching Object names...");
    // create a list of accepted names
    final Set<String> includedNames = new HashSet<String>();
    if (includes != null && includes.length > 0) {
        for (String name : includes) {
            name = name.trim();// w w w  . j  ava 2 s. c  om
            if (name.isEmpty()) {
                throw new MojoExecutionException("Invalid empty name in includes");
            }
            includedNames.add(name);
        }
    }

    final Set<String> excludedNames = new HashSet<String>();
    if (excludes != null && excludes.length > 0) {
        for (String name : excludes) {
            name = name.trim();
            if (name.isEmpty()) {
                throw new MojoExecutionException("Invalid empty name in excludes");
            }
            excludedNames.add(name);
        }
    }

    // check whether a pattern is in effect
    Pattern incPattern;
    if (includePattern != null && !includePattern.trim().isEmpty()) {
        incPattern = Pattern.compile(includePattern.trim());
    } else if (includedNames.isEmpty()) {
        // include everything by default if no include names are set
        incPattern = Pattern.compile(".*");
    } else {
        // include nothing by default if include names are set
        incPattern = Pattern.compile("^$");
    }

    // check whether a pattern is in effect
    Pattern excPattern;
    if (excludePattern != null && !excludePattern.trim().isEmpty()) {
        excPattern = Pattern.compile(excludePattern.trim());
    } else {
        // exclude nothing by default
        excPattern = Pattern.compile("^$");
    }

    final Set<String> acceptedNames = new HashSet<String>();
    for (String name : objectNames) {
        // name is included, or matches include pattern
        // and is not excluded and does not match exclude pattern
        if ((includedNames.contains(name) || incPattern.matcher(name).matches())
                && !excludedNames.contains(name) && !excPattern.matcher(name).matches()) {
            acceptedNames.add(name);
        }
    }
    objectNames.clear();
    objectNames.addAll(acceptedNames);

    getLog().info(String.format("Found %s matching Objects", objectNames.size()));
}

From source file:com.diversityarrays.kdxplore.curate.TrialDataEditor.java

private void collectSelectedPlotsAndColumns(List<PlotOrSpecimen> selectedPlots,
        Set<TraitInstance> selectedTraitInstances) {
    selectedPlots.clear();//from   w  w  w .j av  a 2s .com
    selectedTraitInstances.clear();

    Map<Integer, TraitInstance> viewColumnToTraitInstance = new HashMap<>();

    for (Integer mcol : curationTableModel.getTraitInstanceColumns()) {
        int vcol = curationTable.convertColumnIndexToView(mcol);
        if (vcol >= 0) {
            TraitInstance ti = curationTableModel.getTraitInstanceAt(mcol);
            if (ti != null) {
                viewColumnToTraitInstance.put(vcol, ti);
                Log.d(TAG, "collectSelectedPlotsAndColumns: " + vcol + " => " + ti); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
    }

    for (Integer modelRow : GuiUtil.getSelectedModelRows(curationTable)) {
        selectedPlots.add(curationTableModel.getPlotOrSpecimenAtRowIndex(modelRow));

        int viewRow = curationTable.convertRowIndexToView(modelRow);
        if (viewRow >= 0) {
            for (Integer checkViewColumn : viewColumnToTraitInstance.keySet()) {
                if (curationTable.isCellSelected(viewRow, checkViewColumn)) {
                    selectedTraitInstances.add(viewColumnToTraitInstance.get(checkViewColumn));
                    System.out.println("Selected: " + checkViewColumn + "," + viewRow); //$NON-NLS-2$
                }
            }
        }
    }
}

From source file:fr.landel.utils.assertor.predicate.PredicateAssertorIterableTest.java

/**
 * Test method for {@link AssertorIterable#contains}.
 * //from   www  . j a v  a  2  s. c o m
 * @throws IOException
 *             On not contain
 */
@Test
public void testContainsIterable() throws IOException {
    final String el1 = "element1";
    final String el2 = "element2";

    final Set<String> set = new HashSet<>();
    final Set<String> set2 = new HashSet<>();
    set.add(el1);
    set2.add(el1);

    Assertor.<Set<String>, String>ofIterable().containsAll(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");
    Assertor.<Set<String>, String>ofIterable().containsAny(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");
    Assertor.<Set<String>, String>ofIterable(EnumAnalysisMode.STREAM).containsAll(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");
    Assertor.<Set<String>, String>ofIterable(EnumAnalysisMode.STREAM).containsAny(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");
    Assertor.<Set<String>, String>ofIterable(EnumAnalysisMode.PARALLEL).containsAll(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");
    Assertor.<Set<String>, String>ofIterable(EnumAnalysisMode.PARALLEL).containsAny(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");

    set2.add(el2);
    Assertor.<Set<String>, String>ofIterable().containsAny(set2).that(set)
            .orElseThrow("iterable doesn't contain the list %s*");

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAll(set2).that(set)
                .orElseThrow("iterable doesn't contain the list %2$s*");
        fail(ERROR);
    }, IllegalArgumentException.class, "iterable doesn't contain the list " + set2.toString());

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAll(set2).that(set).orElseThrow(new IOException(),
                true);
        fail(ERROR);
    }, IOException.class);

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAll((Iterable<String>) null).that(set).orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class, "neither iterables can be null or empty");

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAny((Iterable<String>) null).that(set).orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class, "neither iterables can be null or empty");

    set.clear();

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAll(set2).that(set).orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class);

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAll(set2).that(set).orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class, "neither iterables can be null or empty");

    assertException(() -> {
        Assertor.<Iterable<String>, String>ofIterable().contains(el1).that((Iterable<String>) null)
                .orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class, "the iterable cannot be null or empty");

    assertException(() -> {
        Assertor.<Iterable<String>, String>ofIterable().containsAny(set2).that((Iterable<String>) null)
                .orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class, "neither iterables can be null or empty");

    assertException(() -> {
        Assertor.<Set<String>, String>ofIterable().containsAll((Iterable<String>) null).that(set).orElseThrow();
        fail(ERROR);
    }, IllegalArgumentException.class, "neither iterables can be null or empty");

    set.add(null);
    Assertor.<Set<String>, String>ofIterable().contains(null).that(set).orElseThrow();
}

From source file:org.apache.pulsar.compaction.CompactionTest.java

@Test
public void testCompactorReadsCompacted() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-topic1";

    // capture opened ledgers
    Set<Long> ledgersOpened = Sets.newConcurrentHashSet();
    when(mockBookKeeper.newOpenLedgerOp()).thenAnswer((invocation) -> {
        OpenBuilder builder = (OpenBuilder) spy(invocation.callRealMethod());
        when(builder.withLedgerId(anyLong())).thenAnswer((invocation2) -> {
            ledgersOpened.add((Long) invocation2.getArguments()[0]);
            return invocation2.callRealMethod();
        });//from   ww w . j a  v a 2 s  . c o m
        return builder;
    });

    // subscribe before sending anything, so that we get all messages in sub1
    pulsarClient.newConsumer().topic(topic).subscriptionName("sub1").subscribe().close();

    // create the topic on the broker
    try (Producer<byte[]> producerNormal = pulsarClient.newProducer().topic(topic).create()) {
        producerNormal.newMessage().key("key0").value("my-message-0".getBytes()).send();
    }

    // force ledger roll
    pulsar.getBrokerService().getTopicReference(topic).get().close().get();

    // write a message to avoid issue #1517
    try (Producer<byte[]> producerNormal = pulsarClient.newProducer().topic(topic).create()) {
        producerNormal.newMessage().key("key1").value("my-message-1".getBytes()).send();
    }

    // verify second ledger created
    String managedLedgerName = ((PersistentTopic) pulsar.getBrokerService().getTopicReference(topic).get())
            .getManagedLedger().getName();
    ManagedLedgerInfo info = pulsar.getManagedLedgerFactory().getManagedLedgerInfo(managedLedgerName);
    Assert.assertEquals(info.ledgers.size(), 2);
    Assert.assertTrue(ledgersOpened.isEmpty()); // no ledgers should have been opened

    // compact the topic
    Compactor compactor = new TwoPhaseCompactor(conf, pulsarClient, bk, compactionScheduler);
    compactor.compact(topic).get();

    // should have opened all except last to read
    Assert.assertTrue(ledgersOpened.contains(info.ledgers.get(0).ledgerId));
    Assert.assertFalse(ledgersOpened.contains(info.ledgers.get(1).ledgerId));
    ledgersOpened.clear();

    // force broker to close resources for topic
    pulsar.getBrokerService().getTopicReference(topic).get().close().get();

    // write a message to avoid issue #1517
    try (Producer<byte[]> producerNormal = pulsarClient.newProducer().topic(topic).create()) {
        producerNormal.newMessage().key("key2").value("my-message-2".getBytes()).send();
    }

    info = pulsar.getManagedLedgerFactory().getManagedLedgerInfo(managedLedgerName);
    Assert.assertEquals(info.ledgers.size(), 3);

    // should only have opened the penultimate ledger to get stat
    Assert.assertFalse(ledgersOpened.contains(info.ledgers.get(0).ledgerId));
    Assert.assertFalse(ledgersOpened.contains(info.ledgers.get(1).ledgerId));
    Assert.assertFalse(ledgersOpened.contains(info.ledgers.get(2).ledgerId));
    ledgersOpened.clear();

    // compact the topic again
    compactor.compact(topic).get();

    // shouldn't have opened first ledger (already compacted), penultimate would have some uncompacted data.
    // last ledger already open for writing
    Assert.assertFalse(ledgersOpened.contains(info.ledgers.get(0).ledgerId));
    Assert.assertTrue(ledgersOpened.contains(info.ledgers.get(1).ledgerId));
    Assert.assertFalse(ledgersOpened.contains(info.ledgers.get(2).ledgerId));

    // all three messages should be there when we read compacted
    try (Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic).subscriptionName("sub1")
            .readCompacted(true).subscribe()) {
        Message<byte[]> message1 = consumer.receive();
        Assert.assertEquals(message1.getKey(), "key0");
        Assert.assertEquals(new String(message1.getData()), "my-message-0");

        Message<byte[]> message2 = consumer.receive();
        Assert.assertEquals(message2.getKey(), "key1");
        Assert.assertEquals(new String(message2.getData()), "my-message-1");

        Message<byte[]> message3 = consumer.receive();
        Assert.assertEquals(message3.getKey(), "key2");
        Assert.assertEquals(new String(message3.getData()), "my-message-2");
    }
}

From source file:org.apache.tez.dag.app.rm.TestTaskScheduler.java

@SuppressWarnings("unchecked")
@Test(timeout = 5000)//  w  ww .j av a  2 s.co m
public void testTaskSchedulerDetermineMinHeldContainers() throws Exception {
    RackResolver.init(new YarnConfiguration());

    TezAMRMClientAsync<CookieContainerRequest> mockRMClient = mock(TezAMRMClientAsync.class);

    String appHost = "host";
    int appPort = 0;
    String appUrl = "url";

    TaskSchedulerContext mockApp = setupMockTaskSchedulerContext(appHost, appPort, appUrl, true,
            new Configuration());
    final TaskSchedulerContextDrainable drainableAppCallback = createDrainableContext(mockApp);

    TaskSchedulerWithDrainableContext scheduler = new TaskSchedulerWithDrainableContext(drainableAppCallback,
            mockRMClient);

    scheduler.initialize();
    RegisterApplicationMasterResponse mockRegResponse = mock(RegisterApplicationMasterResponse.class);
    Resource mockMaxResource = mock(Resource.class);
    Map<ApplicationAccessType, String> mockAcls = mock(Map.class);
    when(mockRegResponse.getMaximumResourceCapability()).thenReturn(mockMaxResource);
    when(mockRegResponse.getApplicationACLs()).thenReturn(mockAcls);
    when(mockRMClient.registerApplicationMaster(anyString(), anyInt(), anyString()))
            .thenReturn(mockRegResponse);
    Resource mockClusterResource = mock(Resource.class);
    when(mockRMClient.getAvailableResources()).thenReturn(mockClusterResource);

    scheduler.start();

    String rack1 = "r1";
    String rack2 = "r2";
    String rack3 = "r3";
    String node1Rack1 = "n1r1";
    String node2Rack1 = "n2r1";
    String node1Rack2 = "n1r2";
    String node2Rack2 = "n2r2";
    String node1Rack3 = "n1r3";
    ApplicationAttemptId appId = ApplicationAttemptId.newInstance(ApplicationId.newInstance(0, 0), 0);

    Resource r = Resource.newInstance(0, 0);
    ContainerId mockCId1 = ContainerId.newInstance(appId, 0);
    Container c1 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c1.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc1 = Mockito.spy(new HeldContainer(c1, 0, 0, null, containerSignatureMatcher));
    when(hc1.getNode()).thenReturn(node1Rack1);
    when(hc1.getRack()).thenReturn(rack1);
    when(c1.getId()).thenReturn(mockCId1);
    when(c1.getResource()).thenReturn(r);
    when(hc1.getContainer()).thenReturn(c1);
    ContainerId mockCId2 = ContainerId.newInstance(appId, 1);
    Container c2 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c2.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc2 = Mockito.spy(new HeldContainer(c2, 0, 0, null, containerSignatureMatcher));
    when(hc2.getNode()).thenReturn(node2Rack1);
    when(hc2.getRack()).thenReturn(rack1);
    when(c2.getId()).thenReturn(mockCId2);
    when(c2.getResource()).thenReturn(r);
    when(hc2.getContainer()).thenReturn(c2);
    ContainerId mockCId3 = ContainerId.newInstance(appId, 2);
    Container c3 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c3.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc3 = Mockito.spy(new HeldContainer(c3, 0, 0, null, containerSignatureMatcher));
    when(hc3.getNode()).thenReturn(node1Rack1);
    when(hc3.getRack()).thenReturn(rack1);
    when(c3.getId()).thenReturn(mockCId3);
    when(c3.getResource()).thenReturn(r);
    when(hc3.getContainer()).thenReturn(c3);
    ContainerId mockCId4 = ContainerId.newInstance(appId, 3);
    Container c4 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c4.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc4 = Mockito.spy(new HeldContainer(c4, 0, 0, null, containerSignatureMatcher));
    when(hc4.getNode()).thenReturn(node2Rack1);
    when(hc4.getRack()).thenReturn(rack1);
    when(c4.getId()).thenReturn(mockCId4);
    when(c4.getResource()).thenReturn(r);
    when(hc4.getContainer()).thenReturn(c4);
    ContainerId mockCId5 = ContainerId.newInstance(appId, 4);
    Container c5 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c5.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc5 = Mockito.spy(new HeldContainer(c5, 0, 0, null, containerSignatureMatcher));
    when(hc5.getNode()).thenReturn(node1Rack2);
    when(hc5.getRack()).thenReturn(rack2);
    when(c5.getId()).thenReturn(mockCId5);
    when(c5.getResource()).thenReturn(r);
    when(hc5.getContainer()).thenReturn(c5);
    ContainerId mockCId6 = ContainerId.newInstance(appId, 5);
    Container c6 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c6.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc6 = Mockito.spy(new HeldContainer(c6, 0, 0, null, containerSignatureMatcher));
    when(hc6.getNode()).thenReturn(node2Rack2);
    when(hc6.getRack()).thenReturn(rack2);
    when(c6.getId()).thenReturn(mockCId6);
    when(c6.getResource()).thenReturn(r);
    when(hc6.getContainer()).thenReturn(c6);
    ContainerId mockCId7 = ContainerId.newInstance(appId, 6);
    Container c7 = mock(Container.class, RETURNS_DEEP_STUBS);
    when(c7.getNodeId().getHost()).thenReturn(""); // we are mocking directly
    HeldContainer hc7 = Mockito.spy(new HeldContainer(c7, 0, 0, null, containerSignatureMatcher));
    when(hc7.getNode()).thenReturn(node1Rack3);
    when(hc7.getRack()).thenReturn(rack3);
    when(c7.getId()).thenReturn(mockCId7);
    when(c7.getResource()).thenReturn(r);
    when(hc7.getContainer()).thenReturn(c7);

    scheduler.heldContainers.put(mockCId1, hc1);
    scheduler.heldContainers.put(mockCId2, hc2);
    scheduler.heldContainers.put(mockCId3, hc3);
    scheduler.heldContainers.put(mockCId4, hc4);
    scheduler.heldContainers.put(mockCId5, hc5);
    scheduler.heldContainers.put(mockCId6, hc6);
    scheduler.heldContainers.put(mockCId7, hc7);

    // test empty case
    scheduler.sessionNumMinHeldContainers = 0;
    scheduler.determineMinHeldContainers();
    Assert.assertEquals(0, scheduler.sessionMinHeldContainers.size());

    // test min >= held
    scheduler.sessionNumMinHeldContainers = 7;
    scheduler.determineMinHeldContainers();
    Assert.assertEquals(7, scheduler.sessionMinHeldContainers.size());

    // test min < held
    scheduler.sessionNumMinHeldContainers = 5;
    scheduler.determineMinHeldContainers();
    Assert.assertEquals(5, scheduler.sessionMinHeldContainers.size());

    Set<HeldContainer> heldContainers = Sets.newHashSet();
    for (ContainerId cId : scheduler.sessionMinHeldContainers) {
        heldContainers.add(scheduler.heldContainers.get(cId));
    }
    Set<String> racks = Sets.newHashSet();
    Set<String> nodes = Sets.newHashSet();
    for (HeldContainer hc : heldContainers) {
        nodes.add(hc.getNode());
        racks.add(hc.getRack());
    }
    // 1 container from each node in rack1 and rack2. 1 container from rack3.
    // covers not enough containers in rack (rack 3)
    // covers just enough containers in rack (rack 2)
    // covers more than enough containers in rack (rack 1)
    Assert.assertEquals(5, nodes.size());
    Assert.assertTrue(nodes.contains(node1Rack1) && nodes.contains(node2Rack1) && nodes.contains(node1Rack2)
            && nodes.contains(node2Rack2) && nodes.contains(node1Rack3));
    Assert.assertEquals(3, racks.size());
    Assert.assertTrue(racks.contains(rack1) && racks.contains(rack2) && racks.contains(rack3));

    long currTime = System.currentTimeMillis();
    heldContainers.clear();
    heldContainers.addAll(scheduler.heldContainers.values());
    for (HeldContainer hc : heldContainers) {
        when(hc.isNew()).thenReturn(true);
        scheduler.delayedContainerManager.addDelayedContainer(hc.getContainer(), currTime);
    }
    Thread.sleep(1000);
    drainableAppCallback.drain();
    // only the 2 container not in min-held containers are released
    verify(mockRMClient, times(2)).releaseAssignedContainer((ContainerId) any());
    Assert.assertEquals(5, scheduler.heldContainers.size());

    String appMsg = "success";
    AppFinalStatus finalStatus = new AppFinalStatus(FinalApplicationStatus.SUCCEEDED, appMsg, appUrl);
    when(mockApp.getFinalAppStatus()).thenReturn(finalStatus);
    scheduler.shutdown();
}

From source file:org.apache.hadoop.mapreduce.approx.lib.input.SampleTextInputFormat.java

/**
 * Return all the splits in the specified set of paths
 *///  w  w  w  .j ava2s . c o  m
private void getMoreSplits(JobContext job, Path[] paths, long maxSize, long minSizeNode, long minSizeRack,
        List<InputSplit> splits) throws IOException {
    Configuration conf = job.getConfiguration();

    // all blocks for all the files in input set
    OneFileInfo[] files;

    // mapping from a rack name to the list of blocks it has
    HashMap<String, List<OneBlockInfo>> rackToBlocks = new HashMap<String, List<OneBlockInfo>>();

    // mapping from a block to the nodes on which it has replicas
    HashMap<OneBlockInfo, String[]> blockToNodes = new HashMap<OneBlockInfo, String[]>();

    // mapping from a node to the list of blocks that it contains
    HashMap<String, List<OneBlockInfo>> nodeToBlocks = new HashMap<String, List<OneBlockInfo>>();

    files = new OneFileInfo[paths.length];
    if (paths.length == 0) {
        return;
    }

    // populate all the blocks for all files
    //***************************************sampling info*************************************
    //long totLength = 0;
    for (int i = 0; i < paths.length; i++) {
        files[i] = new OneFileInfo(paths[i], conf, isSplitable(job, paths[i]), rackToBlocks, blockToNodes,
                nodeToBlocks, rackToNodes, maxSize);
        //totLength += files[i].getLength();
    }

    ArrayList<OneBlockInfo> validBlocks = new ArrayList<OneBlockInfo>();
    Set<String> nodes = new HashSet<String>();
    long curSplitSize = 0;

    // process all nodes and create splits that are local
    // to a node. 
    for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = nodeToBlocks.entrySet().iterator(); iter
            .hasNext();) {

        Map.Entry<String, List<OneBlockInfo>> one = iter.next();
        nodes.add(one.getKey());
        List<OneBlockInfo> blocksInNode = one.getValue();

        // for each block, copy it into validBlocks. Delete it from 
        // blockToNodes so that the same block does not appear in 
        // two different splits.
        for (OneBlockInfo oneblock : blocksInNode) {
            if (blockToNodes.containsKey(oneblock)) {
                validBlocks.add(oneblock);
                blockToNodes.remove(oneblock);
                //*******************************************segments compose splits****************
                curSplitSize += oneblock.length;
                if (blockunit) {
                    addCreatedSplit1(splits, validBlocks);
                    curSplitSize = 0;
                    validBlocks.clear();
                    continue;
                }
                // if the accumulated split size exceeds the maximum, then 
                // create this split.
                if (maxSize != 0 && curSplitSize >= maxSize) {
                    // create an input split and add it to the splits array
                    addCreatedSplit(splits, nodes, validBlocks);
                    curSplitSize = 0;
                    validBlocks.clear();
                }
            }
        }
        // if there were any blocks left over and their combined size is
        // larger than minSplitNode, then combine them into one split.
        // Otherwise add them back to the unprocessed pool. It is likely 
        // that they will be combined with other blocks from the 
        // same rack later on.
        if (minSizeNode != 0 && curSplitSize >= minSizeNode) {
            // create an input split and add it to the splits array
            addCreatedSplit(splits, nodes, validBlocks);
        } else {
            for (OneBlockInfo oneblock : validBlocks) {
                blockToNodes.put(oneblock, oneblock.hosts);
            }
        }
        validBlocks.clear();
        nodes.clear();
        curSplitSize = 0;
    }

    // if blocks in a rack are below the specified minimum size, then keep them
    // in 'overflow'. After the processing of all racks is complete, these 
    // overflow blocks will be combined into splits.
    ArrayList<OneBlockInfo> overflowBlocks = new ArrayList<OneBlockInfo>();
    Set<String> racks = new HashSet<String>();

    // Process all racks over and over again until there is no more work to do.
    while (blockToNodes.size() > 0) {

        // Create one split for this rack before moving over to the next rack. 
        // Come back to this rack after creating a single split for each of the 
        // remaining racks.
        // Process one rack location at a time, Combine all possible blocks that
        // reside on this rack as one split. (constrained by minimum and maximum
        // split size).

        // iterate over all racks 
        for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = rackToBlocks.entrySet().iterator(); iter
                .hasNext();) {

            Map.Entry<String, List<OneBlockInfo>> one = iter.next();
            racks.add(one.getKey());
            List<OneBlockInfo> blocks = one.getValue();

            // for each block, copy it into validBlocks. Delete it from 
            // blockToNodes so that the same block does not appear in 
            // two different splits.
            boolean createdSplit = false;
            for (OneBlockInfo oneblock : blocks) {
                if (blockToNodes.containsKey(oneblock)) {
                    validBlocks.add(oneblock);
                    blockToNodes.remove(oneblock);
                    curSplitSize += oneblock.length;

                    // if the accumulated split size exceeds the maximum, then 
                    // create this split.
                    if (maxSize != 0 && curSplitSize >= maxSize) {
                        // create an input split and add it to the splits array
                        addCreatedSplit(splits, getHosts(racks), validBlocks);
                        createdSplit = true;
                        break;
                    }
                }
            }

            // if we created a split, then just go to the next rack
            if (createdSplit) {
                curSplitSize = 0;
                validBlocks.clear();
                racks.clear();
                continue;
            }

            if (!validBlocks.isEmpty()) {
                if (minSizeRack != 0 && curSplitSize >= minSizeRack) {
                    // if there is a minimum size specified, then create a single split
                    // otherwise, store these blocks into overflow data structure
                    addCreatedSplit(splits, getHosts(racks), validBlocks);
                } else {
                    // There were a few blocks in this rack that 
                    // remained to be processed. Keep them in 'overflow' block list. 
                    // These will be combined later.
                    overflowBlocks.addAll(validBlocks);
                }
            }
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    assert blockToNodes.isEmpty();
    assert curSplitSize == 0;
    assert validBlocks.isEmpty();
    assert racks.isEmpty();

    // Process all overflow blocks
    for (OneBlockInfo oneblock : overflowBlocks) {
        validBlocks.add(oneblock);
        curSplitSize += oneblock.length;

        // This might cause an exiting rack location to be re-added,
        // but it should be ok.
        for (int i = 0; i < oneblock.racks.length; i++) {
            racks.add(oneblock.racks[i]);
        }

        // if the accumulated split size exceeds the maximum, then 
        // create this split.
        if (maxSize != 0 && curSplitSize >= maxSize) {
            // create an input split and add it to the splits array
            addCreatedSplit(splits, getHosts(racks), validBlocks);
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    // Process any remaining blocks, if any.
    if (!validBlocks.isEmpty()) {
        addCreatedSplit(splits, getHosts(racks), validBlocks);
    }
}

From source file:com.pari.pcb.zip.ZIPProcessor.java

private void trimWithHostAndFileNames(Map<String, Set<String>> file2HostNameMap,
        Map<String, Set<String>> hostName2fileMap) {
    for (String fileName : file2HostNameMap.keySet()) {
        Set<String> hnSet = file2HostNameMap.get(fileName);
        if (hnSet.size() > 1) {
            Set<String> matchingHnSet = new HashSet<String>();
            for (String hn : hnSet) {
                if (fileName.contains(hn)) {
                    matchingHnSet.add(hn);
                }/*from   w  w w  .  j  av  a  2s  . co  m*/
            }
            if (matchingHnSet.size() == 1) {
                String matchingHn = matchingHnSet.iterator().next();
                hnSet.clear();
                hnSet.add(matchingHn);
            }
        }
    }
    for (String hostName : hostName2fileMap.keySet()) {
        Set<String> fnSet = hostName2fileMap.get(hostName);
        if (fnSet.size() > 1) {
            Set<String> matchingFnSet = new HashSet<String>();
            for (String fn : fnSet) {
                if (fn.contains(hostName)) {
                    matchingFnSet.add(fn);
                }
            }
            if (matchingFnSet.size() == 1) {
                String matchingFn = matchingFnSet.iterator().next();
                fnSet.clear();
                fnSet.add(matchingFn);
            }
        }
    }

}