import java.util.Observable; /** * Abstract class containing shared update, swap and ordered methods. * * @author Alexander Schaap * @param * Type of word, in order to compare two words. * */ public abstract class AlphabetizerAbstract> implements AlphabetizerInterface { StorageInterface>>> sls; /** * Simple swap method * * @param line * array in which to swap * @param a * index of first element to swap with second * @param b * index of second element to swap with first */ protected void swap( StorageInterface>> line, int a, int b) { StorageInterface> tmp = line.get(b); line.set(b, line.get(a)); line.set(a, tmp); } /** * Check whether two elements are ordered * * @param line * array in which to check * @param a * index of first element * @param b * index of second element * @return true if ordered */ protected boolean ordered( StorageInterface>> line, int a, int b) { // Cannot be because it could possibly compare different types in // compareTo int result = 0, maxComparableNofWords = Math.min(line.get(a).length(), line .get(b).length()); for (int w = 0; line.length() > 0 && result == 0 && w < maxComparableNofWords; w++) { // w for word result = line.get(a).get(w).compareTo(line.get(b).get(w)); } return result < 0; } @SuppressWarnings("unchecked") @Override public void update(Observable ls, Object line) { sort((StorageInterface>>) line); sls.set(sls.length() - 1, (StorageInterface>>) line); } @Override public void connect(StorageInterface>>> sls) { this.sls = sls; } }