Example usage for java.util Optional get

List of usage examples for java.util Optional get

Introduction

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

Prototype

public T get() 

Source Link

Document

If a value is present, returns the value, otherwise throws NoSuchElementException .

Usage

From source file:org.onosproject.segmentrouting.config.SegmentRoutingAppConfigTest.java

/**
 * Tests vRouterId getter./*from   ww w.  jav  a2  s  .  co  m*/
 *
 * @throws Exception
 */
@Test
public void testVRouterId() throws Exception {
    Optional<DeviceId> vRouterId = config.vRouterId();
    assertTrue(vRouterId.isPresent());
    assertThat(vRouterId.get(), is(VROUTER_ID_1));
}

From source file:ch.ralscha.extdirectspring.provider.RemoteProviderMetadata.java

@ExtDirectMethod(value = ExtDirectMethodType.STORE_READ, group = "metadata")
public List<Row> method4(@MetadataParam(value = "id") Optional<Integer> id) {
    if (!id.isPresent()) {
        assertThat(id.orElse(null)).isNull();
    } else {/*from   www.  j a  va2 s .  c o  m*/
        assertThat(id.get()).isEqualTo(Integer.valueOf(13));
    }
    return RemoteProviderStoreRead.createRows(":" + id);
}

From source file:me.yanaga.winter.data.jpa.PersonRepositoryTest.java

@Test
public void testFindOnePredicateAndConsumer() {
    Person first = new Person();
    first.setName("Yanaga");
    personRepository.save(first);/*w  ww  . j  a v a  2  s.  c  om*/
    Optional<Person> found = personRepository.findOne(Persons.withNameContaining("a"), q -> q.limit(1));
    assertThat(found.get()).isEqualTo(first);
}

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 .  j  a v  a  2s . 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:bg.vitkinov.edu.services.JokeService.java

@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT })
public ResponseEntity<?> insert(@RequestParam String title, @RequestParam String content,
        @RequestParam(required = false, defaultValue = "false") boolean base64) {
    String jokeContent = base64 ? new String(Base64.getDecoder().decode(content)) : content;
    Optional<Joke> joke = jokeRepository.findFirstByContentIgnoreCaseContaining(jokeContent);
    if (joke.isPresent()) {
        return new ResponseEntity<>(joke.get(), HttpStatus.CONFLICT);
    }/*from   ww w .j a  va 2  s.  c o  m*/
    Joke newJoke = new Joke();
    newJoke.setTitle(title);
    newJoke.setContent(jokeContent);
    newJoke.setCategory(findCategories(jokeContent));
    return new ResponseEntity<>(jokeRepository.save(newJoke), HttpStatus.CREATED);
}

From source file:com.javaeeeee.controllers.BookmarksController.java

/**
 * A method to add a bookmark./*from   w  w  w.j a  v a  2  s  . co m*/
 */
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<Bookmark> addBookmark(@PathVariable(value = "username") String username,
        @RequestBody Bookmark bookmark) throws UserNotFoundException {
    Optional<User> optional = usersRepository.findByUsername(username);
    if (optional.isPresent()) {
        User user = optional.get();
        user.addBookmark(bookmark);
        bookmark.setUser(user);
        bookmarksRepository.save(bookmark);
        return new ResponseEntity<>(bookmark, HttpStatus.CREATED);
    } else {
        throw new UserNotFoundException(username);
    }
}

From source file:org.onosproject.segmentrouting.config.SegmentRoutingAppConfigTest.java

/**
 * Tests vRouterId setter./*from  w w  w  . j a  v a 2  s . co  m*/
 *
 * @throws Exception
 */
@Test
public void testSetVRouterId() throws Exception {
    config.setVRouterId(VROUTER_ID_2);

    Optional<DeviceId> vRouterId = config.vRouterId();
    assertTrue(vRouterId.isPresent());
    assertThat(vRouterId.get(), is(VROUTER_ID_2));
}

From source file:ddf.catalog.validation.impl.validator.MatchAnyValidator.java

private List<AttributeValidationReport> validateAttributeSerializable(String attributeName,
        Serializable serializable) {
    Attribute newAttribute = new AttributeImpl(attributeName, serializable);
    List<AttributeValidationReport> validationReportList = new ArrayList<>();

    for (AttributeValidator attributeValidator : validators) {
        Optional<AttributeValidationReport> attributeValidationReport = attributeValidator
                .validate(newAttribute);
        if (attributeValidationReport.isPresent()) {
            validationReportList.add(attributeValidationReport.get());
        }/*  www  .j ava2  s.c  o  m*/
    }

    return validationReportList;
}

From source file:io.fabric8.vertx.maven.plugin.mojos.InitializeMojo.java

private void unpackWebjars(Set<Artifact> dependencies) throws MojoExecutionException {
    for (Artifact artifact : dependencies) {
        Optional<File> maybeFile = getArtifactFile(artifact);
        if (artifact.getType().equalsIgnoreCase("jar") && maybeFile.isPresent()) {
            if (WebJars.isWebJar(getLog(), maybeFile.get())) {
                try {
                    WebJars.extract(this, maybeFile.get(), createWebRootDirIfNeeded(), stripWebJarVersion);
                } catch (IOException e) {
                    throw new MojoExecutionException("Unable to unpack '" + artifact.toString() + "'", e);
                }//from   w  ww  .  j  ava 2  s.co  m
            }
        }
    }
}

From source file:io.mapzone.arena.csw.catalog.ProjectNodeSynchronizer.java

@EventHandler(delay = 1000, scope = Event.Scope.JVM)
protected void projectNodeCommitted(List<ProjectNodeCommittedEvent> evs) throws Exception {
    // the delay causes this to be executed inside an UIJob, so
    // this update runs asynchronously and does no effect main thread
    try (Updater updater = catalog.prepareUpdate(); UnitOfWork uow = ProjectRepository.newUnitOfWork();) {
        for (ProjectNodeCommittedEvent ev : evs) {
            ProjectNode entity = ev.getEntity(uow);

            if (entity instanceof ILayer) {
                Optional<MetadataReference> mdRef = findMetadataRefs(entity);
                if (mdRef.isPresent()) {
                    String identifier = mdRef.get().metadataId.get();
                    updater.updateEntry(identifier, update(entity));
                } else {
                    String identifier = UUID.randomUUID().toString();
                    createMetadataRefs(entity, identifier);
                    updater.newEntry(md -> {
                        md.setIdentifier(identifier);
                        update(entity).accept(md);
                    });/* w  w w .j a va 2  s.  c o  m*/
                }
            }
        }
        updater.commit();
        uow.commit();
    } catch (HttpHostConnectException e) {
        // just a short message for better developing
        log.warn("Unable to connect CSW server: " + e.getLocalizedMessage());
    }
}