Example usage for java.util List indexOf

List of usage examples for java.util List indexOf

Introduction

In this page you can find the example usage for java.util List indexOf.

Prototype

int indexOf(Object o);

Source Link

Document

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Usage

From source file:com.joliciel.csvLearner.features.RealValueFeatureEvaluator.java

/**
 * For a given feature, calculate the entropy after each level of splitting.
 * Level 0: the entropy taking into account only those events which have a value as opposed to those which don't
 * Level 1: entropy for events without a value (where value=0) + entropy of other events after first split
 * Level 2: entropy for events without a value (where value=0) + entropy of other events after second split
 * etc./*from   w w  w. j  a  v  a2 s . co  m*/
 * @param events the list of events
 * @param feature the feature to consider for splitting
 * @return 
 */
public List<Double> evaluateFeature(GenericEvents events, String feature, String testOutcome) {
    long startTime = (new Date()).getTime();

    if (LOG.isTraceEnabled()) {
        LOG.trace("Evaluating feature: " + feature);
        LOG.trace("Test outcome: " + testOutcome);
    }
    long startTimeInitialise = (new Date()).getTime();

    PriorityQueue<NameValuePair> heap = new PriorityQueue<NameValuePair>(events.size());
    Collection<NameValuePair> featureValues = new ArrayList<NameValuePair>();
    Map<String, Integer> eventOutcomeMap = new TreeMap<String, Integer>();
    Map<String, Integer> featureOutcomeMap = new TreeMap<String, Integer>();
    Map<String, Integer> nonFeatureOutcomeMap = new TreeMap<String, Integer>();

    List<String> outcomes = null;
    if (testOutcome == null) {
        Set<String> outcomeSet = events.getOutcomes();
        outcomes = new ArrayList<String>(outcomeSet);
    } else {
        outcomes = new ArrayList<String>();
        outcomes.add(testOutcome);
        outcomes.add("");
    }
    int[] eventOutcomeCounts = new int[outcomes.size()];
    int[] featureOutcomeCounts = new int[outcomes.size()];
    int[] nonFeatureOutcomeCounts = new int[outcomes.size()];

    int eventCount = events.size();
    int featureCount = 0;
    for (GenericEvent event : events) {
        if (!event.isTest()) {
            String outcome = event.getOutcome();
            int outcomeIndex = 0;
            if (testOutcome == null) {
                outcomeIndex = outcomes.indexOf(outcome);
            } else {
                if (!outcome.equals(testOutcome)) {
                    outcome = "";
                    outcomeIndex = 1;
                } else {
                    outcomeIndex = 0;
                }
            }

            long startTimeFindFeature = (new Date()).getTime();
            int featureIndex = event.getFeatureIndex(feature);
            long endTimeFindFeature = (new Date()).getTime();
            totalTimeFindFeature += (endTimeFindFeature - startTimeFindFeature);
            if (featureIndex >= 0) {
                long startTimeOrdering = (new Date()).getTime();
                heap.add(new NameValuePair(outcome, event.getWeights().get(featureIndex)));
                long endTimeOrdering = (new Date()).getTime();
                totalTimeOrdering += (endTimeOrdering - startTimeOrdering);
                featureOutcomeCounts[outcomeIndex]++;
                featureCount++;
            } else {
                nonFeatureOutcomeCounts[outcomeIndex]++;
            }
            eventOutcomeCounts[outcomeIndex]++;
        }
    }
    int nonFeatureCount = eventCount - featureCount;

    long startTimeOrdering = (new Date()).getTime();
    while (!heap.isEmpty())
        featureValues.add(heap.poll());
    long endTimeOrdering = (new Date()).getTime();
    totalTimeOrdering += (endTimeOrdering - startTimeOrdering);

    int i = 0;
    for (String outcome : outcomes) {
        eventOutcomeMap.put(outcome, eventOutcomeCounts[i]);
        featureOutcomeMap.put(outcome, featureOutcomeCounts[i]);
        nonFeatureOutcomeMap.put(outcome, nonFeatureOutcomeCounts[i]);
        i++;
    }

    long endTimeInitialise = (new Date()).getTime();
    totalTimeInitialise += (endTimeInitialise - startTimeInitialise);

    long startTimeInitialEntropy = (new Date()).getTime();
    double eventSpaceEntropy = EntropyCalculator.getEntropy(eventOutcomeMap.values(), eventCount);
    double featureEntropy = EntropyCalculator.getEntropy(featureOutcomeMap.values(), featureCount);
    double nonFeatureEntropy = EntropyCalculator.getEntropy(nonFeatureOutcomeMap.values(), nonFeatureCount);
    long endTimeInitialEntropy = (new Date()).getTime();
    totalTimeInitialEntropy += (endTimeInitialEntropy - startTimeInitialEntropy);

    List<Double> entropyByLevel = new ArrayList<Double>();
    entropyByLevel.add(eventSpaceEntropy);

    double proportionalFeatureEntropy = ((double) featureCount / (double) eventCount) * featureEntropy;
    double proportionalNonFeatureEntropy = ((double) nonFeatureCount / (double) eventCount) * nonFeatureEntropy;
    double level0Entropy = proportionalFeatureEntropy + proportionalNonFeatureEntropy;
    entropyByLevel.add(level0Entropy);

    if (LOG.isTraceEnabled()) {
        LOG.trace("eventSpaceEntropy: " + eventSpaceEntropy);
        LOG.trace("proportionalFeatureEntropy: " + proportionalFeatureEntropy);
        LOG.trace("proportionalNonFeatureEntropy: " + proportionalNonFeatureEntropy);
        LOG.trace("level 0 Entropy: " + level0Entropy);
    }

    List<NameValuePair> featureValueList = new ArrayList<NameValuePair>(featureValues);
    long startTimeSplit = (new Date()).getTime();
    featureSplitter.split(featureValueList);
    long endTimeSplit = (new Date()).getTime();
    totalTimeSplit += (endTimeSplit - startTimeSplit);

    Map<Integer, Set<Split>> splitsByDepth = featureSplitter.getSplitsByDepth();

    for (int level : splitsByDepth.keySet()) {
        double levelEntropy = proportionalNonFeatureEntropy;
        if (splitsByDepth.get(level).size() == 0)
            levelEntropy += proportionalFeatureEntropy;
        else {
            for (Split split : splitsByDepth.get(level)) {
                long startTimeSplitEntropy = (new Date()).getTime();
                double proprotionalEntropy = ((double) split.getSize() / (double) eventCount)
                        * split.getEntropy();
                long endTimeSplitEntropy = (new Date()).getTime();
                totalTimeSplitEntropy += (endTimeSplitEntropy - startTimeSplitEntropy);
                levelEntropy += proprotionalEntropy;
            }
        }
        entropyByLevel.add(levelEntropy);
        if (LOG.isTraceEnabled())
            LOG.trace("level " + level + " Entropy: " + levelEntropy);
    }
    long endTime = (new Date()).getTime();
    totalTime += (endTime - startTime);

    return entropyByLevel;
}

From source file:com.qcadoo.mes.assignmentToShift.print.xls.AssignmentToShiftXlsHelper.java

public String getListOfWorkersWithOtherCases(final List<Entity> staffAssignmentToShifts) {
    if (staffAssignmentToShifts == null) {
        return L_EMPTY;
    }//from ww  w  . java2 s  .c  o m

    StringBuilder listOfWorkersWithOtherCases = new StringBuilder();

    for (Entity staffAssignmentToShift : staffAssignmentToShifts) {
        Entity worker = staffAssignmentToShift.getBelongsToField(StaffAssignmentToShiftFields.WORKER);

        listOfWorkersWithOtherCases.append(worker.getStringField(StaffFields.NAME));
        listOfWorkersWithOtherCases.append(" ");
        listOfWorkersWithOtherCases.append(worker.getStringField(StaffFields.SURNAME));
        listOfWorkersWithOtherCases.append(" - ");
        listOfWorkersWithOtherCases.append(
                staffAssignmentToShift.getStringField(StaffAssignmentToShiftFields.OCCUPATION_TYPE_NAME));

        if (staffAssignmentToShifts.indexOf(staffAssignmentToShift) != (staffAssignmentToShifts.size() - 1)) {
            listOfWorkersWithOtherCases.append(", ");
        }
    }

    return listOfWorkersWithOtherCases.toString();
}

From source file:ch.oakmountain.tpa.parser.TpaParser.java

private Map<ColumnIdentifier, Integer> getColLayoutMapping(String colLayoutString,
        List<ColumnIdentifier> cols) {

    List<String> colLayoutList = Arrays.asList(colLayoutString.split(","));

    Map<ColumnIdentifier, Integer> colLayoutMapping = new HashMap<ColumnIdentifier, Integer>();

    for (ColumnIdentifier col : cols) {
        if (colLayoutList.indexOf(col.toString()) >= 0) {
            colLayoutMapping.put(col, colLayoutList.indexOf(col.toString()));
        } else if (colLayoutList.indexOf(lastNonEmptyColPrefix + col.toString()) >= 0) {
            colLayoutMapping.put(col, lastNonEmptyColNb);
        } else {/*  w ww. j av  a2 s .  co m*/
            throw new IllegalArgumentException("Could not find column " + col.name() + " in the column layout");
        }
    }
    return colLayoutMapping;
}

From source file:com.twinsoft.convertigo.beans.core.RequestableStep.java

private void increaseOrder(DatabaseObject databaseObject, Long before) throws EngineException {
    List<Long> ordered = null;
    Long value = new Long(databaseObject.priority);

    if (databaseObject instanceof Variable)
        ordered = orderedVariables.get(0);

    if (!ordered.contains(value))
        return;//from w  w  w . ja  v a 2s  .com
    int pos = ordered.indexOf(value);
    if (pos == 0)
        return;

    if (before == null)
        before = ordered.get(pos - 1);
    int pos1 = ordered.indexOf(before);

    ordered.add(pos1, value);
    ordered.remove(pos + 1);
    hasChanged = true;
}

From source file:edu.cmu.tetrad.search.Mimbuild2.java

private TetradMatrix getCov(ICovarianceMatrix _measurescov, List<Node> latents, Node[][] indicators) {
    if (latents.size() != indicators.length) {
        throw new IllegalArgumentException();
    }/*from w  w  w  .j  a  va 2 s.  c  om*/

    TetradMatrix measurescov = _measurescov.getMatrix();
    TetradMatrix latentscov = new TetradMatrix(latents.size(), latents.size());

    for (int i = 0; i < latentscov.rows(); i++) {
        for (int j = i; j < latentscov.columns(); j++) {
            if (i == j)
                latentscov.set(i, j, 1.0);
            else {
                double v = .5;
                latentscov.set(i, j, v);
                latentscov.set(j, i, v);
            }
        }
    }

    double[][] loadings = new double[indicators.length][];

    for (int i = 0; i < indicators.length; i++) {
        loadings[i] = new double[indicators[i].length];
    }

    for (int i = 0; i < indicators.length; i++) {
        loadings[i] = new double[indicators[i].length];

        for (int j = 0; j < indicators[i].length; j++) {
            loadings[i][j] = .5;
        }
    }

    int[][] indicatorIndices = new int[indicators.length][];
    List<Node> measures = _measurescov.getVariables();

    for (int i = 0; i < indicators.length; i++) {
        indicatorIndices[i] = new int[indicators[i].length];

        for (int j = 0; j < indicators[i].length; j++) {
            indicatorIndices[i][j] = measures.indexOf(indicators[i][j]);
        }
    }

    // Variances of the measures.
    double[] delta = new double[measurescov.rows()];

    for (int i = 0; i < delta.length; i++) {
        delta[i] = 1;
    }

    int numNonMeasureVarianceParams = 0;

    for (int i = 0; i < latentscov.rows(); i++) {
        for (int j = i; j < latentscov.columns(); j++) {
            numNonMeasureVarianceParams++;
        }
    }

    for (int i = 0; i < indicators.length; i++) {
        numNonMeasureVarianceParams += indicators[i].length;
    }

    double[] allParams1 = getAllParams(indicators, latentscov, loadings, delta);

    optimizeNonMeasureVariancesQuick(indicators, measurescov, latentscov, loadings, indicatorIndices);

    //        for (int i = 0; i < 10; i++) {
    //            optimizeNonMeasureVariancesConditionally(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);
    //            optimizeMeasureVariancesConditionally(measurescov, latentscov, loadings, indicatorIndices, delta);
    //
    //            double[] allParams2 = getAllParams(indicators, latentscov, loadings, delta);
    //            if (distance(allParams1, allParams2) < epsilon) break;
    //            allParams1 = allParams2;
    //        }

    this.numParams = allParams1.length;

    //        // Very slow but could be done alone.
    optimizeAllParamsSimultaneously(indicators, measurescov, latentscov, loadings, indicatorIndices, delta);

    double N = _measurescov.getSampleSize();
    int p = _measurescov.getDimension();

    int df = (p) * (p + 1) / 2 - (numParams);
    double x = (N - 1) * minimum;
    this.pValue = 1.0 - new ChiSquaredDistribution(df).cumulativeProbability(x);

    return latentscov;
}

From source file:com.ailk.oci.ocnosql.tools.load.csvbulkload.PhoenixCsvToKeyValueMapper.java

@Override
protected void setup(Context context) throws IOException, InterruptedException {

    Configuration conf = context.getConfiguration();
    String jdbcUrl = getJdbcUrl(conf);

    // This statement also ensures that the driver class is loaded
    LOG.info("Connection with driver {} with url {}", PhoenixDriver.class.getName(), jdbcUrl);

    try {//from  ww  w.j ava  2 s . co  m
        conn = (PhoenixConnection) DriverManager.getConnection(jdbcUrl);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    upsertListener = new MapperUpsertListener(context, conf.getBoolean(IGNORE_INVALID_ROW_CONFKEY, true));
    csvUpsertExecutor = buildUpsertExecutor(conf);
    csvLineParser = new CsvLineParser(conf.get(FIELD_DELIMITER_CONFKEY).charAt(0));

    preUpdateProcessor = loadPreUpsertProcessor(conf);

    // ?
    // ?
    List<String> importColumnList = new ArrayList<String>();
    for (ColumnInfo colInfo : buildColumnInfoList(conf)) {
        importColumnList.add(colInfo.getColumnName());
    }
    // ?hash?
    List<String> rowPrefixColumns = Lists
            .newArrayList(Splitter.on(",").trimResults().split(conf.get(ROW_PREFIX_COLUMNS)));
    // rowkey?hash?
    rowPrefixColIdxs = new ArrayList<Integer>();
    for (String rpCol : rowPrefixColumns) {
        // ?1csv??
        rowPrefixColIdxs.add(importColumnList.indexOf(rpCol) - 1);
    }

    // rowkey
    List<String> rowColumns = Lists.newArrayList(Splitter.on(",").trimResults().split(conf.get(ROW_COLUMNS)));
    rowColIdxs = new ArrayList<Integer>();
    for (String rCol : rowColumns) {
        // ?1csv??
        rowColIdxs.add(importColumnList.indexOf(rCol) - 1);
    }

    // 
    List<String> uniqueIndexColumns = Lists
            .newArrayList(Splitter.on(",").trimResults().split(conf.get(UNIQUE_INDEX_COLUMNS, "_allColumns")));
    if (uniqueIndexColumns.size() == 1 && uniqueIndexColumns.get(0).equals("_allColumns")) {
        unqIdxColIdxs = null;
    } else {
        unqIdxColIdxs = new ArrayList<Integer>();
        for (String rCol : uniqueIndexColumns) {
            // ?1csv??
            unqIdxColIdxs.add(importColumnList.indexOf(rCol) - 1);
        }
    }

    // ?rowkey???(md5)
    rowKeyGenerator = buildRowKeyGenerator(conf.get(ROW_PREFIX_ALG, "md5"));
    separator = conf.get(FIELD_DELIMITER_CONFKEY);

    // ??
    rowGentemp = new StringBuilder();
}

From source file:it.grid.storm.namespace.config.xml.XMLParserUtil.java

private int retrieveNumberByName(String name, String collectionElement) {

    int result = -1;
    int size = -1;
    // log.debug(" NAME : "+name+"  |  Collection Element :"+collectionElement);
    List prop = configuration.getList(collectionElement);
    if (prop != null) {
        size = prop.size();//  w w  w  .jav  a  2  s  . c  o m
        result = prop.indexOf(name);
    } else {
        log.warn("[retrieveNumberByName_2] Element <" + collectionElement
                + "> does not exists in namespace configuration file");
    }
    return result;
}

From source file:io.druid.segment.IndexMerger.java

public static File merge(List<IndexableAdapter> indexes, final AggregatorFactory[] metricAggs, File outDir,
        Map<String, Object> segmentMetadata, IndexSpec indexSpec, ProgressIndicator progress)
        throws IOException {
    FileUtils.deleteDirectory(outDir);/*  w ww  .  j  av a 2  s  . c o m*/
    if (!outDir.mkdirs()) {
        throw new ISE("Couldn't make outdir[%s].", outDir);
    }

    final List<String> mergedDimensions = mergeIndexed(
            Lists.transform(indexes, new Function<IndexableAdapter, Iterable<String>>() {
                @Override
                public Iterable<String> apply(@Nullable IndexableAdapter input) {
                    return input.getDimensionNames();
                }
            }));
    final List<String> mergedMetrics = Lists.transform(mergeIndexed(Lists.newArrayList(
            FunctionalIterable.create(indexes).transform(new Function<IndexableAdapter, Iterable<String>>() {
                @Override
                public Iterable<String> apply(@Nullable IndexableAdapter input) {
                    return input.getMetricNames();
                }
            }).concat(Arrays.<Iterable<String>>asList(new AggFactoryStringIndexed(metricAggs))))),
            new Function<String, String>() {
                @Override
                public String apply(@Nullable String input) {
                    return input;
                }
            });
    if (mergedMetrics.size() != metricAggs.length) {
        throw new IAE("Bad number of metrics[%d], expected [%d]", mergedMetrics.size(), metricAggs.length);
    }

    final AggregatorFactory[] sortedMetricAggs = new AggregatorFactory[mergedMetrics.size()];
    for (int i = 0; i < metricAggs.length; i++) {
        AggregatorFactory metricAgg = metricAggs[i];
        sortedMetricAggs[mergedMetrics.indexOf(metricAgg.getName())] = metricAgg;
    }

    for (int i = 0; i < mergedMetrics.size(); i++) {
        if (!sortedMetricAggs[i].getName().equals(mergedMetrics.get(i))) {
            throw new IAE("Metric mismatch, index[%d] [%s] != [%s]", i, metricAggs[i].getName(),
                    mergedMetrics.get(i));
        }
    }

    Function<ArrayList<Iterable<Rowboat>>, Iterable<Rowboat>> rowMergerFn = new Function<ArrayList<Iterable<Rowboat>>, Iterable<Rowboat>>() {
        @Override
        public Iterable<Rowboat> apply(@Nullable ArrayList<Iterable<Rowboat>> boats) {
            return CombiningIterable.create(
                    new MergeIterable<Rowboat>(Ordering.<Rowboat>natural().nullsFirst(), boats),
                    Ordering.<Rowboat>natural().nullsFirst(), new RowboatMergeFunction(sortedMetricAggs));
        }
    };

    return makeIndexFiles(indexes, outDir, progress, mergedDimensions, mergedMetrics, segmentMetadata,
            rowMergerFn, indexSpec);
}

From source file:com.alibaba.dubbo.util.KetamaNodeLocatorTest.java

@Test
public void testWeightedDistribution() {
    final int nodeSize = 5;
    final int keySize = 10000;
    final List<String> nodes = generateRandomStrings(nodeSize);
    final List<String> weightedNodes = new ArrayList<String>(nodes);
    weightedNodes.add(nodes.get(3)); // 20%
    for (int ix = 0; ix < 4; ix++) {
        weightedNodes.add(nodes.get(4));
    } // 50%/*w  w w  .ja  va  2s . c  o m*/
    final long start1 = System.currentTimeMillis();
    final KetamaNodeLocator locator = new KetamaNodeLocator(weightedNodes);

    // Make sure the initialization doesn't take too long.
    assertTrue((System.currentTimeMillis() - start1) < 100);

    final int[] counts = new int[nodeSize];
    for (int ix = 0; ix < nodeSize; ix++) {
        counts[ix] = 0;
    }

    final List<String> keys = generateRandomStrings(keySize);

    for (final String key : keys) {
        final String primary = locator.getPrimary(key);
        counts[nodes.indexOf(primary)] += 1;
    }

    // Give about a 30% leeway each way...
    final int min = (keySize * 7) / (nodeSize * 2 * 10);
    final int max = (keySize * 13) / (nodeSize * 2 * 10);
    int total = 0;
    boolean error = false;
    final StringBuilder sb = new StringBuilder("Key distribution error - \n");

    for (int ix = 0; ix < nodeSize; ix++) {
        int expectedMin = min;
        int expectedMax = max;
        if (ix == 3) {
            expectedMin = 2 * min;
            expectedMax = 2 * max;
        }
        if (ix == 4) {
            expectedMin = 5 * min;
            expectedMax = 5 * max;
        }
        if (counts[ix] < expectedMin || counts[ix] > expectedMax) {
            error = true;
            sb.append("  !!  ");
        } else {
            sb.append("      ");
        }
        sb.append(StringUtils.rightPad(nodes.get(ix), 12)).append(": ").append(counts[ix]).append("\n");
        total += counts[ix];
    }
    // Make sure we didn't miss any keys returning values.
    assertEquals(keySize, total);

    //        System.out.println(sb.toString());
    if (error) {
        fail(sb.toString());
    }
}

From source file:com.liferay.jenkins.tools.DurationMatcher.java

protected String parseTextDuration(String[] optionValues) {
    List<String> parsedStrings = new ArrayList<>();

    for (String optionValue : optionValues) {
        optionValue = optionValue.toUpperCase();

        if (NumberUtils.isDigits(optionValue)) {
            parsedStrings.add(optionValue);
        } else if (secondsLabels.contains(optionValue)) {
            parsedStrings.add("S");
        } else if (minutesLabels.contains(optionValue)) {
            parsedStrings.add("M");
        } else if (hoursLabels.contains(optionValue)) {
            parsedStrings.add("H");
        } else if (daysLabels.contains(optionValue)) {
            parsedStrings.add("D");
        }//from  w  ww.  j a va 2s.  c  o m
    }

    StringBuilder sb = new StringBuilder();

    sb.append("P");

    boolean timeSectionSet = false;

    for (String section : possibleSections) {
        if (parsedStrings.contains(section)) {
            if (!timeSectionSet) {
                sb.append("T");

                timeSectionSet = true;
            }

            int index = parsedStrings.indexOf(section);

            sb.append(parsedStrings.get(index - 1));
            sb.append(parsedStrings.get(index));
        }
    }

    return sb.toString();
}