Example usage for com.google.common.collect Iterators getOnlyElement

List of usage examples for com.google.common.collect Iterators getOnlyElement

Introduction

In this page you can find the example usage for com.google.common.collect Iterators getOnlyElement.

Prototype

public static <T> T getOnlyElement(Iterator<T> iterator) 

Source Link

Document

Returns the single element contained in iterator .

Usage

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Map<String, String> getProperties(Id.DatasetInstance dataset, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(dataset, scope).iterator()).getProperties();
}

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Map<String, String> getProperties(Id.Stream stream, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(stream, scope).iterator()).getProperties();
}

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Map<String, String> getProperties(Id.Stream.View view, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(view, scope).iterator()).getProperties();
}

From source file:org.tzi.use.gui.views.diagrams.behavior.communicationdiagram.CommunicationDiagram.java

@Override
protected PopupMenuInfo unionOfPopUpMenu() {
    // context menu on right mouse click
    JPopupMenu popupMenu = new JPopupMenu();
    PopupMenuInfo popupInfo = new PopupMenuInfo(popupMenu);

    // position for the popupMenu items
    int pos = 0;/*from  w w  w. j  a  v  a2s.co m*/

    final Set<PlaceableNode> selectedNodesSet = new HashSet<>();

    // Split selected nodes into model elements
    for (PlaceableNode node : fNodeSelection) {
        if (node instanceof ObjectNodeActivity) {
            selectedNodesSet.add(node);
        } else if (node instanceof LinkBoxNode) {
            selectedNodesSet.add(node);
        }
    }

    // This text is reused often
    String selectedObjectsText = null;
    if (selectedNodesSet.size() == 1) {
        selectedObjectsText = Iterators.getOnlyElement(selectedNodesSet.iterator()).getTextForMenu();
    } else if (selectedNodesSet.size() > 1) {
        selectedObjectsText = selectedNodesSet.size() + " Nodes";
    }

    if (!selectedNodesSet.isEmpty()) {
        popupMenu.insert(new ActionHideCommunicationDiagram("Hide " + selectedObjectsText, selectedNodesSet,
                fNodeSelection, fGraph, this), pos++);
        popupMenu.insert(new ActionHideCommunicationDiagram("Crop " + selectedObjectsText,
                getNoneSelectedNodes(selectedNodesSet), fNodeSelection, fGraph, this), pos++);
        popupMenu.insert(new JSeparator(), pos++);
    }

    final JMenu showHideCrop = new JMenu("Show/hide/crop objects");
    showHideCrop.add(fSelection.getSelectionWithOCLViewAction());
    showHideCrop.add(fSelection.getSelectionObjectView());
    popupMenu.insert(showHideCrop, pos++);

    if (CollectionUtil.exists(fGraph.iterator(), new Predicate<PlaceableNode>() {
        @Override
        public boolean apply(PlaceableNode input) {
            return !input.isVisible();
        }
    })) {
        final JMenuItem showAllObjects = new JMenuItem("Show hidden objects");
        showAllObjects.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                showAll();
                invalidateContent(true);
            }
        });
        popupMenu.insert(showAllObjects, pos++);
    }

    if (fGraph.size() > 0) {
        popupMenu.insert(fSelection.getSubMenuHideObject(), pos++);
    }

    if (CollectionUtil.exists(fGraph.iterator(), new Predicate<PlaceableNode>() {
        @Override
        public boolean apply(PlaceableNode input) {
            return input.isHidden();
        }
    })) {
        popupMenu.insert(fSelection.getSubMenuShowObject(), pos++);
    }

    popupMenu.insert(new JSeparator(), pos++);

    // new menu item "Show all life states"
    final JCheckBoxMenuItem objectBoxStatesItem = new JCheckBoxMenuItem("Show all life states");
    objectBoxStatesItem.setState(((CommunicationDiagramOptions) fOpt).isShowLifeStates());
    objectBoxStatesItem.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            ((CommunicationDiagramOptions) fOpt).setShowLifeStates(ev.getStateChange() == ItemEvent.SELECTED);
            invalidateContent(true);
        }
    });

    popupMenu.insert(objectBoxStatesItem, pos++);
    popupMenu.insert(new JSeparator(), pos++);

    final JCheckBoxMenuItem messagesItem = new JCheckBoxMenuItem("Show communication messages");
    messagesItem.setState(((CommunicationDiagramOptions) fOpt).isShowCommunicationMessages());
    messagesItem.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent ev) {
            ((CommunicationDiagramOptions) fOpt)
                    .setShowCommunicationMessages(ev.getStateChange() == ItemEvent.SELECTED);
            invalidateContent(true);
        }
    });

    popupMenu.insert(messagesItem, pos++);

    final JMenuItem navigationItem = new JMenuItem("Messages navigation");
    navigationItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            naviDialog = new MessagesNavigationDialog(fParent.getCommunicationDiagram());
            naviDialog.setVisible(true);
        }
    });

    popupMenu.insert(navigationItem, pos++);
    popupMenu.insert(new JSeparator(), pos++);

    final JCheckBoxMenuItem showAllCommandsItem = new JCheckBoxMenuItem("Show all Commands");
    showAllCommandsItem.setState(!messageFilter[0] && !messageFilter[1] && !messageFilter[2]
            && !messageFilter[3] && !messageFilter[4]);
    showAllCommandsItem.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (showAllCommandsItem.isSelected()) {
                for (int i = 0; i < messageFilter.length; i++) {
                    messageFilter[i] = false;
                }
                filterGraphByEvent(messageFilter);
            } else {
                for (int i = 0; i < messageFilter.length; i++) {
                    messageFilter[i] = true;
                }
                filterGraphByEvent(messageFilter);
            }

        }
    });

    popupMenu.insert(showAllCommandsItem, pos++);
    final JMenuItem showSomeCommandsItem = new JMenuItem("Commands to show...");
    showSomeCommandsItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ev) {
            createCmdChooseWindow();
        }
    });

    popupMenu.insert(showSomeCommandsItem, pos++);
    popupMenu.insert(new JSeparator(), pos++);

    final JMenuItem showSomeMessagesItem = new JMenuItem("Select message intervall...");
    showSomeMessagesItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            createMessageChooseWindow();
        }
    });

    popupMenu.insert(showSomeMessagesItem, pos++);

    final JCheckBoxMenuItem resetEnumItem = new JCheckBoxMenuItem("Relativ numbering");
    resetEnumItem.setState(resetEnum);
    resetEnumItem.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            resetEnum = resetEnumItem.getState();
            resetEnum(resetEnumItem.getState());
        }
    });

    popupMenu.insert(resetEnumItem, pos++);
    popupMenu.insert(new JSeparator(), pos++);

    final JMenuItem messageDepthItem = new JMenuItem("Select message depth...");
    messageDepthItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            createMessageDepthWindow();
        }
    });

    popupMenu.insert(messageDepthItem, pos++);

    final JCheckBoxMenuItem showAllMessagesItem = new JCheckBoxMenuItem("Show all messages");
    showAllMessagesItem.setState(showAllMessages);
    showAllMessagesItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            filterGraphByMessageDepth(0);
        }
    });
    popupMenu.insert(showAllMessagesItem, pos++);
    popupMenu.insert(new JSeparator(), pos++);

    final JMenuItem fixActorItem;

    if (actorSymbolNode.isUnmovable()) {
        // new menu item "Set actor movable"
        fixActorItem = new JMenuItem("Set actor movable");
        fixActorItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                actorSymbolNode.setStrategy(StrategyFixed.instance);
                actorSymbolNode.setUnmovable(false);
                getStatusBar().showTmpMessage("Actor was set to movable");
            }
        });
    } else {
        // new menu item "Set actor unmovable"
        fixActorItem = new JMenuItem("Set actor unmovable");
        fixActorItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                actorSymbolNode.setStrategy(StrategyUnmovable.instance);
                actorSymbolNode.setUnmovable(true);
                getStatusBar().showTmpMessage("Actor was set to unmovable");
            }
        });
    }

    popupMenu.insert(fixActorItem, pos++);

    final JMenuItem actorNameItem = new JMenuItem("Change actor name...");
    actorNameItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            actorNameDialog = new ActorChangeNameDialog(fParent.getCommunicationDiagram());
            actorNameDialog.setVisible(true);
        }
    });

    popupMenu.insert(actorNameItem, pos++);
    popupMenu.insert(new JSeparator(), pos++);

    popupMenu.add(getMenuItemCommentNode(popupInfo));
    popupMenu.addSeparator();
    popupMenu.add(getMenuAlign());
    popupMenu.add(getMenuItemAntiAliasing());
    popupMenu.add(getMenuItemShowGrid());

    addLayoutMenuItems(popupMenu);

    return popupInfo;
}

From source file:grakn.core.graql.reasoner.query.ReasonerQueryImpl.java

/**
 * returns id transform that would convert this query to a query alpha-equivalent to the query,
 * provided they are structurally equivalent
 * @param query for which the transform is to be constructed
 * @param unifier between this query and provided query
 * @return id transform//from   w  ww .j  a v  a2s .c o  m
 */
public Map<Variable, ConceptId> idTransform(ReasonerQueryImpl query, Unifier unifier) {
    Map<Variable, ConceptId> transform = new HashMap<>();
    this.getAtoms(IdPredicate.class).forEach(thisP -> {
        Collection<Variable> vars = unifier.get(thisP.getVarName());
        Variable var = !vars.isEmpty() ? Iterators.getOnlyElement(vars.iterator()) : thisP.getVarName();
        IdPredicate p2 = query.getIdPredicate(var);
        if (p2 != null)
            transform.put(thisP.getVarName(), p2.getPredicate());
    });
    return transform;
}

From source file:org.eclipse.sirius.diagram.ui.tools.internal.palette.PaletteManagerImpl.java

/**
 * Returns the palette entry contained in the given {@link PaletteContainer}
 * with the given id, of the given type. If none found,
 * {@link Options#newNone()} will be returned. If several found, we will log
 * a warning and return only one of the candidates.
 * /*from w  w  w  . j a  v a 2  s  .  com*/
 * @param <T>
 *            the type of the searched palette entry
 * @param container
 *            the container in which search for this palette entry
 * @param id
 *            the searched id
 * @param type
 *            the expected type
 * @return {@link Options#newNone()} if no matching candidate is found, or
 *         the found candidate (if several found, we will log a warning and
 *         return only one of the candidates).
 */
private <T extends PaletteEntry> Option<T> getPaletteEntry(PaletteContainer container, final String id,
        Class<T> type) {
    Option<T> matchingPaletteEntry = Options.newNone();
    UnmodifiableIterator<T> matchingPaletteEntries = Iterators
            .filter(Iterators.filter(container.getChildren().iterator(), type), new Predicate<T>() {
                @Override
                public boolean apply(T paletteEntry) {
                    return id.equals(paletteEntry.getId());
                }
            });
    try {
        matchingPaletteEntry = Options.newSome(Iterators.getOnlyElement(matchingPaletteEntries));
    } catch (NoSuchElementException e) {
        // Here no matching candidate has been found, we will return
        // Options.newNone
    } catch (IllegalArgumentException e) {
        DiagramPlugin.getDefault().logWarning(MessageFormat
                .format(Messages.PaletteManagerImpl_severalCandidatesInPalette, type.getName(), id));
        // Here no matching candidate has been found, we will return
        // Options.newNone
    }
    return matchingPaletteEntry;
}

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Set<String> getTags(Id.Application app, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(app, scope).iterator()).getTags();
}

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Set<String> getTags(Id.Artifact artifact, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(artifact, scope).iterator()).getTags();
}

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Set<String> getTags(Id.Program program, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(program, scope).iterator()).getTags();
}

From source file:co.cask.cdap.client.MetadataTestBase.java

protected Set<String> getTags(Id.DatasetInstance dataset, MetadataScope scope) throws Exception {
    return Iterators.getOnlyElement(getMetadata(dataset, scope).iterator()).getTags();
}