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

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

Introduction

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

Prototype

public static <T> Optional<T> of(T reference) 

Source Link

Document

Returns an Optional instance containing the given non-null reference.

Usage

From source file:com.github.rinde.rinsim.ui.renderers.PointUtil.java

static Optional<Point> intersectionPoint(Point own1, Point own2, Point oth1, Point oth2) {

    final double dx1 = own2.x - own1.x;
    final double dy1 = own2.y - own1.y;
    final double dx2 = oth2.x - oth1.x;
    final double dy2 = oth2.y - oth1.y;

    final double angle1 = Math.PI + Math.atan2(-dy1, -dx1);
    final double angle2 = Math.PI + Math.atan2(-dy2, -dx2);

    final double sin1 = Math.sin(angle1);
    final double sin2 = Math.sin(angle2);
    final double cos1 = Math.cos(angle1);
    final double cos2 = Math.cos(angle2);

    final double d = sin1 * cos2 - sin2 * cos1;
    if (Math.abs(d) < TOLERANCE) {
        return Optional.absent();
    }/*from   ww  w  .j ava 2s. c o  m*/
    final double d1 = Math.hypot(dx1, dy1);
    final double d2 = Math.hypot(dx2, dy2);
    final double offset1 = own2.x * own1.y - own1.x * own2.y / d1;
    final double offset2 = oth2.x * oth1.y - oth1.x * oth2.y / d2;

    return Optional.of(new Point((cos1 * offset2 - cos2 * offset1) / d, (sin1 * offset2 - sin2 * offset1) / d));
}

From source file:fi.vm.sade.osoitepalvelu.kooste.common.util.CriteriaHelper.java

public static Optional<Criteria> inAnyKoodiVersion(Criteria c, String key, Collection<String> koodis) {
    if (koodis == null || koodis.isEmpty()) {
        return Optional.absent();
    }/* w w w  .  jav  a  2s. co m*/

    Criteria[] criteria = new Criteria[koodis.size()];
    int i = 0;
    for (String koodi : koodis) {
        // Strip the version part if specified:
        Matcher m = KOODI_WITH_VERSION.matcher(koodi);
        if (m.find()) {
            koodi = m.group(1);
        }
        criteria[i++] = Criteria.where(key).regex(Pattern.compile(koodi + OPTIONAL_KOODI_VERSION_POSTFIX));
    }
    return Optional.of(c.orOperator(criteria));
}

From source file:com.stepstone.sonar.plugin.coldfusion.cflint.xml.TagAttribute.java

protected Optional<String> getAttributeValue(String name, XMLStreamReader stream) {

    for (int i = 0; i < stream.getAttributeCount(); i++) {

        if (name.equalsIgnoreCase(stream.getAttributeLocalName(i))) {
            return Optional.of(StringUtils.trimToEmpty(stream.getAttributeValue(i)));
        }//from  w  w w  . j a  v  a 2  s .  c om
    }

    return Optional.absent();
}

From source file:com.spotify.helios.cli.command.TargetAndClient.java

public TargetAndClient(final Target target, final HeliosClient client) {
    this.target = Optional.of(Preconditions.checkNotNull(target));
    this.client = client;
}

From source file:org.terasology.commonworld.geom.BoundingBox.java

/**
 * @param pts a collection of Vector2is//from   w w  w .  ja  va2s . co m
 * @return the bounding rectangle of pts or absent() if pts is empty
 */
public static Optional<Rectangle> getBoundingRect(Collection<Vector2i> pts) {
    if (pts.isEmpty()) {
        return Optional.absent();
    }

    BoundingBox bbox = new BoundingBox(pts);
    return Optional.of(bbox.toRectangle());
}

From source file:im.dadoo.teak.biz.bo.impl.DefaultSignBO.java

@Override
public Optional<UserPO> signin(String name, String password) {
    UserPO userPO = this.userDAO.findByName(name);
    if (userPO != null && userPO.getPassword().equals(password)) {
        return Optional.of(userPO);
    } else {/*ww  w . j  ava2  s . c  o  m*/
        return Optional.absent();
    }
}

From source file:org.jclouds.compute.util.AutomaticHardwareIdSpec.java

public static AutomaticHardwareIdSpec parseId(String hardwareId) {
    AutomaticHardwareIdSpec spec = new AutomaticHardwareIdSpec();
    String hardwareSpec = hardwareId.substring(10);
    Map<String, String> specValues = Splitter.on(';').trimResults().omitEmptyStrings()
            .withKeyValueSeparator('=').split(hardwareSpec);
    if (!specValues.containsKey("ram") || !specValues.containsKey("cores")) {
        throw new IllegalArgumentException(String.format(
                "Omitted keys on hardwareId: %s. Please set number " + "of cores and ram amount.", hardwareId));
    }/*from   ww  w . ja  v  a  2s .  c o m*/
    if (specValues.containsKey("disk")) {
        float disk = Float.parseFloat(specValues.get("disk"));
        if (disk > 0.0f) {
            spec.disk = Optional.of(disk);
        } else {
            throw new IllegalArgumentException(String.format("Invalid disk value: %s", hardwareId));
        }
    }
    spec.ram = Integer.parseInt(specValues.get("ram"));
    spec.cores = Double.parseDouble(specValues.get("cores"));
    return spec;
}

From source file:org.eclipse.mylyn.internal.wikitext.commonmark.inlines.BackslashEscapeSpan.java

@Override
public Optional<? extends Inline> createInline(Cursor cursor) {
    char c = cursor.getChar();
    if (c == '\\' && cursor.hasNext()) {
        if (cursor.getNext() == '\n') {
            return Optional.of(new HardLineBreak(cursor.getLineAtOffset(), cursor.getOffset(), 2));
        } else if (ESCAPABLE.matches(cursor.getNext())) {
            return Optional
                    .of(new EscapedCharacter(cursor.getLineAtOffset(), cursor.getOffset(), cursor.getNext()));
        }//from   ww  w. j a  v  a  2s .  co  m
    }
    return Optional.absent();
}

From source file:springfox.documentation.swagger1.web.ApiListingMerger.java

public static Optional<ApiListing> mergedApiListing(Collection<ApiListing> apiListings) {
    if (nullToEmptyList(apiListings).size() > 1) {
        ApiListing merged = new ApiListing();
        merged.setSwaggerVersion("1.2");
        merged.setPosition(0);/*w w  w .  j  a va2 s . co  m*/
        for (ApiListing each : apiListings) {
            merged.setApiVersion(each.getApiVersion());
            merged.setBasePath(each.getBasePath());
            merged.setResourcePath(each.getResourcePath());
            merged.setDescription(each.getDescription());
            merged.appendAuthorizations(each.getAuthorizations());
            merged.appendApis(each.getApis());
            merged.appendProtocols(newHashSet(each.getProtocols()));
            merged.appendConsumes(newHashSet(each.getConsumes()));
            merged.appendModels(each.getModels());
            merged.appendProduces(newHashSet(each.getProduces()));
        }
        return Optional.of(merged);
    }
    return from(nullToEmptyList(apiListings)).first();
}

From source file:org.sfs.util.ExceptionHelper.java

public static <T extends Throwable> Optional<T> unwrapCause(Class<T> exception, Throwable t) {
    int counter = 0;
    Throwable result = t;/*from   w ww  .j av  a 2  s  . c om*/
    while (result != null && !exception.isAssignableFrom(result.getClass())) {
        if (result.getCause() == null) {
            return Optional.absent();
        }
        if (result.getCause() == result) {
            return Optional.absent();
        }
        if (counter++ > 10) {
            LOGGER.warn("Exception cause unwrapping ran for 10 levels...", t);
            return Optional.absent();
        }
        result = result.getCause();
    }
    if (exception.isAssignableFrom(result.getClass())) {
        return Optional.of((T) result);
    } else {
        return Optional.absent();
    }
}