Skip to content

Commit

Permalink
Better way to handle filter references
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 committed Aug 26, 2016
1 parent 0e84ca8 commit c97d1e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/main/java/in/twizmwaz/cardinal/module/filter/FilterModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import in.twizmwaz.cardinal.module.filter.type.CreatureFilter;
import in.twizmwaz.cardinal.module.filter.type.CrouchingFilter;
import in.twizmwaz.cardinal.module.filter.type.EntityFilter;
import in.twizmwaz.cardinal.module.filter.type.FilterReference;
import in.twizmwaz.cardinal.module.filter.type.FlyingFilter;
import in.twizmwaz.cardinal.module.filter.type.HoldingFilter;
import in.twizmwaz.cardinal.module.filter.type.LayerFilter;
Expand Down Expand Up @@ -83,13 +84,16 @@
import org.jdom2.Element;
import org.jdom2.located.Located;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@ModuleEntry(depends = {RegionModule.class, TeamModule.class})
public class FilterModule extends AbstractModule implements Listener {

private Map<Match, Map<String, Filter>> filters = Maps.newHashMap();
private Map<Match, List<FilterReference>> references = Maps.newHashMap();

//Static filters. Can be shared across matches, because they use no arguments
public static final Filter ALLOW = new StaticFilter(FilterState.ALLOW);
Expand All @@ -114,6 +118,7 @@ public FilterModule() {
@Override
public boolean loadMatch(@NonNull Match match) {
filters.put(match, Maps.newHashMap());
references.put(match, new ArrayList<>());
for (Element filtersElement : match.getMap().getDocument().getRootElement().getChildren("filters")) {
for (Element filterElement : filtersElement.getChildren()) {
filters.get(match).put("always", ALLOW);
Expand All @@ -125,6 +130,10 @@ public boolean loadMatch(@NonNull Match match) {
}
}
}
for (FilterReference reference : references.get(match)) {
reference.load(this, match);
}
references.remove(match);
return true;
}

Expand Down Expand Up @@ -158,6 +167,12 @@ public Filter getFilter(@NonNull Match match, @NonNull String id) {
return filters.get(match).get(id);
}

public Filter getFilterReference(@NonNull Match match, @NonNull String id) {
FilterReference reference = new FilterReference(id);
references.get(match).add(reference);
return reference;
}

/**
* Parses an element for a filter.
*
Expand Down Expand Up @@ -312,7 +327,7 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu
for (String alternateAttribute : alternateAttributes) {
String filterValue = element.getAttributeValue(alternateAttribute);
if (filterValue != null) {
Filter filter = getFilter(match, filterValue);
Filter filter = getFilterReference(match, filterValue);
if (filter != null) {
return checkFilter(match, id, filter);
}
Expand All @@ -321,7 +336,7 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu

String filterValue = element.getAttributeValue("id");
if (filterValue != null) {
Filter filter = getFilter(match, filterValue);
Filter filter = getFilterReference(match, filterValue);
if (filter != null) {
return checkFilter(match, id, filter);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package in.twizmwaz.cardinal.module.filter.type;

import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.ModuleError;
import in.twizmwaz.cardinal.module.filter.Filter;
import in.twizmwaz.cardinal.module.filter.FilterModule;
import in.twizmwaz.cardinal.module.filter.FilterState;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class FilterReference implements Filter {

private final String id;
private Filter filter;

public void load(FilterModule module, Match match) {
filter = module.getFilter(match, id);
if (filter == null) {
module.getErrors().add(
new ModuleError(module, match.getMap(), new String[]{"Could not find filter for id \'" + id + "\'"}, false));
}
}

@Override
public FilterState evaluate(Object... objects) {
if (filter != null) {
return filter.evaluate(objects);
} else {
return FilterState.ABSTAIN;
}
}

}

0 comments on commit c97d1e3

Please sign in to comment.