Permalink
Browse files

APPNG-2203 added field 'hits' to option

  • Loading branch information...
madness-inc committed Jul 26, 2018
1 parent 1946a66 commit f76e0d3b6c11180ffd68f4eca3a26e6aeeb050b4
@@ -22,6 +22,7 @@
import org.appng.api.model.Identifiable;
import org.appng.api.model.NameProvider;
import org.appng.api.model.Named;
import org.appng.api.support.OptionOwner.HitCounter;
import org.appng.api.support.OptionOwner.Selector;
import org.appng.xml.platform.Option;
@@ -424,7 +425,8 @@
* @return a new {@link OptionOwner} (actually a {@link org.appng.xml.platform.Selection} or a
* {@link org.appng.xml.platform.OptionGroup})
*/
public <S> T fromObjects(String id, String titleId, S[] allElements, Selector selector, NameProvider<S> nameProvider) {
public <S> T fromObjects(String id, String titleId, S[] allElements, Selector selector,
NameProvider<S> nameProvider) {
T owner = getOwner(id, titleId);
addOptions(allElements, new ArrayList<S>(), owner, nameProvider);
applySelector(owner, selector);
@@ -475,6 +477,38 @@
return owner;
}
/**
* Counts and sets the hits for the options based on their value.
*
* @param options
* some options
* @param counter
* a {@link HitCounter}
*
* @see Option#getValue()
* @see Option#getHits()
*/
public void countHits(Iterable<Option> options, HitCounter<String> counter) {
for (Option option : options) {
option.setHits(counter.count(option.getValue()));
}
}
/**
* Counts and sets the hits for the owner's options based on their value.
*
* @param optionOwner
* an {@link OptionOwner}
* @param counter
* a {@link HitCounter}
*
* @see Option#getValue()
* @see Option#getHits()
*/
public void countHits(OptionOwner optionOwner, HitCounter<String> counter) {
countHits(optionOwner.getOptions(), counter);
}
private <I extends Identifiable<?>> void addIdentifiableOptions(Iterable<? extends I> allElements,
Collection<? extends I> selectedElements, OptionOwner owner, NameProvider<I> nameProvider) {
for (I element : allElements) {
@@ -514,6 +548,7 @@
private void applySelector(OptionOwner owner, Selector selector) {
for (Option option : owner.getOptions()) {
selector.select(option);
option.setHits(selector.count(option.getValue()));
}
}
@@ -28,16 +28,41 @@
org.appng.xml.platform.Option addOption(String name, String value, boolean selected);
/**
* A selector decides whether or not a given {@link Option} should be selected
* A selector decides whether or not a given {@link Option} should be selected. Also implements {@link HitCounter},
* using the options's value and returning {@code null} by default.
*
* @see Option#isSelected()
*/
public interface Selector {
public interface Selector extends HitCounter<String> {
/**
* Selects or de-selects an option by calling {@link Option#setSelected(Boolean)}.
*
* @param o
* the {@link Option} which might be selected
*/
void select(Option o);
default Integer count(String optionValue) {
return null;
}
}
/**
* Counts the hits for {@link Option}s.
*
* @param <T>
* the type of the elements to count
* @see Option#getHits()
*/
public interface HitCounter<T> {
/**
* Counts the hits for the option created from the given element.
*
* @param element
* the element to count the hits for
* @return the number of hits for this option
*/
Integer count(T element);
}
class OptionOwnerBase implements OptionOwner {
@@ -68,6 +68,11 @@ public void select(Option o) {
o.setSelected(true);
}
}
@Override
public Integer count(String optionValue) {
return 5;
}
};
enum Force {
@@ -125,6 +130,18 @@ public void testFromNamed() {
assertSelectionEquals(s4, s6);
}
@Test
public void testHitCounter() {
org.appng.api.support.SelectionFactory.Selection s1 = selectionFactory.fromNamed(id, title, allElements,
selectedElements);
org.appng.api.support.SelectionFactory.Selection s2 = selectionFactory.fromNamed(id, title, allElements,
selector);
selectionFactory.countHits(s1, o -> 1);
s1.getOptions().forEach(o -> Assert.assertEquals(Integer.valueOf(1), o.getHits()));
s2.getOptions().forEach(o -> Assert.assertEquals(Integer.valueOf(5), o.getHits()));
}
@Test
public void testFromIdentifiable() {
Selection s1 = selectionFactory.fromIdentifiable(id, title, allElements, selectedElements);
@@ -163,6 +163,7 @@ protected Option getOption(String binding, org.appng.xml.platform.Option o, Coll
option.setSelected(o.isSelected());
}
option.setValue(o.getValue());
option.setHits(o.getHits());
return option;
}
@@ -397,6 +397,9 @@ definitions:
selected:
type: boolean
description: "Whether or not this option is selected."
hits:
type: integer
description: "Tells how many hits there are for this option."
options:
type: array
description: "An option can act as a group and thus can itself own a list of options."
@@ -2614,6 +2614,16 @@ If empty, the template is being applied to any output-type.]]>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
<xs:attribute name="hits" type="xs:int" use="optional">
<xs:annotation>
<xs:documentation>Tells how many hits there are for this option.</xs:documentation>
<xs:appinfo>
<jxb:property>
<jxb:javadoc>Tells how many hits there are for this option.</jxb:javadoc>
</jxb:property>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
<xs:attribute name="disabled" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used for marking the option as disabled.</xs:documentation>

0 comments on commit f76e0d3

Please sign in to comment.