Skip to content

Instantly share code, notes, and snippets.

View MazizEsa's full-sized avatar

Maziz MazizEsa

View GitHub Profile
@MazizEsa
MazizEsa / incorrectcoupling.java
Last active November 8, 2020 22:40
Incorrect Coupling
public class IncorrectCoupling {
public static class Points{
private String productId;
private Long totalPoints;
private PointsMappingExternalSystem pointsMappingExternalSystem;
private PointsMultiplierExternalSystem pointsMultiplierExternalSystem;
public Double calculateTotalPoints(){
return pointsMappingExternalSystem.getPointsMappingFromExternalSystem().stream().map(PointsMapping::getPoints).reduce(0d, Double::sum)
public void exampleMinimizingIfElse() {
final List<String> someExampleArray = new ArrayList<>();
Optional.ofNullable(someExampleArray).orElse(Collections.emptyList()).forEach(this::doSomethingWithIt);
ObjectUtils.defaultIfNull(someExampleArray, Collections.<String>emptyList()).forEach(this::doSomethingWithIt);
returnEmptyArrayIfNecessary(someExampleArray).forEach(this::doSomethingWithIt);
}
public void buildThumbnailWithUrl() {
final List<Thumbnail> rawThumbnailList = getThumbnailListFromSomeWhere();
final String appropriateCdn = getTheRightCdnBasedOnRegion();
final String defaultCdn = getDefaultCdn();
final List<Thumbnail> thumbnailListWithCdn = rawThumbnailList.stream().map(eachThumbnail -> {
final String cdnChose = eachThumbnail.isHasLarge() ? defaultCdn : appropriateCdn;
eachThumbnail.setThumbnailUri(cdnChose + eachThumbnail.getThumbnailUri());
return eachThumbnail;
}).collect(Collectors.toList());
@MazizEsa
MazizEsa / methodAbstraction.java
Last active October 24, 2020 23:16
Method Abstraction Level
public List<SomeDataFull> methodWithNoAbstraction() {
// get data from DB for instance
final List<SomeData> listOfDataFromDb = getDataFromDb(); // a.1
final List<String> someDataRetrievedIds = listOfDataFromDb.stream().map(SomeData::getId).collect(Collectors.toList()); //a.2
final Map<String, Boolean> someDataIdMapping = getVerificationFromExternalSystem(someDataRetrievedIds)
.stream().collect(Collectors.toMap(SomeDataVerification::getId, SomeDataVerification::isOk));
final List<SomeData> verifiedSomeData = listOfDataFromDb.stream().filter(eachData -> someDataIdMapping.getOrDefault(eachData.getId(), false)) //a.3
.collect(Collectors.toList());
@MazizEsa
MazizEsa / application.yaml
Last active September 14, 2020 14:23
Producer application.yaml
spring:
kafka:
bootstrap-servers: localhost:9092
properties:
schema.registry.url: http://localhost:8081
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: io.confluent.kafka.serializers.KafkaAvroSerializer
properties:
value.subject.name.strategy: io.confluent.kafka.serializers.subject.RecordNameStrategy # so that the name of the subject will be derived from record name ie, org.maziz.something
{
"namespace": "org.maz.schema",
"type": "record",
"name": "SplitSampleData",
"fields": [
{
"name": "SplitSampleData",
"type": {
"avro.java.string": "String",
"type": "string"
{
"namespace": "org.maz.schema",
"name": "SplitEnumerationSample",
"type": "enum",
"symbols": [
"SAMPLE1",
"SAMPLE2",
"SAMPLE3"
],
"default": "SAMPLE1"
@MazizEsa
MazizEsa / org.maz.schema.FlatSampleData.avsc
Last active September 14, 2020 13:36
Flat Schema Source
{
"namespace": "org.maz.schema",
"type": "record",
"name": "FlatSampleData",
"fields": [
{
"name": "FlatSampleData",
"type": {
"avro.java.string": "String",
"type": "string"
@MazizEsa
MazizEsa / consumerpluginpom.xml
Last active September 15, 2020 12:26
Confluent Schema Maven Plugin Consumer with Schema Reference Example
<plugin>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-maven-plugin</artifactId>
<version>${confluent.platform.version}</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>download</goal>
</goals>
@MazizEsa
MazizEsa / producerpluginpom.xml
Last active September 15, 2020 12:24
Confluent Schema Maven Plugin Producer with Schema Reference Example
<plugin>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-maven-plugin</artifactId>
<version>${confluent.platform.version}</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>register</goal>
</goals>