package de.ugoe.cs.autoquest.usability.util; import java.util.List; import com.google.common.base.CharMatcher; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.collect.HashMultiset; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Multiset; import de.ugoe.cs.autoquest.eventcore.gui.TextInput; import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; public class TextInputUtil { public static Multiset aggregateEnteredTextFromTextInputs(List nodesWithTextInputEvents) { List> allTextInputs = Lists.newArrayList(); for(ITaskTreeNode nodeWithTextInput : nodesWithTextInputEvents) { TextInput textInput = (TextInput) ((IEventTask) nodeWithTextInput).getEventType(); allTextInputs.add(splitTextIntoWordsAndSigns(textInput.getEnteredText())); } return HashMultiset.create(Iterables.concat(allTextInputs)); } public static Iterable splitTextIntoWordsAndSigns(String enteredText) { CharMatcher onlyWords = CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsJavaIdentifierPartPredicate())); CharMatcher onlySigns = CharMatcher.WHITESPACE.or(CharMatcher.forPredicate(characterIsJavaIdentifierPartPredicate()).negate()); Iterable words = Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText); Iterable signs = Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText); return Iterables.concat(words, signs); } public static Predicate characterIsJavaIdentifierPartPredicate() { return new Predicate() { @Override public boolean apply(Character character) { return !Character.isJavaIdentifierPart(character); } }; } public static Predicate characterIsLetterOrDigitPredicate() { return new Predicate() { @Override public boolean apply(Character character) { return !Character.isLetterOrDigit(character); } }; } }