package de.ugoe.cs.swe.bnftools.validation;

import java.util.ArrayList;
import java.util.List;

public class RuleDuplication {
	List<DuplicateEntry> dupes = new ArrayList<DuplicateEntry>();

	public List<DuplicateEntry> getDupes() {
		return dupes;
	}

	public void setDupes(List<DuplicateEntry> dupes) {
		this.dupes = dupes;
	}
	
	public void addDupe(DuplicateEntry dupe) {
		dupes.add(dupe);
	}
	
	public List<DuplicateEntry> getFilteredDupes() {
		ArrayList<DuplicateEntry> dupesFiltered = new ArrayList<DuplicateEntry>();
		
		dupesFiltered.addAll(dupes);
		
		boolean change = true;
		while (change) {
			change = false;			
			for (int i=0; i < dupesFiltered.size(); i++) {
				for (int j=0; j < dupesFiltered.size(); j++) {
					DuplicateEntry dupeA = dupesFiltered.get(i);
					DuplicateEntry dupeB = dupesFiltered.get(j);
					
					if (dupeA.getSnippet().contains(dupeB.getSnippet()) && !dupeA.getSnippet().equals(dupeB.getSnippet())) {
						dupesFiltered.remove(j);
						change = true;
					} else if (dupeB.getSnippet().contains(dupeA.getSnippet()) && !dupeB.getSnippet().equals(dupeA.getSnippet())) {
						dupesFiltered.remove(i);
						change = true;
					}
					if (change)
						break;
				}
				if (change)
					break;
			}
		}
		
		return dupesFiltered;
	}
	
}
