Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Focusing

Abhishek Gupta abhirockzz

:octocat:
Focusing
View GitHub Profile
@abhirockzz
abhirockzz / RandomGist.java
Last active Mar 4, 2018
Simple utility method to check for 'emptiness' of objects.The 'emptiness' criteria is defined by a java.util.function.Predicate.On the lines of Objects.requireNonNull
View RandomGist.java
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
public class RandomGist {
public static <T> T requireNonEmpty(T object, Predicate<T> predicate, String msgToCaller){
Objects.requireNonNull(object);
Objects.requireNonNull(predicate);
@abhirockzz
abhirockzz / MySingletonBean.java
Created Sep 13, 2014
Demonstration of Container managed concurrency in Singleton Beans (default, locking type not explicitly specified )
View MySingletonBean.java
package com.abhirockzz.wordpress.ejb.lifecycle.singleton;
import com.abhirockzz.wordpress.ejb.lifecycle.stateful.MyStatefulBean;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Singleton
@abhirockzz
abhirockzz / MySingletonBean.java
Created Sep 13, 2014
Sticking with Container managed concurrency. Changing the explicit lock type from WRITE to READ
View MySingletonBean.java
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@abhirockzz
abhirockzz / SimpleOptional.java
Last active Aug 29, 2015
Generating a simple Optional containing the original value
View SimpleOptional.java
public static Optional<String> stringOptional(String input) {
return Optional.of(input);
}
@abhirockzz
abhirockzz / NullableOptional.java
Last active Aug 29, 2015
An Optional wrapping a value which can be null
View NullableOptional.java
public static Optional<String> stringNullableOptional(String input) {
if (!new Random().nextBoolean()) {
input = null;
}
return Optional.ofNullable(input);
}
@abhirockzz
abhirockzz / EmptyOptional.java
Last active Aug 29, 2015
Returning an Optional with no value (no value does not equal null)
View EmptyOptional.java
public static Optional<String> emptyOptional() {
return Optional.empty();
}
View SimpleOptionalConsumer.java
public static void consumingOptional() {
Optional<String> wrapped = Optional.of("aString");
if (wrapped.isPresent()) {
System.out.println("Got string - " + wrapped.get());
}
else {
System.out.println("Gotcha !");
}
}
View NullableOptionalConsumer.java
public static void consumingNullableOptional() {
String input = null;
if (new Random().nextBoolean()) {
input = "iCanBeNull";
}
Optional<String> wrapped = Optional.ofNullable(input);
System.out.println(wrapped.orElse("default"));
}
View NullableOptionalWithSupplier.java
public static void consumingEmptyOptional() {
String input = null;
if (new Random().nextBoolean()) {
input = "iCanBeNull";
}
Optional<String> wrapped = Optional.ofNullable(input);
System.out.println(wrapped.orElseGet(
() -> {
return "defaultBySupplier";
}
View UsingObjects.java
package com.abhirockzz.wordpress.npesaviors;
import java.util.Map;
import java.util.Objects;
public class UsingObjects {
String getVal(Map<String, String> aMap, String key) {
return aMap.containsKey(key) ? aMap.get(key) : null;
}
You can’t perform that action at this time.