Last active
June 26, 2016 02:08
-
-
Save hastebrot/9ae0584a0dce8762747a16d04f483450 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 es4j.example | |
| //compile "com.eventsourcing:eventsourcing-core:0.4.0-SNAPSHOT" | |
| //compile "com.eventsourcing:eventsourcing-inmem:0.4.0-SNAPSHOT" | |
| //compile "com.eventsourcing:eventsourcing-h2:0.4.0-SNAPSHOT" | |
| import com.eventsourcing.Event | |
| import com.eventsourcing.Model | |
| import com.eventsourcing.Repository | |
| import com.eventsourcing.StandardCommand | |
| import com.eventsourcing.StandardEvent | |
| import com.eventsourcing.annotations.Index | |
| import com.eventsourcing.index.IndexEngine.IndexFeature.EQ | |
| import com.eventsourcing.index.IndexEngine.IndexFeature.UNIQUE | |
| import com.eventsourcing.index.MemoryIndexEngine | |
| import com.eventsourcing.index.SimpleAttribute | |
| import com.eventsourcing.inmem.MemoryJournal | |
| import com.googlecode.cqengine.query.QueryFactory.equal | |
| import com.googlecode.cqengine.query.option.QueryOptions | |
| import java.util.Optional | |
| import java.util.UUID | |
| import java.util.stream.Stream | |
| //------------------------------------------------------------------------------------------------- | |
| // MAIN METHOD. | |
| //------------------------------------------------------------------------------------------------- | |
| fun main(args: Array<String>) { | |
| val repository = Repository.create() | |
| val journal = MemoryJournal() | |
| val index = MemoryIndexEngine() | |
| repository.journal = journal | |
| repository.indexEngine = index | |
| repository.startAsync().awaitRunning() | |
| val createUser = CreateUser() | |
| val user = repository.publish(createUser).get() | |
| println(user) | |
| } | |
| //------------------------------------------------------------------------------------------------- | |
| // MODELS. | |
| //------------------------------------------------------------------------------------------------- | |
| data class User(@JvmField val repository: Repository, | |
| @JvmField val id: UUID) : Model { | |
| override fun getRepository() = repository // issue: constructor value. | |
| override fun id() = id // issue: constructor value. | |
| companion object { | |
| fun lookup(repository: Repository, | |
| id: UUID): Optional<User> { // issue: optional monad. | |
| repository.query(UserCreated::class.java, equal(UserCreated.ID, id)).use { | |
| return Optional.of(User(repository, it.uniqueResult().uuid())) | |
| } | |
| } | |
| } | |
| } | |
| //------------------------------------------------------------------------------------------------- | |
| // COMMANDS. | |
| //------------------------------------------------------------------------------------------------- | |
| class CreateUser() : StandardCommand<User>() { | |
| val id: UUID by lazy { UUID.randomUUID() } | |
| lateinit private var repository: Repository | |
| override fun events(repository: Repository): Stream<Event> { | |
| this.repository = repository | |
| return Stream.of(UserCreated().apply { uuid(id) }) | |
| } | |
| override fun onCompletion(): User { | |
| println("command: " + id) | |
| return User.lookup(repository, id).get() | |
| } | |
| } | |
| //------------------------------------------------------------------------------------------------- | |
| // EVENTS. | |
| //------------------------------------------------------------------------------------------------- | |
| class UserCreated : StandardEvent() { | |
| companion object { | |
| @Index(EQ, UNIQUE) | |
| val ID = object : SimpleAttribute<UserCreated, UUID>("id") { // issue: type inference. | |
| override fun getValue(userCreated: UserCreated, | |
| queryOptions: QueryOptions): UUID { | |
| println("event: " + userCreated.uuid()) | |
| return userCreated.uuid() | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment