Java Code Examples for org.eclipse.collections.api.list.MutableList#size()

The following examples show how to use org.eclipse.collections.api.list.MutableList#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServiceRoutingSelectorImpl.java    From alibaba-rsocket-broker with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Integer findHandler(Integer serviceId) {
    MutableList<Integer> handlers = serviceHandlers.get(serviceId);
    int handlerCount = handlers.size();
    if (handlerCount > 1) {
        try {
            return handlers.get(ThreadLocalRandom.current().nextInt(handlerCount));
        } catch (Exception e) {
            return handlers.get(0);
        }
    } else if (handlerCount == 1) {
        return handlers.get(0);
    } else {
        return null;
    }
}
 
Example 2
Source File: CsvReaderDataSourceTest.java    From obevo with Apache License 2.0 6 votes vote down vote up
private void verifyCsv(String input, MutableList<String> keyFields, MutableMap<String, String>... rows) throws Exception {
    CsvReaderDataSource ds = new CsvReaderDataSource(csvVersion, "test", new StringReader(input), ',', StringFunctions.toLowerCase(), "null");
    ds.setCatoConfiguration(new CatoSimpleJavaConfiguration(new SimpleCatoProperties(keyFields)));
    ds.init();
    ds.open();
    final MutableList<Map<String, Object>> dataRows = IteratorIterate.collect(ds, new Function<CatoDataObject, Map<String, Object>>() {
        @Override
        public Map<String, Object> valueOf(CatoDataObject catoDataObject) {
            Map<String, Object> data = Maps.mutable.empty();
            for (String field : catoDataObject.getFields()) {
                data.put(field, catoDataObject.getValue(field));
            }
            data.remove(CsvReaderDataSource.ROW_NUMBER_FIELD);

            return data;
        }
    }, Lists.mutable.<Map<String, Object>>empty());
    ds.close();

    assertEquals(rows.length, dataRows.size());
    for (int i = 0; i < dataRows.size(); i++) {
        assertEquals("Error on row " + i, rows[i], dataRows.get(i));
    }
}
 
Example 3
Source File: ServiceRoutingSelectorImpl.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public Integer getInstanceCount(Integer serviceId) {
    MutableList<Integer> handlerCount = serviceHandlers.get(serviceId);
    if (handlerCount.size() <= 1) {
        return handlerCount.size();
    } else {
        return handlerCount.distinct().size();
    }
}
 
Example 4
Source File: FastListMultimap2.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
public boolean removeAllSameValue(K key, V value) {
    MutableList<V> collection = this.map.get(key);
    if (collection == null) {
        return false;
    }
    int originSize = collection.size();
    boolean changed = collection.removeIf(Predicate.isEqual(value));
    if (changed) {
        this.totalSize = this.totalSize - (originSize - collection.size());
        if (collection.isEmpty()) {
            this.map.remove(key);
        }
    }
    return changed;
}
 
Example 5
Source File: FastListMultimap2.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
public boolean keepOnlyOne(K key, V value) {
    MutableList<V> collection = this.map.get(key);
    if (collection == null) {
        return false;
    }
    int originSize = collection.size();
    collection.removeIf(Predicate.isEqual(value));
    collection.add(value);
    this.totalSize = this.totalSize - (originSize - collection.size());
    return true;
}
 
Example 6
Source File: MsSqlDomainSqlTranslator.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public String handleRawFullSql(String sql, ChangeInput change) {
    sql = CommentRemover.removeComments(sql, "sybase sp_addtype conversion").trim();
    if (sql.startsWith("sp_addtype")) {
        // all the params are in string literals, e.g. 'param'. Let's extract it out
        MutableList<SqlToken> allParts = new SqlTokenParser().parseTokens(sql);
        MutableList<SqlToken> parts = allParts.select(new Predicate<SqlToken>() {
            @Override
            public boolean accept(SqlToken it) {
                return it.getTokenType().equals(SqlTokenType.STRING);
            }
        });

        String domainName = null;
        String domainType = null;
        String domainProps = null;
        for (int i = 0; i < parts.size(); i++) {
            String part = this.stripAddTypeParam(parts.get(i).getText().trim());
            if (i == 0) {
                domainName = part;
            } else if (i == 1) {
                domainType = part;
            } else if (i == 2) {
                domainProps = part;
            } else {
                throw new IllegalArgumentException("Not expecting more than 3 args here, but got: " +
                        Arrays.asList(parts));
            }
        }

        return this.createDomainSql(domainName, domainType, domainProps);
    } else {
        return sql;
    }
}
 
Example 7
Source File: AseDomainSqlTranslator.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public String handleRawFullSql(String sql, ChangeInput change) {
    sql = CommentRemover.removeComments(sql, "sybase sp_addtype conversion").trim();
    if (sql.startsWith("sp_addtype")) {
        // all the params are in string literals, e.g. 'param'. Let's extract it out
        MutableList<SqlToken> allParts = new SqlTokenParser().parseTokens(sql);
        MutableList<SqlToken> parts = allParts.select(new Predicate<SqlToken>() {
            @Override
            public boolean accept(SqlToken it) {
                return it.getTokenType().equals(SqlTokenType.STRING);
            }
        });

        String domainName = null;
        String domainType = null;
        String domainProps = null;
        for (int i = 0; i < parts.size(); i++) {
            String part = this.stripAddTypeParam(parts.get(i).getText().trim());
            if (i == 0) {
                domainName = part;
            } else if (i == 1) {
                domainType = part;
            } else if (i == 2) {
                domainProps = part;
            } else {
                throw new IllegalArgumentException("Not expecting more than 3 args here, but got: " +
                        Arrays.asList(parts));
            }
        }

        return this.createDomainSql(domainName, domainType, domainProps);
    } else {
        return sql;
    }
}
 
Example 8
Source File: DirectoryAssert.java    From obevo with Apache License 2.0 5 votes vote down vote up
/**
 * Primitive DB comparison method
 * We just compare file names, not the subdirectory structure also
 * (so this would fail if multiple subdirectories had the same file name)
 */
public static void assertDirectoriesEqual(File expected, File actual, boolean ignoreWhitespace) {
    MutableList<File> expectedFiles = FastList.newList(FileUtils.listFiles(expected, new WildcardFileFilter("*"),
            DIR_FILE_FILTER));
    expectedFiles = expectedFiles.sortThisBy(toRelativePath(expected));
    MutableList<File> actualFiles = FastList.newList(FileUtils.listFiles(actual, new WildcardFileFilter("*"),
            DIR_FILE_FILTER));
    actualFiles = actualFiles.sortThisBy(toRelativePath(actual));

    assertEquals(
            String.format("Directories did not have same # of files:\nExpected: %1$s\nbut was: %2$s",
                    expectedFiles.makeString("\n"), actualFiles.makeString("\n")),
            expectedFiles.size(), actualFiles.size());
    for (int i = 0; i < expectedFiles.size(); i++) {
        File expectedFile = expectedFiles.get(i);
        File actualFile = actualFiles.get(i);

        String expectedFilePath = getRelativePath(expectedFile, expected);
        String actualFilePath = getRelativePath(actualFile, actual);
        LOG.info("Comparing {} vs {}", expectedFilePath, actualFilePath);
        assertEquals("File " + i + " [" + expectedFile + " vs " + actualFile
                + " does not match paths relative from their roots", expectedFilePath, actualFilePath);

        if (ignoreWhitespace) {
            String expectedContent = FileUtilsCobra.readFileToString(expectedFile);
            String actualContent = FileUtilsCobra.readFileToString(actualFile);
            assertThat(actualContent, Matchers.equalToIgnoringWhiteSpace(expectedContent));
        } else {
            FileAssert.assertEquals("Mismatch on file " + expectedFile.getAbsolutePath(), expectedFile, actualFile);
        }
    }
}
 
Example 9
Source File: MultiThreadedTx.java    From reladomo with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(MultiThreadedTx tx) throws SystemException
{
    MutableList<Future<Integer>> futures = FastList.newList(tx.resourceManagers.size());
    int i = -1;
    try
    {
        for(i = 1; i < tx.resourceManagers.size();i++)
        {
            TxGroup branch = tx.resourceManagers.get(i);
            futures.add(branch.prepare());
        }
        int offset = 1;
        for(i = 0; i < futures.size();i++)
        {
            Future<Integer> result = futures.get(i);
            int prepareState = result.get();
            if (prepareState == XAResource.XA_RDONLY)
            {
                tx.resourceManagers.remove(i + offset);
                offset--;
            }
        }
        tx.status.set(COMMITTING);
    }
    catch (Throwable e)
    {
        logger.error("Error preparing. Rolling back instead", e);
        tx.resourceManagers.remove(i); // resource threw an exception and is presumed rolled back
        tx.rollbackCause = e;
        tx.status.set(ROLLING_BACK);
    }
}
 
Example 10
Source File: SqlTokenParser.java    From obevo with Apache License 2.0 4 votes vote down vote up
public MutableList<SqlToken> parseTokens(String str) {
    str += "\n";  // to handle case where last line entry is // ending comment
    MutableList<SqlToken> tokens = Lists.mutable.empty();

    SqlTokenParserImpl parser = new SqlTokenParserImpl(new StringReader(str));

    boolean inToken = false;
    List<String> queuedTokens = new ArrayList<String>();
    for (Token token = parser.getNextToken(); token.kind != SqlTokenParserImplConstants.EOF;
         token = parser.getNextToken()) {

        SqlTokenType tokenType;
        switch (token.kind) {
        case SqlTokenParserImplConstants.OTHER:
            inToken = true;
            queuedTokens.add(token.image);
            break;
        case SqlTokenParserImplConstants.DOUBLE_QUOTE_STRING:
        case SqlTokenParserImplConstants.SINGLE_QUOTE_STRING:
            tokenType = SqlTokenType.STRING;
            this.processToken(inToken, queuedTokens, tokens, tokenType, token);
            inToken = false;
            break;
        case SqlTokenParserImplConstants.WHITESPACE:
            tokenType = SqlTokenType.WHITESPACE;
            this.processToken(inToken, queuedTokens, tokens, tokenType, token);
            inToken = false;
            break;
        case SqlTokenParserImplConstants.SINGLE_LINE_COMMENT1:
        case SqlTokenParserImplConstants.SINGLE_LINE_COMMENT2:
        case SqlTokenParserImplConstants.MULTI_LINE_COMMENT:
            tokenType = SqlTokenType.COMMENT;
            this.processToken(inToken, queuedTokens, tokens, tokenType, token);
            inToken = false;
            break;
        default:
            throw new IllegalArgumentException("Not expecting this token type: " + token.kind);
        }
    }

    if (inToken) {
        String queuedTokenStr = StringUtils.join(queuedTokens, "");
        queuedTokens.clear();
        tokens.add(new SqlToken(SqlTokenType.TOKEN, queuedTokenStr));
    }

    if (!tokens.isEmpty()) {
        int lastIndex = tokens.size() - 1;
        SqlToken lastToken = tokens.get(lastIndex);
        String lastText = lastToken.getText();
        if (!lastText.isEmpty() && lastText.charAt(lastText.length() - 1) == '\n') {
            lastText = lastText.substring(0, lastText.length() - 1);
            if (lastText.isEmpty()) {
                tokens.remove(lastIndex);
            } else {
                tokens.set(lastIndex, new SqlToken(lastToken.getTokenType(), lastText));
            }
        }
    }
    return tokens;
}
 
Example 11
Source File: MultiLineStringSplitter.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public MutableList<String> valueOf(String inputString) {
    inputString += "\n";  // add sentinel to facilitate line split

    MutableList<SqlToken> sqlTokens = this.tokenParser.parseTokens(inputString);
    sqlTokens = this.collapseWhiteSpaceAndTokens(sqlTokens);

    MutableList<String> finalSplitStrings = Lists.mutable.empty();
    String currentSql = "";

    for (SqlToken sqlToken : sqlTokens) {
        if (sqlToken.getTokenType() == SqlTokenType.COMMENT || sqlToken.getTokenType() == SqlTokenType.STRING) {
            currentSql += sqlToken.getText();
        } else {
            String pattern = splitOnWholeLine ? "(?i)^" + this.splitToken + "$" : this.splitToken;
            MutableList<String> splitStrings =
                    Lists.mutable.with(Pattern.compile(pattern, Pattern.MULTILINE).split(sqlToken.getText()));
            if (splitStrings.isEmpty()) {
                // means that we exactly match
                finalSplitStrings.add(currentSql);
                currentSql = "";
            } else if (splitStrings.size() == 1) {
                currentSql += sqlToken.getText();
            } else {
                splitStrings.set(0, currentSql + splitStrings.get(0));

                if (splitOnWholeLine) {
                    if (splitStrings.size() > 1) {
                        splitStrings.set(0, StringUtils.chomp(splitStrings.get(0)));
                        for (int i = 1; i < splitStrings.size(); i++) {
                            String newSql = splitStrings.get(i);
                            if (newSql.startsWith("\n")) {
                                newSql = newSql.replaceFirst("^\n", "");
                            } else if (newSql.startsWith("\r\n")) {
                                newSql = newSql.replaceFirst("^\r\n", "");
                            }

                            // Chomping the end of each sql due to the split of the GO statement
                            if (i < splitStrings.size() - 1) {
                                newSql = StringUtils.chomp(newSql);
                            }
                            splitStrings.set(i, newSql);
                        }
                    }
                }

                finalSplitStrings.addAll(splitStrings.subList(0, splitStrings.size() - 1));
                currentSql = splitStrings.getLast();
            }
        }
    }

    if (!currentSql.isEmpty()) {
        finalSplitStrings.add(currentSql);
    }

    // accounting for the sentinel
    if (finalSplitStrings.getLast().isEmpty()) {
        finalSplitStrings.remove(finalSplitStrings.size() - 1);
    } else {
        finalSplitStrings.set(finalSplitStrings.size() - 1, StringUtils.chomp(finalSplitStrings.getLast()));
    }

    return finalSplitStrings;
}