Example usage for com.google.common.base Optional or

List of usage examples for com.google.common.base Optional or

Introduction

In this page you can find the example usage for com.google.common.base Optional or.

Prototype

@Beta
public abstract T or(Supplier<? extends T> supplier);

Source Link

Document

Returns the contained instance if it is present; supplier.get() otherwise.

Usage

From source file:com.pinterest.teletraan.resource.EnvCapacitys.java

@POST
public void add(@PathParam("envName") String envName, @PathParam("stageName") String stageName,
        @QueryParam("capacityType") Optional<CapacityType> capacityType, @NotEmpty String name,
        @Context SecurityContext sc) throws Exception {
    EnvironBean envBean = Utils.getEnvStage(environDAO, envName, stageName);
    authorizer.authorize(sc, new Resource(envBean.getEnv_name(), Resource.Type.ENV), Role.OPERATOR);
    String operator = sc.getUserPrincipal().getName();
    if (capacityType.or(CapacityType.GROUP) == CapacityType.GROUP) {
        groupDAO.addGroupCapacity(envBean.getEnv_id(), name);
    } else {// w w  w.  j av a 2s  .co  m
        groupDAO.addHostCapacity(envBean.getEnv_id(), name);
    }
    LOG.info("Successfully added {} to env {}/{} capacity config by {}.", name, envName, stageName, operator);
}

From source file:com.pinterest.teletraan.resource.EnvCapacitys.java

@DELETE
public void delete(@PathParam("envName") String envName, @PathParam("stageName") String stageName,
        @QueryParam("capacityType") Optional<CapacityType> capacityType, @NotEmpty String name,
        @Context SecurityContext sc) throws Exception {
    EnvironBean envBean = Utils.getEnvStage(environDAO, envName, stageName);
    authorizer.authorize(sc, new Resource(envBean.getEnv_name(), Resource.Type.ENV), Role.OPERATOR);
    String operator = sc.getUserPrincipal().getName();
    if (capacityType.or(CapacityType.GROUP) == CapacityType.GROUP) {
        groupDAO.removeGroupCapacity(envBean.getEnv_id(), name);
    } else {//from   w ww.  j  a va 2 s .  c  o m
        groupDAO.removeHostCapacity(envBean.getEnv_id(), name);
    }
    LOG.info("Successfully deleted {} from env {}/{} capacity config by {}.", name, envName, stageName,
            operator);
}

From source file:org.locationtech.geogig.remote.LocalRemoteRepo.java

/**
 * Push all new objects from the specified {@link Ref} to the given refspec.
 * //from  w  w  w.  j a va2s  .c o m
 * @param ref the local ref that points to new commit data
 * @param refspec the refspec to push to
 */
@Override
public void pushNewData(final Ref ref, final String refspec, final ProgressListener progress)
        throws SynchronizationException {

    Optional<Ref> remoteRef = remoteGeoGig.command(RefParse.class).setName(refspec).call();
    remoteRef = remoteRef.or(remoteGeoGig.command(RefParse.class).setName(Ref.TAGS_PREFIX + refspec).call());
    checkPush(ref, remoteRef);

    CommitTraverser traverser = getPushTraverser(remoteRef);

    traverser.traverse(ref.getObjectId());
    progress.setDescription("Uploading objects to " + refspec);
    progress.setProgress(0);
    while (!traverser.commits.isEmpty()) {
        ObjectId commitId = traverser.commits.pop();
        walkHead(commitId, false, progress);
    }

    String nameToSet = remoteRef.isPresent() ? remoteRef.get().getName() : Ref.HEADS_PREFIX + refspec;

    Ref updatedRef = remoteGeoGig.command(UpdateRef.class).setName(nameToSet).setNewValue(ref.getObjectId())
            .call().get();

    Ref remoteHead = headRef();
    if (remoteHead instanceof SymRef) {
        if (((SymRef) remoteHead).getTarget().equals(updatedRef.getName())) {
            remoteGeoGig.command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(ref.getName()).call();
            RevCommit commit = remoteGeoGig.getRepository().getCommit(ref.getObjectId());
            remoteGeoGig.getRepository().workingTree().updateWorkHead(commit.getTreeId());
            remoteGeoGig.getRepository().index().updateStageHead(commit.getTreeId());
        }
    }
}

From source file:org.apache.gobblin.instrumented.writer.InstrumentedDataWriterBase.java

protected InstrumentedDataWriterBase(State state, Optional<Class<?>> classTag) {
    this.closer = Closer.create();
    this.instrumentationEnabled = GobblinMetrics.isEnabled(state);
    this.metricContext = this.closer
            .register(Instrumented.getMetricContext(state, classTag.or(this.getClass())));

    if (this.instrumentationEnabled) {
        this.writerMetricsUpdater = Optional.of(buildWriterMetricsUpdater());
        scheduleWriterMetricsUpdater(this.writerMetricsUpdater.get(), getWriterMetricsUpdaterInterval(state));
    } else {/* w  ww  .  j  a v a  2  s.  co  m*/
        this.writerMetricsUpdater = Optional.absent();
    }

    regenerateMetrics();
}

From source file:org.apache.gobblin.instrumented.extractor.InstrumentedExtractorBase.java

protected InstrumentedExtractorBase(WorkUnitState workUnitState, Optional<Class<?>> classTag) {
    super();/*from  ww w. ja v  a2 s.c  o  m*/
    this.closer = Closer.create();

    this.instrumentationEnabled = GobblinMetrics.isEnabled(workUnitState);

    this.metricContext = this.closer.register(Instrumented.getMetricContext(workUnitState,
            classTag.or(this.getClass()), generateTags(workUnitState)));

    regenerateMetrics();
}

From source file:org.jclouds.elasticstack.compute.functions.StandardDriveToWellKnownImage.java

private OsFamily extractOsFamily(final String name) {
    final String lowerCaseName = name.toLowerCase();
    Optional<OsFamily> family = tryFind(asList(OsFamily.values()), new Predicate<OsFamily>() {
        @Override/*w w  w . ja  v a  2s.c  o  m*/
        public boolean apply(OsFamily input) {
            return lowerCaseName.startsWith(input.name().toLowerCase());
        }
    });

    if (!family.isPresent()) {
        logger.warn("could not find the operating system family for image: %s", name);
    }

    return family.or(OsFamily.UNRECOGNIZED);
}

From source file:org.locationtech.geogig.remotes.internal.LocalRemoteRepo.java

@Override
public void pushNewData(final Repository local, final Ref ref, final String refspec,
        final ProgressListener progress) throws SynchronizationException {

    Optional<Ref> remoteRef = remoteRepository.command(RefParse.class).setName(refspec).call();
    remoteRef = remoteRef
            .or(remoteRepository.command(RefParse.class).setName(Ref.TAGS_PREFIX + refspec).call());
    checkPush(local, ref, remoteRef);/*from   www.j  a v a2 s  .c o  m*/

    CommitTraverser traverser = getPushTraverser(local, remoteRef);

    traverser.traverse(ref.getObjectId());
    progress.setDescription("Uploading objects to " + refspec);
    progress.setProgress(0);
    while (!traverser.commits.isEmpty()) {
        ObjectId commitId = traverser.commits.pop();
        walkHead(commitId, local, remoteRepository, progress);
    }

    String nameToSet = remoteRef.isPresent() ? remoteRef.get().getName() : Ref.HEADS_PREFIX + refspec;

    Ref updatedRef = remoteRepository.command(UpdateRef.class).setName(nameToSet).setNewValue(ref.getObjectId())
            .call().get();

    Ref remoteHead = headRef().orNull();
    if (remoteHead instanceof SymRef) {
        if (((SymRef) remoteHead).getTarget().equals(updatedRef.getName())) {
            remoteRepository.command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(ref.getName()).call();
            RevCommit commit = remoteRepository.getCommit(ref.getObjectId());
            remoteRepository.workingTree().updateWorkHead(commit.getTreeId());
            remoteRepository.index().updateStageHead(commit.getTreeId());
        }
    }
}

From source file:lcmc.cluster.service.NetworkService.java

public Map<String, Integer> getNetworksIntersection(final Collection<Host> hosts) {
    Optional<Map<String, Integer>> networksIntersection = Optional.absent();

    for (final Host host : hosts) {
        final HostNetworks hostNetworks = hostNetInterfacesByHost.get(host);
        if (hostNetworks != null) {
            networksIntersection = hostNetworks.getNetworksIntersection(networksIntersection);
        }//from w  w  w. j  a v a  2s .  c  o m
    }
    return networksIntersection.or(new HashMap<String, Integer>());
}

From source file:org.mayocat.image.DefaultImageService.java

@Override
public Optional<Dimension> newDimension(Rectangle boundaries, Optional<Integer> width, Optional<Integer> height)
        throws IOException {
    int imageWidth = (int) Math.round(boundaries.getWidth());
    int imageHeight = (int) Math.round(boundaries.getHeight());

    if (imageHeight == height.or(imageHeight) && imageWidth == width.or(imageWidth)) {
        return Optional.absent();
    }/*from  ww w.ja va  2  s. c  om*/

    int requestedWidth = width.or(-1);
    int requestedHeight = height.or(-1);
    int newWidth = imageWidth;
    int newHeight = imageHeight;

    double aspectRatio = (double) imageWidth / (double) imageHeight;

    if (requestedWidth <= 0 || requestedWidth >= imageWidth) {
        // Ignore the requested width. Check the requested height.
        if (requestedHeight > 0 && requestedHeight < imageHeight) {
            // Reduce the height, keeping aspect ratio.
            newWidth = (int) (requestedHeight * aspectRatio);
            newHeight = requestedHeight;
        }
    } else if (requestedHeight <= 0 || requestedHeight >= imageHeight) {
        // Ignore the requested height. Reduce the width, keeping aspect ratio.
        newWidth = requestedWidth;
        newHeight = (int) (requestedWidth / aspectRatio);
    } else {
        // Reduce the width and check if the corresponding height is less than the requested height.
        newWidth = requestedWidth;
        newHeight = (int) (requestedWidth / aspectRatio);
        if (newHeight > requestedHeight) {
            // We have to reduce the height instead and compute the width based on it.
            newWidth = (int) (requestedHeight * aspectRatio);
            newHeight = requestedHeight;
        }
    }

    if (newWidth != imageWidth && newHeight != imageHeight) {
        return Optional.of(new Dimension(newWidth, newHeight));
    } else {
        return Optional.absent();
    }
}

From source file:com.google.errorprone.dataflow.nullnesspropagation.NullnessPropagationTransferCases2.java

public void optionalMethodsReturnNonNullUnlessAnnotated() {
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(Optional.absent());
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(Optional.of(""));
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(Optional.fromNullable(null));
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(Optional.of("Test"));
    Optional<String> myOptional = Optional.absent();
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(myOptional.get());
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(myOptional.or(""));
    // BUG: Diagnostic contains: (Non-null)
    triggerNullnessChecker(myOptional.asSet());

    // BUG: Diagnostic contains: (Nullable)
    triggerNullnessChecker(myOptional.orNull());
}