Skip to content

Instantly share code, notes, and snippets.

View liviutudor's full-sized avatar

Liviu Tudor liviutudor

View GitHub Profile
@liviutudor
liviutudor / ReadJarManifest.java
Created November 21, 2019 07:41
How to read own manifest file and get version number
URLClassLoader cl = (URLClassLoader) App.class.getClassLoader();
URL url = cl.findResource("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(url.openStream());
Attributes attr = manifest.getMainAttributes());
System.out.println(manifest.getMainAttributes().getValue("Implementation-Title"));
@liviutudor
liviutudor / DelegationBean.groovy
Created October 31, 2017 05:58
after handcrafting the comparison in the beans we are delegating to
package delegation
import groovy.transform.Sortable
class DelegationBean {
@Delegate
BeanOne person
@Delegate
BeanTwo dateOfBirth
@liviutudor
liviutudor / BeanTwo.groovy
Created October 31, 2017 05:57
Bean two with handcrafted comparison
package delegation
import groovy.transform.TupleConstructor
@TupleConstructor
class BeanTwo implements Comparable<BeanTwo> {
int yob
public int getAge() {
new Date().year - yob
@liviutudor
liviutudor / BeanOne.groovy
Created October 31, 2017 05:56
Groovy bean one with handcrafted comparison
package delegation
import groovy.transform.TupleConstructor
@TupleConstructor
class BeanOne implements Comparable<BeanOne> {
String firstName
String familyName
public String getFullName() {
@liviutudor
liviutudor / DelegationBean.groovy
Last active September 22, 2017 05:48
Aggregation 2 classes with @DeleGate
class DelegationBean {
@Delegate
BeanOne person
@Delegate
BeanTwo dateOfBirth
static void main(String... args) {
DelegationBean one = new DelegationBean(person: new BeanOne("Liviu", "Tudor"), dateOfBirth: new BeanTwo(yob: 1975))
DelegationBean two = new DelegationBean(person: new BeanOne("William", "Shakespeare"), dateOfBirth: new BeanTwo(yob: 1564))
@liviutudor
liviutudor / BeanTwo.groovy
Created September 21, 2017 05:46
Using @DeleGate in groovy, second bean
import groovy.transform.Sortable
import groovy.transform.TupleConstructor
@Sortable
@TupleConstructor
class BeanTwo {
int yob
public int getAge() {
new Date().year - yob
@liviutudor
liviutudor / BeanOne.groovy
Created September 21, 2017 05:45
Using @DeleGate in groovy, first bean
import groovy.transform.Sortable
import groovy.transform.TupleConstructor
@Sortable
@TupleConstructor
class BeanOne {
String firstName
String familyName
public String getFullName() {
@liviutudor
liviutudor / JacksonBean.java
Created September 16, 2017 00:47
Usage of ConstructorProperties annotation when removing setters and zero-args constructor
import java.beans.ConstructorProperties;
public class JacksonBean {
private final int value;
private final String another;
@ConstructorProperties({"value", "another"})
public JacksonBean(int value, String another) {
this.value = value;
this.another = another;
@liviutudor
liviutudor / JacksonBean.java
Created September 16, 2017 00:29
Usage of Jackson annotations when removing setters and zero-args constructor
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class JacksonBean {
private final int value;
private final String another;
@JsonCreator
public JacksonBean(@JsonProperty("value") int value, @JsonProperty("another") String another) {
this.value = value;
@liviutudor
liviutudor / Sample.java
Created June 28, 2017 07:19
Writing the same code using RxJava Observable
@GET
public Observable<Response> processRequest( @QueryParam("id")String id, @QueryParam("userId")String userId ) {
return Observable.zip(
Observable.fromCallable(() -> database.retrieveRecord(id)),
Observable.fromCallable(() -> userStore.retrieve(userId)),
Observable.fromCallable(() -> logger.writeEntryFor(id, userId)),
this::generateAdResponse
);
}