Example usage for java.util Comparator comparingInt

List of usage examples for java.util Comparator comparingInt

Introduction

In this page you can find the example usage for java.util Comparator comparingInt.

Prototype

public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) 

Source Link

Document

Accepts a function that extracts an int sort key from a type T , and returns a Comparator that compares by that sort key.

Usage

From source file:com.streamsets.datacollector.bundles.SupportBundleManager.java

/**
 * Orchestrate what definitions should be used for this bundle.
 *
 * Either get all definitions that should be used by default or only those specified in the generators argument.
 *///from   www  . ja  v  a  2s. c o  m
private List<BundleContentGeneratorDefinition> getRequestedDefinitions(List<String> generators) {
    Stream<BundleContentGeneratorDefinition> stream = definitions.stream();
    if (generators == null || generators.isEmpty()) {
        // Filter out default generators
        stream = stream.filter(BundleContentGeneratorDefinition::isEnabledByDefault);
    } else {
        stream = stream.filter(def -> generators.contains(def.getId()));
    }
    return stream.sorted(Comparator.comparingInt(BundleContentGeneratorDefinition::getOrder))
            .collect(Collectors.toList());
}

From source file:com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates.java

public static void transitionToEnableState(SchemaVersionLifecycleContext context)
        throws SchemaNotFoundException, IncompatibleSchemaException, SchemaLifecycleException,
        SchemaBranchNotFoundException {/*from   w  ww .  ja  v a 2  s.  co m*/
    Long schemaVersionId = context.getSchemaVersionId();
    SchemaVersionService schemaVersionService = context.getSchemaVersionService();

    SchemaMetadataInfo schemaMetadataInfo = schemaVersionService.getSchemaMetadata(schemaVersionId);
    SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
    String schemaName = schemaMetadata.getName();
    SchemaValidationLevel validationLevel = schemaMetadata.getValidationLevel();

    SchemaVersionInfo schemaVersionInfo = schemaVersionService.getSchemaVersionInfo(schemaVersionId);
    int schemaVersion = schemaVersionInfo.getVersion();
    String schemaText = schemaVersionInfo.getSchemaText();
    List<SchemaVersionInfo> allEnabledSchemaVersions = schemaVersionService
            .getAllSchemaVersions(SchemaBranch.MASTER_BRANCH, schemaName).stream()
            .filter(x -> SchemaVersionLifecycleStates.ENABLED.getId().equals(x.getStateId()))
            .collect(Collectors.toList());

    if (!allEnabledSchemaVersions.isEmpty()) {
        if (validationLevel.equals(SchemaValidationLevel.ALL)) {
            for (SchemaVersionInfo curSchemaVersionInfo : allEnabledSchemaVersions) {
                int curVersion = curSchemaVersionInfo.getVersion();
                if (curVersion < schemaVersion) {
                    checkCompatibility(schemaVersionService, schemaMetadata, schemaText,
                            curSchemaVersionInfo.getSchemaText());
                } else {
                    checkCompatibility(schemaVersionService, schemaMetadata,
                            curSchemaVersionInfo.getSchemaText(), schemaText);
                }
            }
        } else if (validationLevel.equals(SchemaValidationLevel.LATEST)) {
            List<SchemaVersionInfo> sortedSchemaVersionInfos = new ArrayList<>(allEnabledSchemaVersions);
            sortedSchemaVersionInfos.sort(Comparator.comparingInt(SchemaVersionInfo::getVersion));
            int i = 0;
            int size = sortedSchemaVersionInfos.size();
            for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() < schemaVersion; i++) {
                String fromSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
                checkCompatibility(schemaVersionService, schemaMetadata, schemaText, fromSchemaText);
            }
            for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() > schemaVersion; i++) {
                String toSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
                checkCompatibility(schemaVersionService, schemaMetadata, toSchemaText, schemaText);
            }
        }
    }
    context.setState(ENABLED);
    context.updateSchemaVersionState();
}

From source file:enumj.EnumerableTest.java

@Test
public void testRange() {
    System.out.println("range");
    assertTrue(Enumerable.rangeInt(0, 100)
            .elementsEqual(Enumerable.range(0, 100, x -> x + 2, Comparator.comparingInt(x -> x))
                    .concat(Enumerable.range(1, 100, x -> x + 2, Comparator.comparingInt(x -> x))).sorted()));
}

From source file:enumj.EnumerableTest.java

@Test
public void testRangeClosed() {
    System.out.println("rangeClosed");
    assertTrue(Enumerable.rangeIntClosed(0, 100)
            .elementsEqual(Enumerable.rangeClosed(0, 100, x -> x + 2, Comparator.comparingInt(x -> x))
                    .concat(Enumerable.rangeClosed(1, 100, x -> x + 2, Comparator.comparingInt(x -> x)))
                    .sorted()));//from  ww  w  . j  a va2 s.c om
}

From source file:enumj.EnumerableTest.java

@Test
public void testSorted_Comparator() {
    System.out.println("sorted");
    assertTrue(Enumerable.rangeInt(-99, 1).map(x -> -x)
            .elementsEqual(Enumerable.rangeInt(0, 100).sorted(Comparator.comparingInt(x -> -x))));
}

From source file:enumj.EnumeratorTest.java

@Test
public void testMax() {
    System.out.println("max");
    assertEquals(Enumerator.rangeInt(0, 100).max(Comparator.comparingInt(i -> i)).get().intValue(), 99);
}

From source file:enumj.EnumeratorTest.java

@Test
public void testMin() {
    System.out.println("min");
    assertEquals(Enumerator.rangeInt(0, 100).min(Comparator.comparingInt(i -> i)).get().intValue(), 0);
}

From source file:enumj.EnumeratorTest.java

@Test
public void testSorted_Comparator() {
    System.out.println("sorted");
    assertTrue(Enumerator.rangeInt(0, 100).sorted(Comparator.comparingInt(i -> -i))
            .elementsEqual(Enumerator.rangeInt(0, 100).reverse()));
}

From source file:bwem.Graph.java

private int[] computeDistances(final ChokePoint start, final List<ChokePoint> targets) {
    final int[] distances = new int[targets.size()];

    TileImpl.getStaticMarkable().unmarkAll();

    final Queue<Pair<Integer, ChokePoint>> toVisit = new PriorityQueue<>(Comparator.comparingInt(a -> a.first));
    toVisit.offer(new Pair<>(0, start));

    int remainingTargets = targets.size();
    while (!toVisit.isEmpty()) {
        final Pair<Integer, ChokePoint> distanceAndChokePoint = toVisit.poll();
        final int currentDist = distanceAndChokePoint.first;
        final ChokePoint current = distanceAndChokePoint.second;
        final Tile currentTile = getMap().getData().getTile(current.getCenter().toTilePosition(),
                CheckMode.NO_CHECK);//from  w w w.ja  va 2 s.  c om
        //            bwem_assert(currentTile.InternalData() == currentDist);
        if (!(((TileImpl) currentTile).getInternalData() == currentDist)) {
            throw new IllegalStateException();
        }
        ((TileImpl) currentTile).setInternalData(0); // resets Tile::m_internalData for future usage
        ((TileImpl) currentTile).getMarkable().setMarked();

        for (int i = 0; i < targets.size(); ++i) {
            if (current == targets.get(i)) {
                distances[i] = currentDist;
                --remainingTargets;
            }
        }
        if (remainingTargets == 0) {
            break;
        }

        if (current.isBlocked() && (!current.equals(start))) {
            continue;
        }

        for (final Area pArea : new Area[] { current.getAreas().getFirst(), current.getAreas().getSecond() }) {
            for (final ChokePoint next : pArea.getChokePoints()) {
                if (!next.equals(current)) {
                    final int newNextDist = currentDist + distance(current, next);
                    final Tile nextTile = getMap().getData().getTile(next.getCenter().toTilePosition(),
                            CheckMode.NO_CHECK);
                    if (!((TileImpl) nextTile).getMarkable().isMarked()) {
                        if (((TileImpl) nextTile).getInternalData() != 0) { // next already in toVisit
                            if (newNextDist < ((TileImpl) nextTile).getInternalData()) { // nextNewDist < nextOldDist
                                                                                         // To update next's distance, we need to remove-insert it from toVisit:
                                                                                         //                                    bwem_assert(iNext != range.second);
                                final boolean removed = toVisit
                                        .remove(new Pair<>(((TileImpl) nextTile).getInternalData(), next));
                                if (!removed) {
                                    throw new IllegalStateException();
                                }
                                ((TileImpl) nextTile).setInternalData(newNextDist);
                                ((ChokePointImpl) next).setPathBackTrace(current);
                                toVisit.offer(new Pair<>(newNextDist, next));
                            }
                        } else {
                            ((TileImpl) nextTile).setInternalData(newNextDist);
                            ((ChokePointImpl) next).setPathBackTrace(current);
                            toVisit.offer(new Pair<>(newNextDist, next));
                        }
                    }
                }
            }
        }
    }

    //    //   bwem_assert(!remainingTargets);
    //        if (!(remainingTargets == 0)) {
    //            throw new IllegalStateException();
    //        }

    // reset Tile::m_internalData for future usage
    for (Pair<Integer, ChokePoint> distanceToChokePoint : toVisit) {
        ((TileImpl) getMap().getData().getTile(distanceToChokePoint.second.getCenter().toTilePosition(),
                CheckMode.NO_CHECK)).setInternalData(0);
    }

    return distances;
}

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

@Override
public XMLSlideShow report(final ReportData report, final boolean slidePerVisualizer)
        throws TemplateLoadException {
    final SlideShowTemplate template = loadTemplate();
    final XMLSlideShow ppt = template.getSlideShow();

    final Rectangle2D.Double pageAnchor = createPageAnchor(ppt);
    double width = pageAnchor.getWidth();
    double height = pageAnchor.getHeight();

    if (!slidePerVisualizer) {
        // If drawing multiple visualizations on a single slide, we need to render the charts first since adding
        //   chart objects directly via XML after calling slide.getShapes() or createShape() etc. will break things.
        Arrays.sort(report.getChildren(), Comparator.comparingInt(PowerPointServiceImpl::prioritizeCharts));
    }// w w  w.  jav  a2  s  .  co  m

    // As above, we need to have a separate slide to place our sizing textbox for calculations.
    XSLFSlide sizingSlide = ppt.createSlide();
    // This is the slide to draw on.
    XSLFSlide slide = ppt.createSlide();

    int shapeId = 1;
    boolean first = true;

    for (final ReportData.Child child : report.getChildren()) {
        if (slidePerVisualizer && !first) {
            sizingSlide = ppt.createSlide();
            slide = ppt.createSlide();
        }

        first = false;

        final ComposableElement data = child.getData();
        final Rectangle2D.Double anchor = new Rectangle2D.Double(pageAnchor.getMinX() + width * child.getX(),
                pageAnchor.getMinY() + height * child.getY(), width * child.getWidth(),
                height * child.getHeight());

        if (child.getMargin() >= 0) {
            final double margin = child.getMargin();
            final double marginX2 = margin * 2;
            final double textMargin = child.getTextMargin();

            if (anchor.getWidth() > marginX2) {
                double xCursor = anchor.getMinX() + margin, xWidthAvail = anchor.getWidth() - marginX2,
                        yCursor = anchor.getMinY() + margin, yHeightAvail = anchor.getHeight() - marginX2;
                XSLFTextBox sizingBox = null;

                final String title = child.getTitle();
                if (StringUtils.isNotEmpty(title) && yHeightAvail > 0) {
                    sizingBox = sizingSlide.createTextBox();
                    final Rectangle2D.Double sizingAnchor = new Rectangle2D.Double(xCursor, yCursor,
                            xWidthAvail, yHeightAvail);
                    sizingBox.setAnchor(sizingAnchor);
                    sizingBox.clearText();
                    addTextRun(sizingBox.addNewTextParagraph(), title, child.getFontSize(), Color.BLACK)
                            .setFontFamily(child.getFontFamily());

                    final double textHeight = sizingBox.getTextHeight() + textMargin;
                    yCursor += textHeight;
                    yHeightAvail -= textHeight;
                }

                if (yHeightAvail > 0) {
                    anchor.setRect(xCursor, yCursor, xWidthAvail, yHeightAvail);
                } else if (sizingBox != null) {
                    sizingSlide.removeShape(sizingBox);
                }
            }
        }

        if (data instanceof DategraphData) {
            addDategraph(template, slide, anchor, (DategraphData) data, shapeId, "relId" + shapeId);
            shapeId++;
        } else if (data instanceof ListData) {
            final ListData listData = (ListData) data;
            addList(imageSource, ppt, slide, anchor, false, listData, null, null);
        } else if (data instanceof MapData) {
            final MapData mapData = (MapData) data;
            addMap(slide, anchor, addPictureData(imageSource, ppt, mapData.getImage()), mapData.getMarkers(),
                    mapData.getPolygons());
        } else if (data instanceof SunburstData) {
            addSunburst(template, slide, anchor, (SunburstData) data, shapeId, "relId" + shapeId);
            shapeId++;
        } else if (data instanceof TableData) {
            final TableData tableData = (TableData) data;
            addTable(slide, anchor, tableData.getRows(), tableData.getCols(), tableData.getCells(), true);
        } else if (data instanceof TopicMapData) {
            addTopicMap(slide, anchor, (TopicMapData) data);
        } else if (data instanceof TextData) {
            addTextData(slide, anchor, (TextData) data);
        }

        if (slidePerVisualizer) {
            transferSizedTextboxes(ppt, slide, sizingSlide);
        }
    }

    if (!slidePerVisualizer) {
        transferSizedTextboxes(ppt, slide, sizingSlide);
    }

    return ppt;
}