Skip to content

Instantly share code, notes, and snippets.

@maxov
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save maxov/7228ddb9730c3e7e9ec1 to your computer and use it in GitHub Desktop.

Select an option

Save maxov/7228ddb9730c3e7e9ec1 to your computer and use it in GitHub Desktop.
message computation
// All lists are immutable
public ComputedMessage computeAll(@Nullable Message parent) {
Message self = null;
if (parent == null) {
self = this;
} else {
self = Messages.compute(parent, self);
}
List<ComputedMessage> newChildren = new ArrayList<Message>();
for(Message message: getChildren()) {
newChildren.add(message.computeAll(self));
}
return new ComputedMessage(self, newChildren);
}
// compute Message{color = blue, children = [Message{}]} = ComputedMessage{color = blue, children = [ComputedMessage{color_computed=blue}]
// Done so we can cache the original Message
class ComputedMessage implements Message {
private Message handle;
private List<Message> children;
public ComputedMessage(Message handle, List<Message> children) {
this.handle = handle;
}
// also likely will have constructor if there is a parent message
// this and other proxies
public TextColor getColor() {
return this.handle.getColor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment