Example usage for com.google.common.collect Iterables getFirst

List of usage examples for com.google.common.collect Iterables getFirst

Introduction

In this page you can find the example usage for com.google.common.collect Iterables getFirst.

Prototype

@Nullable
public static <T> T getFirst(Iterable<? extends T> iterable, @Nullable T defaultValue) 

Source Link

Document

Returns the first element in iterable or defaultValue if the iterable is empty.

Usage

From source file:org.sonar.java.checks.StringLiteralDuplicatedCheck.java

@Override
public void scanFile(JavaFileScannerContext context) {
    occurrences.clear();/*ww  w  . j  a  va 2s .c  om*/
    scan(context.getTree());
    for (String entry : occurrences.keySet()) {
        Collection<LiteralTree> literalTrees = occurrences.get(entry);
        int literalOccurence = literalTrees.size();
        if (literalOccurence >= threshold) {
            List<JavaFileScannerContext.Location> flow = new ArrayList<>();
            for (Tree element : literalTrees) {
                flow.add(new JavaFileScannerContext.Location("Duplication", element));
            }
            context.reportIssue(this, Iterables.getFirst(literalTrees, null),
                    "Define a constant instead of duplicating this literal " + entry + " " + literalOccurence
                            + " times.",
                    flow, literalOccurence);
        }
    }
}

From source file:com.lyndir.love.webapp.data.User.java

@Override
public String getLocalizedInstance() {
    return msgs.instance(Iterables.getFirst(getEmailAddresses(), "<no email>"));
}

From source file:com.jiuxian.mossrose.quartz.QuartzJobOperation.java

private JobRuntimeInfo getJobInfoByKey(final JobKey jobKey) {
    JobRuntimeInfo job = null;//from w  w  w  .j a  v a  2s . c o m
    try {
        final JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        // Only one trigger for a job
        final Trigger trigger = Iterables.getFirst(scheduler.getTriggersOfJob(jobKey), null);

        if (trigger != null) {
            job = new JobRuntimeInfo();

            job.setId(jobKey.getName());
            job.setGroup(jobKey.getGroup());
            job.setDescription(jobDetail.getDescription());
            job.setStartTime(trigger.getStartTime());
            job.setEndTime(trigger.getEndTime());
            job.setPreviousFireTime(trigger.getPreviousFireTime());
            job.setNextFireTime(trigger.getNextFireTime());
            job.setState(State.valueOf(scheduler.getTriggerState(trigger.getKey()).name()));
            if (trigger instanceof CronTrigger) {
                job.setCron(((CronTrigger) trigger).getCronExpression());
            }
        }
    } catch (SchedulerException eInGettingJobInfo) {
        LOGGER.error(eInGettingJobInfo.getMessage(), eInGettingJobInfo);
    }
    return job;
}

From source file:org.activityinfo.ui.client.component.form.field.SuggestBoxWidget.java

@Override
public Promise<Void> setValue(ReferenceValue value) {
    ResourceId newValue = Iterables.getFirst(value.getResourceIds(), null);
    if (!Objects.equals(newValue, this.value)) {
        this.value = newValue;
        if (newValue == null) {
            suggestBox.setValue(null);/*from   w  ww.  ja v a2s  .c  o  m*/
        } else {
            suggestBox.setValue(findDisplayLabel(newValue));
        }
    }
    return Promise.done();
}

From source file:ezbake.common.http.request.Request.java

private static String getFirst(Multimap<String, String> m, String name) {
    Collection<String> values = m.get(name.toUpperCase());
    if (values == null || values.isEmpty())
        return null;
    return Iterables.getFirst(values, null);
}

From source file:org.janusgraph.graphdb.relations.CacheEdge.java

@Override
public InternalRelation it() {
    InternalRelation it = null;//from  ww w  .ja  v a2s .  com
    InternalVertex startVertex = getVertex(0);

    if (startVertex.hasAddedRelations() && startVertex.hasRemovedRelations()) {
        //Test whether this relation has been replaced
        final long id = super.longId();
        Iterable<InternalRelation> previous = startVertex.getAddedRelations(new Predicate<InternalRelation>() {
            @Override
            public boolean apply(@Nullable InternalRelation internalRelation) {
                return (internalRelation instanceof StandardEdge)
                        && ((StandardEdge) internalRelation).getPreviousID() == id;
            }
        });
        assert Iterables.size(previous) <= 1 || (isLoop() && Iterables.size(previous) == 2);
        it = Iterables.getFirst(previous, null);
    }

    if (it != null)
        return it;

    return super.it();
}

From source file:com.facebook.presto.tpch.testing.SampledTpchRecordSetProvider.java

@Override
public RecordSet getRecordSet(ConnectorSession session, ConnectorSplit split,
        List<? extends ColumnHandle> columns) {
    int sampleWeightField = -1;
    for (int i = 0; i < columns.size(); i++) {
        ColumnHandle column = columns.get(i);
        if (column instanceof TpchColumnHandle && ((TpchColumnHandle) column).getColumnName()
                .equals(SampledTpchMetadata.SAMPLE_WEIGHT_COLUMN_NAME)) {
            sampleWeightField = i;/*w  w w .java2 s. c o  m*/
            break;
        }
    }
    List<? extends ColumnHandle> delegatedColumns = new ArrayList<>(columns);
    if (sampleWeightField > -1) {
        delegatedColumns.remove(sampleWeightField);
        RecordSet recordSet;
        if (delegatedColumns.isEmpty()) {
            // Pick a random column, so that we can figure out how many rows there are
            TpchSplit tpchSplit = (TpchSplit) split;
            ColumnHandle column = Iterables
                    .getFirst(metadata.getColumnHandles(session, tpchSplit.getTableHandle()).values(), null);
            requireNonNull(column, "Could not find any columns");
            recordSet = new EmptyRecordSet(super.getRecordSet(session, split, ImmutableList.of(column)));
        } else {
            recordSet = super.getRecordSet(session, split, delegatedColumns);
        }
        return new SampledTpchRecordSet(recordSet, sampleWeightField, sampleWeight);
    } else {
        return super.getRecordSet(session, split, columns);
    }
}

From source file:com.android.tools.idea.avdmanager.AvdConfigurationOptionHelpPanel.java

public void setDescriptionText(@Nullable String descriptionText) {
    if (descriptionText == null || descriptionText.isEmpty()) {
        ((CardLayout) myContentPanel.getLayout()).show(myContentPanel, "NothingSelected");
    } else {//from   www . j  ava 2  s. c  om
        ((CardLayout) myContentPanel.getLayout()).show(myContentPanel, "Info");
        Iterable<String> iterable = Splitter.on('|').split(descriptionText);
        myTitle.setText("<html>" + Iterables.getFirst(iterable, null) + "</html>");
        myDescription.setText("<html>" + Iterables.getLast(iterable, null) + "</html>");
    }
    Window window = SwingUtilities.getWindowAncestor(this);
    if (window != null) {
        window.pack();
    }
}

From source file:me.lucko.luckperms.bukkit.messaging.BungeeMessagingService.java

@Override
protected void sendMessage(String channel, String message) {
    new BukkitRunnable() {
        @Override/*from  w  w  w  . j  av a  2  s. c  o m*/
        public void run() {
            Collection<? extends Player> players = plugin.getServer().getOnlinePlayers();
            if (players.isEmpty()) {
                return;
            }

            Player p = Iterables.getFirst(players, null);

            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF(message);

            byte[] data = out.toByteArray();

            p.sendPluginMessage(plugin, channel, data);
            cancel();
        }
    }.runTaskTimer(plugin, 1L, 100L);
}

From source file:me.lucko.luckperms.sponge.messaging.BungeeMessagingService.java

@Override
protected void sendMessage(String channel, String message) {
    plugin.getSpongeScheduler().createTaskBuilder().interval(10, TimeUnit.SECONDS).execute(task -> {
        Collection<Player> players = plugin.getGame().getServer().getOnlinePlayers();
        if (players.isEmpty()) {
            return;
        }/*www . j  a v  a  2s  . c o  m*/

        Player p = Iterables.getFirst(players, null);
        this.channel.sendTo(p, buf -> buf.writeUTF(message));

        task.cancel();
    }).submit(plugin);
}