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.trancecode.xproc.step.XQueryStepProcessor.java

@Override
protected void execute(final StepInput input, final StepOutput output) {
    final Iterable<XdmNode> sourcesDoc = readSequencePort(input, XProcPorts.SOURCE);
    final XdmNode queryNode = input.readNode(XProcPorts.QUERY);
    LOG.trace("query = {}", queryNode.getStringValue());

    final Processor processor = input.getPipelineContext().getProcessor();
    final CollectionFinder oldCollResolver = processor.getUnderlyingConfiguration().getCollectionFinder();
    final XQCollectionResolver collResolver = new XQCollectionResolver(oldCollResolver);
    collResolver.addToCollection(sourcesDoc);
    processor.getUnderlyingConfiguration().setCollectionFinder(collResolver);
    final XQueryCompiler xQueryCompiler = processor.newXQueryCompiler();
    try {/*from   w  w  w  . j a va2s  .c om*/
        final XQueryEvaluator xQueryEvaluator = xQueryCompiler.compile(queryNode.getStringValue()).load();
        xQueryEvaluator.setContextItem(Iterables.getFirst(sourcesDoc, null));
        final Map<QName, String> params = getParameters(input);
        for (final Map.Entry<QName, String> param : params.entrySet()) {
            xQueryEvaluator.setExternalVariable(param.getKey(), new XdmAtomicValue(param.getValue()));
        }
        for (XdmItem item : xQueryEvaluator) {
            if (item.isAtomicValue()) {
                throw XProcExceptions.xc0057(SaxonLocation.of(queryNode));
            }
            output.writeNodes(XProcPorts.RESULT, (XdmNode) item);
        }
    } catch (final SaxonApiException e) {
        e.printStackTrace();
    } finally {
        processor.getUnderlyingConfiguration().setCollectionFinder(oldCollResolver);
    }
}

From source file:org.apache.metron.spout.pcap.HDFSWriterConfig.java

public List<String> getZookeeperServers() {
    List<String> out = new ArrayList<>();
    if (zookeeperQuorum != null) {
        for (String hostPort : Splitter.on(',').split(zookeeperQuorum)) {
            Iterable<String> tokens = Splitter.on(':').split(hostPort);
            String host = Iterables.getFirst(tokens, null);
            if (host != null) {
                out.add(host);//www .j av  a2s  .  co  m
            }
        }
    }
    return out;
}

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

@Override
public void sendOutgoingMessage(@Nonnull OutgoingMessage outgoingMessage) {
    new BukkitRunnable() {
        @Override//www .ja  v a  2s  .  c  om
        public void run() {
            Collection<? extends Player> players = BungeeMessenger.this.plugin.getBootstrap().getServer()
                    .getOnlinePlayers();
            Player p = Iterables.getFirst(players, null);
            if (p == null) {
                return;
            }

            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.writeUTF(outgoingMessage.asEncodedString());

            byte[] data = out.toByteArray();

            p.sendPluginMessage(BungeeMessenger.this.plugin.getBootstrap(), CHANNEL, data);
            cancel();
        }
    }.runTaskTimer(this.plugin.getBootstrap(), 1L, 100L);
}

From source file:com.willwinder.ugs.platform.surfacescanner.AutoLevelPreview.java

@Override
public void draw(GLAutoDrawable drawable, boolean idle, Point3d machineCoord, Point3d workCoord,
        Point3d objectMin, Point3d objectMax, double scaleFactor, Point3d mouseWorldCoordinates,
        Point3d rotation) {//from   www  .  j  ava2 s .  c o  m

    // Don't draw something invalid.
    if (positions == null || positions.isEmpty()) {
        return;
    }

    Position first = Iterables.getFirst(positions, null);
    Units unit = first.getUnits();

    double objectX = objectMax.x - objectMin.x;
    double objectY = objectMax.y - objectMin.y;
    double diameter = Math.max(objectX * 0.005, objectY * 0.005);

    double minx, miny, minz;
    double maxx, maxy, maxz;

    minx = maxx = first.x;
    miny = maxy = first.y;
    minz = maxz = first.z;

    GL2 gl = drawable.getGL().getGL2();
    gl.glPushMatrix();
    // Scale inch to mm if needed
    double scale = UnitUtils.scaleUnits(unit, Units.MM);
    if (unit != Units.MM) {
        gl.glScaled(scale, scale, scale);
    }

    // Balls indicating the probe start locations.
    gl.glColor4fv(new float[] { 0.1f, 0.1f, 0.1f, 1.0f }, 0);
    for (Position p : positions) {
        gl.glPushMatrix();
        gl.glTranslated(p.x, p.y, p.z);
        glut.glutSolidSphere(diameter / scale, 7, 7);

        // update min/max
        minx = Math.min(minx, p.x);
        maxx = Math.max(maxx, p.x);
        miny = Math.min(miny, p.y);
        maxz = Math.max(maxz, p.z);
        minz = Math.min(minz, p.z);
        maxy = Math.max(maxy, p.y);
        gl.glPopMatrix();
    }

    // Outline of probe area
    gl.glPushMatrix();
    gl.glTranslated((minx + maxx) / 2, (miny + maxy) / 2, (minz + maxz) / 2);
    gl.glScaled(maxx - minx, maxy - miny, maxz - minz);
    gl.glColor4fv(new float[] { 0.3f, 0, 0, 0.1f }, 0);
    glut.glutWireCube((float) 1.);
    gl.glPopMatrix();

    drawProbedSurface(gl);
    gl.glPopMatrix();
}

From source file:ratpack.server.internal.BaseDirFinder.java

private static Path determineBaseDir(Path configPath) {
    Path baseDir = configPath.getParent();
    if (baseDir == null && configPath.getFileSystem().provider() instanceof ZipFileSystemProvider) {
        baseDir = Iterables.getFirst(configPath.getFileSystem().getRootDirectories(), null);
    }/*from ww  w  .  jav  a2 s . co  m*/
    if (baseDir == null) {
        throw new IllegalStateException("Cannot determine base dir given config resource: " + configPath);
    }
    return baseDir;
}

From source file:controllers.modules.base.Module.java

public <T extends ViewModel> T findViewModel(Class<T> viewModelClass, Iterable<ViewModel> exclude,
        T defaultViewModel) {/*from  ww  w.j av a  2  s.  c  om*/
    exclude = ObjectUtils.defaultIfNull(exclude, Lists.<ViewModel>newArrayList());
    return Iterables.getFirst(
            Iterables.filter(Iterables.filter(this.viewModels,
                    Predicates.not(Predicates.in(Lists.newArrayList(exclude)))), viewModelClass),
            defaultViewModel);
}

From source file:com.github.blacklocus.rdsecho.utl.Route53Find.java

public Optional<ResourceRecordSet> resourceRecordSet(String hostedZoneId) {
    return Optional.fromNullable(Iterables.getFirst(resourceRecordSets(hostedZoneId), null));
}

From source file:com.b2international.commons.dynamic.AbstractDynamicMap.java

@Override
public DynamicValue getFirstPropertyValue(String property, Object defaultValue) {
    Entry firstEntry = Iterables.getFirst(getProperty(property), null);
    return (firstEntry == null) ? DynamicValueImpl.create(defaultValue) : firstEntry.getValue();
}

From source file:com.kolich.logback.LogbackCapturingAppender.java

/**
 * Returns the first logger event message in the {@link List} of captured logging messages.
 *
 * Will return null if no logging events have been captured.
 *///from  w ww .ja  va 2 s. c o  m
public String getFirstMessage() {
    return Iterables.getFirst(getMessages(), null);
}

From source file:com.clarkparsia.geneious.GeneiousUtils.java

public static SequenceAnnotation findParentAnnotation(SequenceAnnotation sequence,
        Iterable<SequenceAnnotation> annotations) {
    SequenceAnnotationInterval interval = Iterables.getFirst(sequence.getIntervals(), null);
    return interval == null ? null : findParentAnnotation(interval, annotations);
}