Example usage for java.util.function BiFunction BiFunction

List of usage examples for java.util.function BiFunction BiFunction

Introduction

In this page you can find the example usage for java.util.function BiFunction BiFunction.

Prototype

BiFunction

Source Link

Usage

From source file:com.diversityarrays.kdxplore.trials.AddScoringSetDialog.java

public AddScoringSetDialog(Window owner, KdxploreDatabase kdxdb, Trial trial,
        Map<Trait, List<TraitInstance>> instancesByTrait, SampleGroup curatedSampleGroup) {
    super(owner, Msg.TITLE_ADD_SCORING_SET(), ModalityType.APPLICATION_MODAL);

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    this.kdxploreDatabase = kdxdb;
    this.trial = trial;
    this.curatedSampleGroupId = curatedSampleGroup == null ? 0 : curatedSampleGroup.getSampleGroupId();

    Map<Trait, List<TraitInstance>> noCalcs = instancesByTrait.entrySet().stream()
            .filter(e -> TraitDataType.CALC != e.getKey().getTraitDataType())
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

    Map<Trait, List<TraitInstance>> noCalcsSorted = new TreeMap<>(TRAIT_COMPARATOR);
    noCalcsSorted.putAll(noCalcs);//ww  w . ja v  a2 s.  c om

    BiFunction<Trait, TraitInstance, String> parentNameProvider = new BiFunction<Trait, TraitInstance, String>() {
        @Override
        public String apply(Trait t, TraitInstance ti) {
            if (ti == null) {
                List<TraitInstance> list = noCalcsSorted.get(t);
                if (list == null || list.size() != 1) {
                    OptionalInt opt = traitInstanceChoiceTreeModel.getChildChosenCountIfNotAllChosen(t);
                    StringBuilder sb = new StringBuilder(t.getTraitName());

                    if (opt.isPresent()) {
                        // only some of the children are chosen
                        int childChosenCount = opt.getAsInt();
                        if (childChosenCount > 0) {
                            sb.append(" (").append(childChosenCount).append(" of ").append(list.size())
                                    .append(")");
                        }
                    } else {
                        // all of the children are chosen
                        if (list != null) {
                            sb.append(" (").append(list.size()).append(")");
                        }
                    }
                    return sb.toString();
                }
            }
            return t.getTraitName();
        }
    };

    Optional<List<TraitInstance>> opt = noCalcsSorted.values().stream().filter(list -> list.size() > 1)
            .findFirst();
    String heading1 = opt.isPresent() ? "Trait/Instance" : "Trait";

    traitInstanceChoiceTreeModel = new ChoiceTreeTableModel<>(heading1, "Use?", //$NON-NLS-1$
            noCalcsSorted, parentNameProvider, childNameProvider);

    //        traitInstanceChoiceTreeModel = new TTChoiceTreeTableModel(instancesByTrait);

    traitInstanceChoiceTreeModel.addChoiceChangedListener(new ChoiceChangedListener() {
        @Override
        public void choiceChanged(Object source, ChoiceNode[] changedNodes) {
            updateCreateAction("choiceChanged");
            treeTable.repaint();
        }
    });

    traitInstanceChoiceTreeModel.addTreeModelListener(new TreeModelListener() {
        @Override
        public void treeStructureChanged(TreeModelEvent e) {
        }

        @Override
        public void treeNodesRemoved(TreeModelEvent e) {
        }

        @Override
        public void treeNodesInserted(TreeModelEvent e) {
        }

        @Override
        public void treeNodesChanged(TreeModelEvent e) {
            updateCreateAction("treeNodesChanged");
        }
    });

    warningMsg.setText(PLEASE_PROVIDE_A_DESCRIPTION);
    warningMsg.setForeground(Color.RED);

    Container cp = getContentPane();

    Box sampleButtons = null;
    if (curatedSampleGroup != null && curatedSampleGroup.getAnyScoredSamples()) {
        sampleButtons = createWantSampleButtons(curatedSampleGroup);
    }

    Box top = Box.createVerticalBox();
    if (sampleButtons == null) {
        top.add(new JLabel(Msg.MSG_THERE_ARE_NO_CURATED_SAMPLES()));
    } else {
        top.add(sampleButtons);
    }
    top.add(descriptionField);

    cp.add(top, BorderLayout.NORTH);

    descriptionField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
            updateCreateAction("documentListener");
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateCreateAction("documentListener");
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateCreateAction("documentListener");
        }
    });

    updateCreateAction("init");
    //        KDClientUtils.initAction(ImageId.`CHECK_ALL, useAllAction, "Click to Use All");

    treeTable = new JXTreeTable(traitInstanceChoiceTreeModel);
    treeTable.setAutoResizeMode(JXTreeTable.AUTO_RESIZE_ALL_COLUMNS);

    TableCellRenderer renderer = treeTable.getDefaultRenderer(Integer.class);
    if (renderer instanceof JLabel) {
        ((JLabel) renderer).setHorizontalAlignment(JLabel.CENTER);
    }

    Box buttons = Box.createHorizontalBox();

    buttons.add(new JButton(useAllAction));
    buttons.add(new JButton(useNoneAction));

    buttons.add(Box.createHorizontalGlue());
    buttons.add(warningMsg);

    buttons.add(new JButton(cancelAction));
    buttons.add(Box.createHorizontalStrut(10));
    buttons.add(new JButton(createAction));

    cp.add(new JScrollPane(treeTable), BorderLayout.CENTER);
    cp.add(buttons, BorderLayout.SOUTH);

    pack();
}

From source file:com.github.totyumengr.minicubes.core.FactTable.java

/**
 * @param merge fact-table will be merge into.
 * @throws IllegalArgumentException when parameter is null
 * @since 0.2/*from w w w .j  a va  2s . c  o m*/
 */
void merge(FactTable merge) {

    if (merge == null) {
        throw new IllegalArgumentException();
    }
    LOGGER.info("Try to merge {} into {}.", merge, this);
    try {
        readWriteLock.writeLock().lock();
        // Start merge
        for (Entry<Integer, Record> entry : merge.records.entrySet()) {
            this.records.put(entry.getKey(), entry.getValue());
        }
        for (Entry<String, RoaringBitmap> entry : merge.bitmapIndex.entrySet()) {
            this.bitmapIndex.merge(entry.getKey(), entry.getValue(),
                    new BiFunction<RoaringBitmap, RoaringBitmap, RoaringBitmap>() {

                        @Override
                        public RoaringBitmap apply(RoaringBitmap t, RoaringBitmap u) {
                            return RoaringBitmap.or(t, u);
                        }
                    });
        }
    } finally {
        readWriteLock.writeLock().unlock();
    }

    LOGGER.info("Merge {} successfully into {}.", merge, this);
    return;
}

From source file:com.redhat.example.rules.unittest.CsvTestHelper.java

/**
 * create RuleFactWatchers from a CSV file list
 * @param fileListCsvPath/*  w  w  w. j a  v a  2s . co m*/
 * @return
 */
public static RuleFactWatchers createRuleFactWatchers(String fileListCsvPath) {
    boolean hasError = false;
    Map<String, Class<?>> factClassMap = new LinkedHashMap<String, Class<?>>();
    Map<String, RuleFactWatcher> watcherMap = new LinkedHashMap<String, RuleFactWatcher>();

    File folder = new File(fileListCsvPath).getParentFile();
    // load CSV files list
    List<CsvFiles> csvFiles = readCsvListFiles(fileListCsvPath);
    // process only expected files
    for (int i = 0; i < csvFiles.size();) {
        CsvFiles csvFile = csvFiles.get(i);
        if (csvFile.type != TestFileType.EX) {
            csvFiles.remove(i);
        } else {
            i++;
        }
    }
    // sort by path
    Collections.sort(csvFiles, new Comparator<CsvFiles>() {
        @Override
        public int compare(CsvFiles o1, CsvFiles o2) {
            return o1.path.compareTo(o2.path);
        }
    });
    // process each expected file
    for (CsvFiles csvFile : csvFiles) {
        // create new RuleFactWatcher
        String path[] = separateParentPath(csvFile.path);
        RuleFactWatcher parentWatcher = watcherMap.get(path[0]);
        Class<?> clazz;
        try {
            clazz = Class.forName(csvFile.clazz);
            Map<String, String> optionMap = getOptionMap(csvFile.options);
            boolean checkByIndex = "true".equalsIgnoreCase(optionMap.get(OPTION_CHECK_BY_INDEX));
            String keyType = optionMap.get(OPTION_KEY_TYPE);
            Class<?> keyClass = (keyType != null) ? Class.forName(keyType) : null;
            RuleFactWatcher watcher = createRuleFactWatcher(new File(folder, csvFile.file).getCanonicalPath(),
                    clazz, checkByIndex, keyClass);
            Class<?> parentClass = factClassMap.get(path[0]);
            watcher.setHeaderFact(parentClass, new BiFunction<Object, Object[], Object>() {
                @Override
                public Object apply(Object t, Object[] u) {
                    return RuleFactWatcher.getProperty(t, path[1]);
                }
            }, null);
            if (parentWatcher != null) {
                parentWatcher.registerChildWatcher(path[1], watcher);
            }
            watcherMap.put(csvFile.path, watcher);
            factClassMap.put(csvFile.path, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            hasError = true;
        }
    }
    if (!hasError) {
        // returns RuleFactWatchers
        RuleFactWatchers watchers = new RuleFactWatchers();
        for (RuleFactWatcher watcher : watcherMap.values()) {
            // filter out the watchers for child
            if (!watcher.isChild()) {
                watchers.add(watcher);
            }
        }
        return watchers;
    } else {
        fail("fail at createRuleFactWatchers(" + fileListCsvPath + ")");
        return null;
    }
}

From source file:com.github.totyumengr.minicubes.cluster.TimeSeriesMiniCubeManagerHzImpl.java

@Override
public Map<Integer, RoaringBitmap> distinct(String distinctName, boolean isDim, String groupByDimName,
        Map<String, List<Integer>> filterDims) {

    try {/*from  w w  w. j  a  v  a  2 s .  c o  m*/
        Set<String> cubeIds = cubeIds();

        // Do execute
        List<Map<Integer, RoaringBitmap>> results = execute(
                new Distinct(distinctName, isDim, groupByDimName, filterDims), cubeIds, hzExecutorTimeout);
        LOGGER.debug("Distinct {} on {} with filter {} results is {}", distinctName, cubeIds, filterDims,
                results);

        Map<Integer, RoaringBitmap> result = new HashMap<Integer, RoaringBitmap>(results.size());
        results.stream().forEach(new Consumer<Map<Integer, RoaringBitmap>>() {

            @Override
            public void accept(Map<Integer, RoaringBitmap> t) {
                t.forEach((k, v) -> result.merge(k, v,
                        new BiFunction<RoaringBitmap, RoaringBitmap, RoaringBitmap>() {

                            @Override
                            public RoaringBitmap apply(RoaringBitmap t, RoaringBitmap u) {
                                return RoaringBitmap.or(t, u);
                            }
                        }));
            }
        });
        LOGGER.debug("Distinct {} on {} with filter {} results is {}", distinctName, cubeIds, filterDims,
                result);
        return result;
    } finally {
        AGG_CONTEXT.remove();
    }
}