List of usage examples for java.util Optional isPresent
public boolean isPresent()
From source file:com.amazonaws.serverless.sample.spring.PetsController.java
@RequestMapping(path = "/pets", method = RequestMethod.GET) public Pet[] listPets(@RequestParam("limit") Optional<Integer> limit) { int queryLimit = 10; if (limit.isPresent()) { queryLimit = limit.get();/*www . jav a2 s .c om*/ } Pet[] outputPets = new Pet[queryLimit]; for (int i = 0; i < queryLimit; i++) { Pet newPet = new Pet(); newPet.setId(UUID.randomUUID().toString()); newPet.setName(PetData.getRandomName()); newPet.setBreed(PetData.getRandomBreed()); newPet.setDateOfBirth(PetData.getRandomDoB()); outputPets[i] = newPet; } return outputPets; }
From source file:com.devicehive.service.configuration.ConfigurationService.java
@Transactional public <T> void save(@NotNull String name, T value) { //TODO check keys are same String str = value != null ? value.toString() : null; Optional<ConfigurationVO> existingOpt = findByName(name); if (existingOpt.isPresent()) { ConfigurationVO existing = existingOpt.get(); existing.setValue(str);//from w w w . j a v a 2s. c om configurationDao.merge(existing); } else { ConfigurationVO configuration = new ConfigurationVO(); configuration.setName(name); configuration.setValue(str); configurationDao.persist(configuration); } }
From source file:se.omegapoint.facepalm.client.controllers.ImageController.java
@RequestMapping("/image") public String image(final @RequestParam String id, final Model model) { final Optional<ImagePost> image = imageAdapter.getImage(id); if (!image.isPresent()) { return "redirect:/404"; }//w w w. ja v a 2 s . c o m final List<ImageComment> comments = imageAdapter.getCommentsForImage(image.get().id); model.addAttribute("image", image.get()); model.addAttribute("comments", comments); return "image"; }
From source file:things.view.rest.ThingRestController.java
@Transactional(readOnly = true) @RequestMapping(value = "/{type}/{key}") @Timed/*from w w w . jav a 2 s . c o m*/ public Thing getUniqueThingForTypeAndKey(@PathVariable("type") String type, @PathVariable("key") String key) throws ThingException, NoSuchThingException { Optional<Thing> thing = thingControl.findUniqueThingMatchingTypeAndKey(type, key, true); if (!thing.isPresent()) { throw new NoSuchThingException(type, key); } return thing.get(); }
From source file:org.obiba.mica.micaConfig.rest.EntityConfigResource.java
@GET @Path("/form") public U get(@Context UriInfo uriInfo, @QueryParam("locale") String locale) { Optional<T> optionalConfig = getConfigService().findComplete(); if (!optionalConfig.isPresent()) throw NoSuchEntityException.withPath(EntityConfig.class, uriInfo.getPath()); T config = optionalConfig.get();/* w ww . j av a2s . c o m*/ entityConfigTranslator.translateSchemaForm(locale, config); return asDto(config); }
From source file:org.openmhealth.shim.fitbit.mapper.FitbitStepCountDataPointMapper.java
@Override protected Optional<DataPoint<StepCount>> asDataPoint(JsonNode node) { int stepCountValue = Integer.parseInt(asRequiredString(node, "value")); if (stepCountValue == 0) { return Optional.empty(); }/*from w w w . j a v a2 s . c om*/ StepCount.Builder builder = new StepCount.Builder(stepCountValue); Optional<LocalDate> stepDate = asOptionalLocalDate(node, "dateTime"); if (stepDate.isPresent()) { LocalDateTime startDateTime = stepDate.get().atTime(0, 0, 0, 0); builder.setEffectiveTimeFrame(TimeInterval.ofStartDateTimeAndDuration( combineDateTimeAndTimezone(startDateTime), new DurationUnitValue(DAY, 1))); } StepCount measure = builder.build(); Optional<Long> externalId = asOptionalLong(node, "logId"); return Optional.of(newDataPoint(measure, externalId.orElse(null))); }
From source file:bg.vitkinov.edu.services.JokeCategoryService.java
private List<KeyWord> getKewWors(String keywords) { String[] words = keywords.split(","); List<KeyWord> result = new ArrayList<>(words.length); for (String word : words) { Optional<KeyWord> keyword = keyWordRepository.findByName(word); if (keyword.isPresent()) { result.add(keyword.get());/*from www. ja v a 2 s . c o m*/ } else { result.add(keyWordRepository.save(new KeyWord(word))); } } return result; }
From source file:pitayaa.nail.msg.core.account.controller.AccountLicenseController.java
@RequestMapping(value = "/accountsLicense/{ID}", method = RequestMethod.POST) public @ResponseBody ResponseEntity<?> saveAccountLicenseModel(@RequestBody AccountLicense accLicenseBody, @PathVariable("ID") UUID uid) throws Exception { Optional<AccountLicense> accountLicense = accLicenseService.findOne(uid); if (!accountLicense.isPresent()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); }//from ww w .j a va2s . c o m accLicenseBody = accLicenseService.save(accLicenseBody); return ResponseEntity.ok(accLicenseBody); }
From source file:pitayaa.nail.msg.core.account.controller.AccountLicenseController.java
@RequestMapping(value = "/accountsLicense/{ID}", method = RequestMethod.PUT) public @ResponseBody ResponseEntity<?> updateAccountLicenseModel(@RequestBody AccountLicense accLicenseBody, @PathVariable("ID") UUID uid) throws Exception { Optional<AccountLicense> accountLicense = accLicenseService.findOne(uid); if (!accountLicense.isPresent()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); }//w ww. j a va 2 s. com accLicenseBody = accLicenseService.update(accLicenseBody); return ResponseEntity.ok(accLicenseBody); }
From source file:controller.LeerlingController.java
@RequestMapping(method = RequestMethod.GET) public String doGet(@RequestParam("klasNaam") String klasNaam, Model model) { final String klNaam = klasNaam; Optional<Klas> klas = klasManager.getItems().stream().filter(k -> k.getNaam().equals(klNaam)).findFirst(); if (klas.isPresent()) { Klas k = klas.get();/*from w ww . jav a 2 s. c o m*/ List<Leerling> leerlingen = k.getLeerlingen(); model.addAttribute("klasNaam", k.getNaam()); model.addAttribute("leerlingen", leerlingen); return "leerlingOverview"; } else { return "redirect:/klas"; } }