Created
August 18, 2021 09:27
-
-
Save michael-simons/db0d355446e3487a868602360282762f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.molecues; | |
import java.util.Collections; | |
import java.util.HashSet; | |
import java.util.Objects; | |
import java.util.Set; | |
import java.util.concurrent.atomic.AtomicLong; | |
import java.util.function.Supplier; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.data.annotation.PersistenceConstructor; | |
import org.springframework.data.annotation.Version; | |
import org.springframework.data.neo4j.core.Neo4jTemplate; | |
import org.springframework.data.neo4j.core.schema.Id; | |
import org.springframework.data.neo4j.core.schema.Node; | |
import org.springframework.data.neo4j.core.schema.Relationship; | |
import org.springframework.stereotype.Component; | |
@Node | |
class Molecule { | |
private static final Supplier<Long> sequenceGenerator = new Supplier<>() { | |
private final AtomicLong source = new AtomicLong(0L); | |
@Override | |
public Long get() { | |
return source.incrementAndGet(); | |
} | |
}; | |
@Id | |
private final Long id; | |
@Version | |
private Long version; | |
private String name; | |
@Relationship(direction = Relationship.Direction.OUTGOING, type = "—") | |
private Set<Molecule> relatedObjects = new HashSet<>(); | |
Molecule(String name) { | |
this(sequenceGenerator.get(), name); | |
} | |
@PersistenceConstructor Molecule(Long id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
public Long getId() { | |
return id; | |
} | |
public Long getVersion() { | |
return version; | |
} | |
public String getName() { | |
return name; | |
} | |
public Set<Molecule> getRelatedObjects() { | |
return Collections.unmodifiableSet(relatedObjects); | |
} | |
private void setRelatedObjects(Set<Molecule> relatedObjects) { | |
this.relatedObjects = relatedObjects; | |
} | |
public void relate(Molecule object) { | |
this.relatedObjects.add(object); | |
object.relatedObjects.add(this); | |
} | |
public void relateUni(Molecule object) { | |
this.relatedObjects.add(object); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
Molecule that = (Molecule) o; | |
return id.equals(that.id); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(id); | |
} | |
} | |
@Node | |
class C extends Molecule { | |
C() { | |
super(""); | |
} | |
@PersistenceConstructor | |
private C(Long id, String name) { | |
super(id, name); | |
} | |
} | |
@Component | |
class Brew implements CommandLineRunner { | |
private final Neo4jTemplate neo4jTemplate; | |
public Brew(Neo4jTemplate neo4jTemplate) { | |
this.neo4jTemplate = neo4jTemplate; | |
} | |
@Override | |
public void run(String... args) throws Exception { | |
neo4jTemplate.deleteAll(Molecule.class); | |
var o = new Molecule("O"); | |
var c1 = new C(); | |
var c2 = new C(); | |
var c3 = new C(); | |
var n = new Molecule("N"); | |
var ch3 = new Molecule("CH3"); | |
var c4 = new C(); | |
var o2 = new Molecule("O"); | |
var n2 = new Molecule("N"); | |
var h3c = new Molecule("H3C"); | |
var n3 = new Molecule("N"); | |
var n4 = new Molecule("N"); | |
var c5 = new C(); | |
var ch3_2 = new Molecule("CH3"); | |
o.relate(c1); | |
c1.relateUni(c2); | |
c2.relate(c3); | |
c2.relateUni(n3); | |
n3.relateUni(c5); | |
n3.relateUni(ch3_2); | |
c3.relateUni(n); | |
c3.relateUni(n4); | |
n4.relate(c5); | |
n.relateUni(ch3); | |
n.relateUni(c4); | |
c4.relate(o2); | |
c4.relateUni(n2); | |
n2.relateUni(h3c); | |
n2.relateUni(c1); | |
neo4jTemplate.save(o); | |
o = neo4jTemplate.findById(o.getId(), Molecule.class).get(); | |
} | |
} | |
// Latest Spring Data Neo4j 6.2-SNAPSHOT is needed to run this successfully… | |
@SpringBootApplication | |
public class CoffeeApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(CoffeeApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment