Skip to content

Instantly share code, notes, and snippets.

View zivce's full-sized avatar
🕹️
Focusing

Милош Живковић zivce

🕹️
Focusing
View GitHub Profile
public StoreConfiguration updateConfig(Store store, String value) {
return store.getConfigurationId().map(configurationId ->{
return storeConfigurationRepository.findById(configurationId).map(storeConfiguration -> {
return updateStoreConfiguration(storeConfiguration, value);
}).orElseThrow();
}).orElseThrow();
}
public Optional<StoreConfiguration> update(Store store, String value) {
return store.getConfigurationId() // Optional<UUID>
.flatMap(storeConfigurationRepository::findById) // Optional<StoreConfiguration>
.map(config -> this.updateStoreConfiguration(store,value)) // Optional<StoreConfiguration>
}
opt.<Runnable>map(value -> () -> System.out.println("Found " + value))
.orElse(() -> System.out.println("Not present!"))
.run();
opt.ifPresentOrElse(System.out::println,
() -> System.out.println("not present"));
Optionals.ifPresentOrElse(opt, System.out::println,
() -> System.out.println("not present"));
@zivce
zivce / ifNotPresent_WrongApproach.java
Last active February 17, 2022 14:16
How to not create ifNotPresent in Java
Optional.of(opt).map(value -> { System.out.println(value);
return value; })
.orElseGet(() -> { System.out.println("not present");
return -1;});
INSERT_UPDATE MinConstraint;id[unique=true];severity(code,itemtype(code));active;annotation;descriptor(enclosingType(code),qualifier);message[lang=de];message[lang=en];value
;AlbumSalesMustNotBeNegative;ERROR:Severity;true;javax.validation.constraints.Min;Band:albumSales;Albumverkäufe dürfen nicht negativ sein;Album sales must not be negative;1
INSERT_UPDATE NotLoremIpsumConstraint;id[unique=true];severity(code,itemtype(code));active;annotation;descriptor(enclosingType(code),qualifier);message[lang=de];message[lang=en]
;BandHistoryNotIpsum;ERROR:Severity;true;concerttours.constraints.NotLoremIpsum;Band:history;Band Geschichte sollte nicht Lorem ipsum Platzhalter Text.;Band history should not be Lorem ipsum placeholder text.;
// two-level functions; wrapper of the recursive one
private void Horners(double[] coeff, double x, int n) {
this.x = x;
this.coeff = coeff;
H(n);
}
private double H(int k)
{
if (k == 0) {
return coeff[0];
public static Node<Integer> InsertRecursively(Node<Integer>head,int position,int element)
{
if(head==null && position>0)//Note this is not the base case
//this is the case when linked list is empty and adding at position other than 0
{
return head;
}
//Base Case
if(position==0)
{
public static Node<Integer> InsertIterative(Node<Integer>head,int pos,int element){
if(pos==0)
{
Node<Integer> newNode=new Node<Integer>(element);
newNode.next=head;
head=newNode;
return head;
}
else
{