You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
	
	
		
			
				
					
						
						
							|  | package xyz.veronie.bgg.result;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import org.eclipse.e4.core.di.annotations.Creatable;
import xyz.veronie.bgg.localdb.LocalDbAdapterService;
@Creatable
@Singleton
public class ThingProvider {
	private LocalDbAdapterService localDbAdapterService;
	
	/// list of things. Each ID is expected to exist exactly once.
	private List<Thing> things;
	
	public ThingProvider() {}
	@PostConstruct
	public void init() {
		localDbAdapterService = new LocalDbAdapterService();
		things = new ArrayList<Thing>();
	}
	
	/// replace current list with new list
	public void replaceThings(ArrayList<Thing> things) {
		this.things = things;
	}
	
	/// add things from argument to current list iff their ID does not exist yet
	public void addThings(ArrayList<Thing> things) {
		for(Thing tmd : things) {
			Boolean exists = false;
			for(Thing thisThing : this.things) {
				if(tmd.getId() == thisThing.getId()) {
					exists = true;
				}
			}
			if(!exists) {
				this.things.add(tmd);
			}
		}
	}
	
	
	// helper function for subtractThingMetas
	private static Predicate<Thing> thingEqual(final Thing other) 
	{
	    return p -> p.getId() == other.getId();
	}
	
	/// remove the things in the argument from the current list of things (using their ID)
	public void subtractThings(ArrayList<Thing> things) {
		for(Thing thing : things) {
			this.things.removeIf(thingEqual(thing));
		}
	}
	/// keep only things that exist in both argument list and current list (by ID)
	public void intersectThings(ArrayList<Thing> things) {
		List<Thing> cpThings = new ArrayList<Thing>(this.things);
		this.things.clear();
		for(Thing thing : things) {
			for(Thing thisThing : cpThings) {
				if(thing.getId() == thisThing.getId()) {
					this.things.add(thing);
				}
			}
		}
	}
	
	/// store current list in DB
	public void storeList() {
		localDbAdapterService.storeThingList(this.things, "TestList");
	}
	
	
	// return the current list of ids
	public List<Thing> getThings() {
        return things;
    }
	
}
 |