Skip to content

Instantly share code, notes, and snippets.

@priyankshah217
Created July 22, 2021 13:09
Show Gist options
  • Save priyankshah217/1aea9618dd7d47dd79f65fc5e3e5d275 to your computer and use it in GitHub Desktop.
Save priyankshah217/1aea9618dd7d47dd79f65fc5e3e5d275 to your computer and use it in GitHub Desktop.
ErrorHandlingAndNullChecks Java8 Way
[
{
"EventID": "1",
"EventName": "login",
"CommonAttributes": {
"Location": "NY",
"Country": "US"
}
},
{
"EventID": "2",
"EventName": "click_on_ad",
"CommonAttributes": {
"Location": "NY",
"Country": "US"
},
"Properties": {
"ShowImpression": true,
"LoginRequired": true,
"UnitPrice": 4.56
}
},
{
"EventID": "3",
"EventName": "click_on_list",
"CommonAttributes": {
"Location": "NY",
"Country": "US"
},
"Properties": {
"LoginRequired": true,
"UnitPrice": 9.18
}
},
{
"EventID": "4",
"EventName": "click_on_ad",
"CommonAttributes": {
"Location": "NY",
"Country": "US"
},
"Properties": {
"ShowImpression": true,
"LoginRequired": false,
"UnitPrice": 5.78
}
},
{
"EventID": "5",
"EventName": "logout",
"CommonAttributes": {
"Location": "NY",
"Country": "US"
}
}
]
package programming.imperative;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class CommonAttributes {
@JsonProperty("Location")
private String location;
@JsonProperty("Country")
private String country;
public String getLocation() {
return location;
}
public String getCountry() {
return country;
}
}
class Properties {
@JsonProperty("ShowImpression")
private boolean showImpression;
@JsonProperty("LoginRequired")
private boolean loginRequired;
@JsonProperty("UnitPrice")
private double unitPrice;
public Boolean isShowImpression() {
return showImpression;
}
public Boolean isLoginRequired() {
return loginRequired;
}
public Double getUnitPrice() {
return unitPrice;
}
}
class Event {
@JsonProperty("EventID")
private String eventID;
@JsonProperty("EventName")
private String eventName;
@JsonProperty("CommonAttributes")
private CommonAttributes commonAttributes;
@JsonProperty("Properties")
private Properties properties;
public String getEventID() {
return eventID;
}
public String getEventName() {
return eventName;
}
public CommonAttributes getCommonAttributes() {
return commonAttributes;
}
public Properties getProperties() {
return properties;
}
}
public class WithExplicitNullCheck {
public static void main(String[] args) {
String fileName = "src/main/resources/Sample.json";
try {
final Event[] events = new ObjectMapper().readValue(new File(fileName), Event[].class);
List<Double> doublePrices = new ArrayList<>();
for (Event event : events) {
if (event.getProperties() != null) {
doublePrices.add(event.getProperties().getUnitPrice());
}
}
System.out.println(doublePrices);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package programming.functional;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class CommonAttributes {
@JsonProperty("Location")
private String location;
@JsonProperty("Country")
private String country;
public String getLocation() {
return location;
}
public String getCountry() {
return country;
}
}
class Properties {
@JsonProperty("ShowImpression")
private boolean showImpression;
@JsonProperty("LoginRequired")
private boolean loginRequired;
@JsonProperty("UnitPrice")
private double unitPrice;
public Boolean isShowImpression() {
return showImpression;
}
public Boolean isLoginRequired() {
return loginRequired;
}
public Double getUnitPrice() {
return unitPrice;
}
}
class Event {
@JsonProperty("EventID")
private String eventID;
@JsonProperty("EventName")
private String eventName;
@JsonProperty("CommonAttributes")
private CommonAttributes commonAttributes;
@JsonProperty("Properties")
private Properties properties;
public String getEventID() {
return eventID;
}
public String getEventName() {
return eventName;
}
public CommonAttributes getCommonAttributes() {
return commonAttributes;
}
public Optional<Properties> getProperties() {
return Optional.ofNullable(properties);
}
}
public class WithoutExplicitNullCheck {
public static void main(String[] args) {
String fileName = "src/main/resources/Sample.json";
try {
final Event[] events = new ObjectMapper().readValue(new File(fileName), Event[].class);
final List<Double> collect = Arrays.stream(events)
.flatMap(event -> event.getProperties().stream())
.map(Properties::getUnitPrice)
.collect(Collectors.toList());
System.out.println(collect);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment