All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.palexdev.mfxcore.collections.GenericAddRemoveChange Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2022 Parisi Alessandro - [email protected]
 * This file is part of MaterialFX (https://github.com/palexdev/MaterialFX)
 *
 * MaterialFX is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * MaterialFX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with MaterialFX. If not, see .
 */

package io.github.palexdev.mfxcore.collections;

import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

import java.util.Collections;
import java.util.List;

abstract class NonIterableChange extends ListChangeListener.Change {
	private final int from;
	private final int to;
	private boolean invalid = true;
	private static final int[] EMPTY_PERM = new int[0];

	protected NonIterableChange(int from, int to, ObservableList list) {
		super(list);
		this.from = from;
		this.to = to;
	}

	public int getFrom() {
		checkState();
		return from;
	}

	public int getTo() {
		checkState();
		return to;
	}

	protected int[] getPermutation() {
		checkState();
		return EMPTY_PERM;
	}

	public boolean next() {
		if (invalid) {
			invalid = false;
			return true;
		} else {
			return false;
		}
	}

	public void reset() {
		invalid = true;
	}

	public void checkState() {
		if (invalid) {
			throw new IllegalStateException("Invalid Change state: next() must be called before inspecting the Change.");
		}
	}

	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 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);
		}

		public List getRemoved() {
			return Collections.emptyList();
		}

		public boolean wasUpdated() {
			return true;
		}
	}

	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;
		}

		public List getRemoved() {
			checkState();
			return Collections.emptyList();
		}

		protected int[] getPermutation() {
			checkState();
			return permutation;
		}
	}

	public static class SimpleAddChange extends NonIterableChange {
		public SimpleAddChange(int from, int to, ObservableList list) {
			super(from, to, list);
		}

		public boolean wasRemoved() {
			checkState();
			return false;
		}

		public List getRemoved() {
			checkState();
			return Collections.emptyList();
		}
	}

	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);
		}

		public boolean wasRemoved() {
			checkState();
			return true;
		}

		public List getRemoved() {
			checkState();
			return removed;
		}
	}

	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;
		}

		public List getRemoved() {
			checkState();
			return removed;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy