Example usage for java.util Optional isPresent

List of usage examples for java.util Optional isPresent

Introduction

In this page you can find the example usage for java.util Optional isPresent.

Prototype

public boolean isPresent() 

Source Link

Document

If a value is present, returns true , otherwise false .

Usage

From source file:com.javaeeeee.repositories.UsersRepositoryTest.java

/**
 * A methods tests that empty optional is returned is user is nonexistent.
 *//*  w w w .  j  a  v  a 2  s  .c o m*/
@Test
public void findByUsernameShouldReturnEmpty() {
    final String name = "Phil";
    entityManager.persist(new User(name, "1"));
    Optional<User> optional = usersRepository.findByUsername(name + "mmm");

    Assert.assertFalse(optional.isPresent());
}

From source file:org.obiba.mica.search.aggregations.TaxonomyAggregationMetaDataProvider.java

private Map<String, LocalizedMetaData> getAllLocalizedMetadata(String aggregation) {
    Optional<Vocabulary> vocabulary = getVocabulary(aggregation);

    if (vocabulary.isPresent()) {
        Map<String, LocalizedMetaData> r = Maps.newHashMap();

        for (Term t : vocabulary.get().getTerms()) {
            LocalizedString title = new LocalizedString();
            title.putAll(t.getTitle());/*from w ww .j a va 2 s.  co  m*/
            LocalizedString description = new LocalizedString();
            description.putAll(t.getDescription());
            String className = t.getAttributeValue("className");
            if (Strings.isNullOrEmpty(className)) {
                className = t.getClass().getSimpleName();
            }
            if (!r.containsKey(t.getName())) {
                r.put(t.getName(), new LocalizedMetaData(title, description, className));
            }
        }

        return r;
    }

    return null;
}

From source file:net.sf.jabref.logic.journals.JournalAbbreviationRepository.java

public Optional<String> getNextAbbreviation(String text) {
    Optional<Abbreviation> abbreviation = getAbbreviation(text);

    if (!abbreviation.isPresent()) {
        return Optional.empty();
    }//from  w ww .j  a  v a2 s  .  c  o m

    Abbreviation abbr = abbreviation.get();
    return Optional.of(abbr.getNext(text));
}

From source file:net.sf.jabref.logic.journals.JournalAbbreviationRepository.java

public Optional<String> getMedlineAbbreviation(String text) {
    Optional<Abbreviation> abbreviation = getAbbreviation(text);

    if (!abbreviation.isPresent()) {
        return Optional.empty();
    }/* w  w w.j  av  a 2s  .c  o m*/

    Abbreviation abbr = abbreviation.get();
    return Optional.of(abbr.getMedlineAbbreviation());
}

From source file:net.sf.jabref.logic.journals.JournalAbbreviationRepository.java

public Optional<String> getIsoAbbreviation(String text) {
    Optional<Abbreviation> abbreviation = getAbbreviation(text);

    if (!abbreviation.isPresent()) {
        return Optional.empty();
    }/* ww w.  j a va 2 s  .c  o  m*/

    Abbreviation abbr = abbreviation.get();
    return Optional.of(abbr.getIsoAbbreviation());
}

From source file:cats.twitter.webapp.controller.module.ApiController.java

@Transactional
@RequestMapping(value = "/api-chain", method = RequestMethod.GET)
public void apiChain(@RequestHeader("token") String token, HttpServletResponse response) {
    Optional<Request> req = reqRepository.findOneByToken(token);
    if (!req.isPresent()) {
        throw new IllegalAccessError("Please verify your token!");
    }/*from   w w w .  j  a v a2  s. c om*/
    String result = null;
    if (req.get().isChained() && req.get().getPreviousRequest() != null) {
        result = req.get().getPreviousRequest().getResult(Result.TypeRes.FILE).getResult();
        try {

            File downloadFile = new File(result);
            FileInputStream inputStream = new FileInputStream(downloadFile);

            // set content attributes for the response
            response.setContentType("text/plain");
            response.setContentLength((int) downloadFile.length());

            // get output stream of the response
            OutputStream outStream = response.getOutputStream();

            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = -1;

            // write bytes read from the input stream into the output stream
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, bytesRead);
            }

            inputStream.close();
            outStream.close();
        } catch (Exception e) {
            Result res = new Result();
            res.setDate(new Date());
            res.setResult(e.getMessage());
            res.setType(Result.TypeRes.ERROR);
            req.get().addResult(res);
            e.printStackTrace();
        }
    }
}

From source file:net.sf.jabref.logic.fulltext.ScienceDirect.java

@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
    Objects.requireNonNull(entry);
    Optional<URL> pdfLink = Optional.empty();

    // Try unique DOI first
    Optional<DOI> doi = entry.getFieldOptional(FieldName.DOI).flatMap(DOI::build);

    if (doi.isPresent()) {
        // Available in catalog?
        try {/*from   w w  w  .  j ava2 s  .c  om*/
            String sciLink = getUrlByDoi(doi.get().getDOI());

            if (!sciLink.isEmpty()) {
                // Retrieve PDF link
                Document html = Jsoup.connect(sciLink).ignoreHttpErrors(true).get();
                Element link = html.getElementById("pdfLink");

                if (link != null) {
                    LOGGER.info("Fulltext PDF found @ ScienceDirect.");
                    pdfLink = Optional.of(new URL(link.attr("pdfurl")));
                }
            }
        } catch (UnirestException e) {
            LOGGER.warn("ScienceDirect API request failed", e);
        }
    }
    return pdfLink;
}

From source file:org.ulyssis.ipp.snapshot.TagSeenEvent.java

protected Snapshot doApply(Snapshot snapshot) {
    if (snapshot.getStartTime().isBefore(getTime()) && snapshot.getEndTime().isAfter(getTime())) {
        Optional<Integer> teamNb = snapshot.getTeamTagMap().tagToTeam(tag);
        if (teamNb.isPresent()) {
            Optional<TeamState> teamState = snapshot.getTeamStates().getStateForTeam(teamNb.get());
            TeamState newTeamState;/*from w  w w.ja  v a 2  s .  c om*/
            if (teamState.isPresent()) {
                newTeamState = teamState.get().addTagSeenEvent(snapshot, this);
            } else {
                newTeamState = (new TeamState()).addTagSeenEvent(snapshot, this);
            }
            TeamStates newTeamStates = snapshot.getTeamStates().setStateForTeam(teamNb.get(), newTeamState);
            Snapshot.Builder builder = Snapshot.builder(getTime(), snapshot).withTeamStates(newTeamStates);
            if (snapshot.getStatus().isPublic()) {
                builder.withPublicTeamStates(newTeamStates);
            }
            return builder.build();
        } else {
            return snapshot;
        }
    } else {
        return snapshot;
    }
}

From source file:com.haulmont.cuba.web.gui.icons.IconResolverImpl.java

protected Resource getResource(String iconPath) {
    Optional<IconProvider> provider = iconProviders.stream().filter(p -> p.canProvide(iconPath)).findAny();

    if (provider.isPresent()) {
        return provider.get().getIconResource(iconPath);
    }/*from www .j  a va  2 s. com*/

    log.warn("There is no IconProvider for the given icon: {}", iconPath);
    return null;
}

From source file:fi.helsinki.opintoni.service.UserServiceTest.java

@Test
public void thatUserIsFoundByEduPersonPrincipalName() {
    Optional<User> user = userService.findFirstByEduPersonPrincipalName(EDU_PERSON_PRINCIPAL_NAME);
    assertThat(user.isPresent()).isTrue();
}