package de.gymnew.sudoku.model;

import java.util.HashSet;
import java.util.Set;

public abstract class Cluster {

	protected Set<Field> fields;

	protected Cluster() {
		fields = new HashSet<Field>();
	}

	public Set<Field> getFields() {
		return new HashSet<Field>(fields);
	}

	public boolean hasValue(byte value) {
		for (Field f : fields) {
			if (f.getValue() == value)
				return true;
		}
		return false;
	}

	public byte countNotes(byte note) { // TODO Code-deduplication
		byte count = 0;
		for (Field f : fields) {
			if (f.hasNote(note))
				count++;
		}
		return count;
	}

	public Set<Field> getFieldsWithNote(byte note) {
		Set<Field> out = new HashSet<Field>();
		for (Field f : fields) {
			if (f.hasNote(note))
				out.add(f);
		}
		return out;
	}

	public void removeNotes(byte note) {
		for (Field f : fields) {
			f.deleteNote(note);
		}
	}

	public boolean containsField(Field field) {
		for (Field f : fields) {
			if (f.equals(field))
				return true;
		}
		return false;
	}
}

@@ -475,35 +475,8 @@
 e) %7B
- // TODO Code-deduplication
 %0D%0A%09%09
@@ -484,90 +484,52 @@
 yte 
-c
 ou
-n
 t = 
-0;%0D%0A%09%09for (Field f : fields) %7B%0D%0A%09%09%09if (f.hasNote(note))%0D%0A%09%09%09%09count++;%0D%0A%09%09%7D
+(byte) getFieldsWithNote(note).size();
 %0D%0A%09%09
@@ -535,20 +535,18 @@
 %09return 
-c
 ou
-n
 t;%0D%0A%09%7D%0D%0A

removed code-duplication