Skip to content

Instantly share code, notes, and snippets.

@laser
Last active October 27, 2016 03:53
Show Gist options
  • Save laser/f56e28c22ed2ce92c5a860d611163010 to your computer and use it in GitHub Desktop.
Save laser/f56e28c22ed2ce92c5a860d611163010 to your computer and use it in GitHub Desktop.
Java Type System-Sadness
package com.erinswensonhealey.experiments;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
public class Experiment {
public interface HasCreatedAtTimestamp<T extends HasCreatedAtTimestamp<T>> {
Instant getCreatedAtTimestamp();
T setCreatedAtTimestamp(Instant ts);
}
public class Address implements HasCreatedAtTimestamp<Address> {
public Address(String city, String state, String code, String country, Instant createdTimestamp) {
this.city = city;
this.state = state;
this.code = code;
this.country = country;
this.createdTimestamp = createdTimestamp;
}
private String city;
private String state;
private String code;
private String country;
private Instant createdTimestamp;
public Instant getCreatedAtTimestamp() {
return this.createdTimestamp;
}
public Address setCreatedAtTimestamp(Instant ts) {
return new Address(this.city, this.state, this.code, this.country, ts);
}
}
public class Person implements HasCreatedAtTimestamp<Person> {
public Person(String name, Instant createdTimestamp) {
this.name = name;
this.createdTimestamp = createdTimestamp;
}
private String name;
private Instant createdTimestamp;
public Instant getCreatedAtTimestamp() {
return this.createdTimestamp;
}
public Person setCreatedAtTimestamp(Instant ts) {
return new Person(this.name, this.createdTimestamp);
}
}
public static class Foo {
public static <T extends HasCreatedAtTimestamp<T>> List<T> overrideCreatedAtTimestamps(List<T> xs, Instant ts) {
return xs.stream().map(x -> x.setCreatedAtTimestamp(ts)).collect(Collectors.toList());
}
public void savePeople(List<Person> ps) {
List<Person> updated = overrideCreatedAtTimestamps(ps, Instant.now());
System.out.println(updated.get(0).name);
}
public void saveAddress(List<Address> as) {
List<Address> updated = overrideCreatedAtTimestamps(as, Instant.now());
System.out.println(updated.get(0).city);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment