Skip to content

Instantly share code, notes, and snippets.

@Spartan322
Last active April 17, 2016 21:57
Show Gist options
  • Save Spartan322/958f81e5ce76745b142732147e977368 to your computer and use it in GitHub Desktop.
Save Spartan322/958f81e5ce76745b142732147e977368 to your computer and use it in GitHub Desktop.
// package and imports
public class ScriptData {
// idToDataList stores word data according to whether words can be identified to it
Map<Integer, Map<String, String>> idToDataList;
// wordLocations will be searched for words using Collections#contains(Object) and will return the integer id if it returns true
List<Collection<String>> wordLocations;
/**
* Retrieves a data map for a word
*
* @param word
* The word to get data for
*/
public Map<String, String> getDataForWord(String word) {
for(int i = 0; i<wordLocations.size(); i++) {
if(wordLocations.get(i).contains(word)) {
Map<String, String> result = idToDataList.get(i);
if(result == null) {
result = new HashMap<String, String>();
idToDataList.put(i, result);
}
return result;
}
}
}
/**
* Retrieves a immutable data map for a word
*
* @param word
* The word to get data for
*/
public Map<String, String> getImmutableData(String word) {
return Collections.unmodifiableMap(this.getDataForWord(word));
}
/**
* Registers words together for data association
*
* @param words
* The words to register
*/
public ScriptData add(Collection<String> words) {
if(words == null || words.size() < 1) return this;
wordLocations.add(words);
return this;
}
/**
* Registers words together for data association
*
* @param words
* The words to register
*/
public ScriptData add(String ...words) {
if(words == null) return this;
return this.add(new ArrayList<String>(Arrays.asList(words))); // Prefer to stay consistent and allow size mutability
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment