Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache some assignments... cache everything! #94

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/src/io/github/alechenninger/monarch/Assignments.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -34,6 +35,7 @@
public class Assignments implements Iterable<Assignment> {
private final Set<Assignment> explicit = new LinkedHashSet<>();
private final Set<Assignment> implicit = new LinkedHashSet<>();
private final Map<String, Assignment> byVariable = new HashMap<>();
private final Inventory inventory;

private final int hash;
Expand Down Expand Up @@ -119,12 +121,11 @@ public Assignments forkAt(String variable) {
}

public boolean isAssigned(String variable) {
return stream().anyMatch(a -> a.variable().name().equals(variable));
return byVariable.containsKey(variable);
}

public Assignment forVariable(String variable) {
return stream().filter(a -> a.variable().name().equals(variable))
.findAny()
return Optional.ofNullable(byVariable.get(variable))
.orElseThrow(NoSuchElementException::new);
}

Expand Down Expand Up @@ -255,7 +256,7 @@ public Map<String, String> toMap() {
}

public Stream<Assignment> stream() {
return Stream.concat(explicit.stream(), implicit.stream());
return byVariable.values().stream();
}

@Override
Expand Down Expand Up @@ -332,6 +333,7 @@ private void add(Assignment assignment) {
}

explicit.add(assignment);
byVariable.put(variable.name(), assignment);
assignment.implied().forEach(this::addImplicit);
}

Expand All @@ -347,6 +349,7 @@ private void addImplicit(Assignment assignment) {
}

this.implicit.add(assignment);
byVariable.put(assignment.variable().name(), assignment);
assignment.implied().forEach(this::addImplicit);
}

Expand Down
22 changes: 17 additions & 5 deletions lib/src/io/github/alechenninger/monarch/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.github.alechenninger.monarch;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -32,6 +33,8 @@ public class Variable {
private final List<Assignable> assignables;
private final Inventory inventory;

private final Map<String, Assignment> cachedAssignments = new HashMap<>();

public Variable(String name, List<Assignable> assignables, Inventory inventory) {
this.name = Objects.requireNonNull(name, "name");
this.assignables = Objects.requireNonNull(assignables, "assignables");
Expand Down Expand Up @@ -76,18 +79,27 @@ public Set<String> values(Assignments assignments) {
}

public Assignment assign(String value) {
Optional<Assignable> assignable = assignables.stream()
.filter(a -> a.value().equals(value))
.findFirst();
Assignment assignment;

if (cachedAssignments.containsKey(value)) {
assignment = cachedAssignments.get(value);
} else {
assignment = assignables.stream()
.filter(a -> a.value().equals(value))
.findFirst()
.map(a -> new Assignment(inventory, this, a))
.orElse(null);
cachedAssignments.put(value, assignment);
}

if (!assignable.isPresent()) {
if (assignment == null) {
throw new IllegalArgumentException("Cannot assign value <" + value + "> to variable <" +
name + "> because value is not assignable for this variable. Check your assignment or " +
"add value to inventory. An inventory needs to be comprehensive so it can be used to " +
"discover all of the sources in your hierarchy.");
}

return new Assignment(inventory, this, assignable.get());
return assignment;
}

@Override
Expand Down