Skip to content

Commit

Permalink
move the actionbasedchangelistener to ListeningChangeFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Jun 24, 2017
1 parent 2bb6a37 commit 09a79f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@
*/
package spoon.experimental.modelobs;

import spoon.experimental.modelobs.action.Action;
import spoon.experimental.modelobs.action.AddAction;
import spoon.experimental.modelobs.action.DeleteAction;
import spoon.experimental.modelobs.action.DeleteAllAction;
import spoon.experimental.modelobs.action.UpdateAction;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.path.CtRole;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -36,31 +30,6 @@
*/
public class FineModelChangeListener {

private final List<ActionBasedChangeListener> listeners = new ArrayList<>(2);

public void addModelChangeListener(final ActionBasedChangeListener listener) {
listeners.add(listener);
}

public void removeModelChangeListener(final ActionBasedChangeListener listener) {
listeners.remove(listener);
}

protected void propagateModelChange(final Action action) {
for (ActionBasedChangeListener listener : listeners) {
listener.onAction(action);
if (action instanceof DeleteAllAction) {
listener.onDeleteAll((DeleteAllAction) action);
} else if (action instanceof DeleteAction) {
listener.onDelete((DeleteAction) action);
} else if (action instanceof AddAction) {
listener.onAdd((AddAction) action);
} else if (action instanceof UpdateAction) {
listener.onUpdate((UpdateAction) action);
}
}
}

public void onObjectUpdate(CtElement currentElement, CtRole role, CtElement newValue, CtElement oldValue) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.experimental.modelobs;

import spoon.experimental.modelobs.action.Action;
import spoon.experimental.modelobs.action.AddAction;
import spoon.experimental.modelobs.action.DeleteAction;
import spoon.experimental.modelobs.action.DeleteAllAction;
Expand All @@ -28,6 +29,7 @@
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.path.CtRole;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -37,6 +39,31 @@
*/
public class ListeningChangeFactory extends FineModelChangeListener {

private final List<ActionBasedChangeListener> listeners = new ArrayList<>(2);

public void addModelChangeListener(final ActionBasedChangeListener listener) {
listeners.add(listener);
}

public void removeModelChangeListener(final ActionBasedChangeListener listener) {
listeners.remove(listener);
}

private void propagateModelChange(final Action action) {
for (ActionBasedChangeListener listener : listeners) {
listener.onAction(action);
if (action instanceof DeleteAllAction) {
listener.onDeleteAll((DeleteAllAction) action);
} else if (action instanceof DeleteAction) {
listener.onDelete((DeleteAction) action);
} else if (action instanceof AddAction) {
listener.onAdd((AddAction) action);
} else if (action instanceof UpdateAction) {
listener.onUpdate((UpdateAction) action);
}
}
}

@Override
public void onObjectUpdate(CtElement currentElement, CtRole role, CtElement newValue, CtElement oldValue) {
propagateModelChange(new UpdateAction<>(new ObjectContext(currentElement, role), newValue, oldValue));
Expand Down

0 comments on commit 09a79f5

Please sign in to comment.