- package xyz.veronie.bgg.result;
-
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.function.Predicate;
-
- import javax.inject.Inject;
-
- import org.eclipse.e4.core.di.annotations.Optional;
- import org.eclipse.e4.ui.di.UIEventTopic;
-
- import com.google.gson.Gson;
-
- import xyz.veronie.bgg.types.EventConstants;
- import xyz.veronie.bgg.types.SourceFilter;
-
- public enum ThingProvider {
- INSTANCE;
-
- /// list of things. Each ID is expected to exist exactly once.
- private List<ThingMetaData> thingMetas;
-
- private ThingProvider() {
- thingMetas = new ArrayList<ThingMetaData>();
- }
-
- /// replace current list with new list
- public void replaceThingMetas(ArrayList<ThingMetaData> metas) {
- this.thingMetas = metas;
- }
-
- /// add things from argument to current list iff their ID does not exist yet
- public void addThingMetas(ArrayList<ThingMetaData> metas) {
- for(ThingMetaData tmd : metas) {
- Boolean exists = false;
- for(ThingMetaData thisTmd : this.thingMetas) {
- if(tmd.getId() == thisTmd.getId()) {
- exists = true;
- }
- }
- if(!exists) {
- this.thingMetas.add(tmd);
- }
- }
- }
-
-
- // helper functino for subtractThingMetas
- private static Predicate<ThingMetaData> thingEqual(ThingMetaData other)
- {
- return p -> p.getId() == other.getId();
- }
-
- /// remove the things in the argument from the current list of things (using their ID)
- public void subtractThingMetas(ArrayList<ThingMetaData> metas) {
- for(ThingMetaData tmd : metas) {
- this.thingMetas.removeIf(thingEqual(tmd));
- }
- }
-
- /// keep only things that exist in both argument list and current list (by ID)
- public void intersectThingMetas(ArrayList<ThingMetaData> metas) {
- List<ThingMetaData> cpMetas = new ArrayList<ThingMetaData>(this.thingMetas);
- this.thingMetas.clear();
- for(ThingMetaData tmd : metas) {
- for(ThingMetaData thisTmd : cpMetas) {
- if(tmd.getId() == thisTmd.getId()) {
- this.thingMetas.add(tmd);
- }
- }
- }
- }
-
- /// create a tag of the current list
- public void tagResult() {
- Gson gson = new Gson();
- String resultList = gson.toJson(this.thingMetas);
-
- try {
- BufferedWriter writer = new BufferedWriter(
- new FileWriter("result_" + Long.toString(System.currentTimeMillis())));
- writer.write(resultList);
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
-
- // return the current list of ids
- public List<ThingMetaData> getThingMetas() {
- return thingMetas;
- }
-
- @Inject
- @Optional
- private void subscribeTopicSourceChanged
- (@UIEventTopic(EventConstants.TOPIC_TAG_RESULT)
- String str) {
- System.out.println("Tag result now.");
- this.tagResult();
- }
-
- }
|