Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Last active September 3, 2022 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j5ik2o/152828477f0825d63d9dfce728c2bc4d to your computer and use it in GitHub Desktop.
Save j5ik2o/152828477f0825d63d9dfce728c2bc4d to your computer and use it in GitHub Desktop.
/*
* Copyright 2022 Junichi Kato
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.j5ik2o.adceet.api.write.adaptor.aggregate.state;
import com.github.j5ik2o.adceet.api.write.domain.events.ThreadCreated;
import com.github.j5ik2o.adceet.api.write.domain.events.ThreadEvent;
public sealed interface ThreadState permits Empty, Just {
default ThreadState applyEvent(ThreadEvent threadEvent) {
return switch (this) {
case Empty empty -> switch (threadEvent) {
case ThreadCreated threadCreated -> empty.create(threadCreated);
default -> throw new IllegalStateException("Unexpected value: " + threadEvent);
};
case Just just -> just.update(threadEvent);
};
}
}
/*
* Copyright 2022 Junichi Kato
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.j5ik2o.adceet.api.write.adaptor.aggregate.state;
import com.github.j5ik2o.adceet.api.write.domain.events.ThreadCreated;
import com.github.j5ik2o.adceet.api.write.domain.events.ThreadEvent;
public sealed interface ThreadState permits Empty, Just {
default ThreadState applyEvent(ThreadEvent threadEvent) {
return switch (this) {
case Empty empty -> {
if (threadEvent instanceof ThreadCreated threadCreated) {
yield empty.create(threadCreated);
} else {
throw new IllegalStateException("Unexpected value: " + threadEvent);
}
}
case Just just -> just.update(threadEvent);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment