/* * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package com.sun.javafx.collections; import java.util.Collections; import java.util.List; import javafx.collections.ListChangeListener.Change; import javafx.collections.ObservableList; public abstract class NonIterableChange extends Change { private final int from; private final int to; private boolean invalid = true; protected NonIterableChange(int from, int to, ObservableList list) { super(list); this.from = from; this.to = to; } @Override public int getFrom() { checkState(); return from; } @Override public int getTo() { checkState(); return to; } private static final int[] EMPTY_PERM = new int[0]; @Override protected int[] getPermutation() { checkState(); return EMPTY_PERM; } @Override public boolean next() { if (invalid) { invalid = false; return true; } return false; } @Override public void reset() { invalid = true; } public void checkState() { if (invalid) { throw new IllegalStateException("Invalid Change state: next() must be called before inspecting the Change."); } } @Override public String toString() { boolean oldInvalid = invalid; invalid = false; String ret; if (wasPermutated()) { ret = ChangeHelper.permChangeToString(getPermutation()); } else if (wasUpdated()) { ret = ChangeHelper.updateChangeToString(from, to); } else { ret = ChangeHelper.addRemoveChangeToString(from, to, getList(), getRemoved()); } invalid = oldInvalid; return "{ " + ret + " }"; } public static class GenericAddRemoveChange extends NonIterableChange { private final List removed; public GenericAddRemoveChange(int from, int to, List removed, ObservableList list) { super(from, to, list); this.removed = removed; } @Override public List getRemoved() { checkState(); return removed; } } public static class SimpleRemovedChange extends NonIterableChange { private final List removed; public SimpleRemovedChange(int from, int to, E removed, ObservableList list) { super(from, to, list); this.removed = Collections.singletonList(removed); } @Override public boolean wasRemoved() { checkState(); return true; } @Override public List getRemoved() { checkState(); return removed; } } public static class SimpleAddChange extends NonIterableChange { public SimpleAddChange(int from, int to, ObservableList list) { super(from, to, list); } @Override public boolean wasRemoved() { checkState(); return false; } @Override public List getRemoved() { checkState(); return Collections.emptyList(); } } public static class SimplePermutationChange extends NonIterableChange{ private final int[] permutation; public SimplePermutationChange(int from, int to, int[] permutation, ObservableList list) { super(from, to, list); this.permutation = permutation; } @Override public List getRemoved() { checkState(); return Collections.emptyList(); } @Override protected int[] getPermutation() { checkState(); return permutation; } } public static class SimpleUpdateChange extends NonIterableChange{ public SimpleUpdateChange(int position, ObservableList list) { this(position, position + 1, list); } public SimpleUpdateChange(int from, int to, ObservableList list) { super(from, to, list); } @Override public List getRemoved() { return Collections.emptyList(); } @Override public boolean wasUpdated() { return true; } } }