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

net.vectorpublish.desktop.vp.utils.SetUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016, Peter Rader. All rights reserved.
 *  ___ ___               __                 ______         __     __  __         __
 * |   |   |.-----..----.|  |_ .-----..----.|   __ \.--.--.|  |--.|  ||__|.-----.|  |--.
 * |   |   ||  -__||  __||   _||  _  ||   _||    __/|  |  ||  _  ||  ||  ||__ --||     |
 *  \_____/ |_____||____||____||_____||__|  |___|   |_____||_____||__||__||_____||__|__|
 *
 * http://www.gnu.org/licenses/gpl-3.0.html
 */
package net.vectorpublish.desktop.vp.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import net.vectorpublish.desktop.vp.api.LayerTreeNode;
import net.vectorpublish.desktop.vp.api.ui.MouseParticipant;
import net.vectorpublish.desktop.vp.api.ui.kf.Keyframe;
import net.vectorpublish.desktop.vp.api.vpd.VectorPublishNode;

public class SetUtils {
	public static final class Gap {
		public final T after;
		public final T before;

		public Gap(T before, T after) {
			this.before = before;
			this.after = after;
		}
	}

	public static final Comparator KEYFRAME_BY_TIME = new Comparator() {
		@Override
		public int compare(Keyframe o1, Keyframe o2) {
			return o1.getMoment().getMS() - o2.getMoment().getMS();
		}
	};

	public static  List asList(Enumeration children, Class clazz) {
		final List list = new LinkedList<>();
		while (children.hasMoreElements()) {
			final S child = children.nextElement();
			if (clazz.isInstance(child)) {
				list.add((T) child);
			}
		}
		return list;
	}

	public static  List asList(Enumeration children) {
		final List list = new LinkedList<>();
		while (children.hasMoreElements()) {
			list.add(children.nextElement());
		}
		return list;
	}

	/**
	 * Finds recursievly all nodes extending a specific Class.
	 * 

* If the node given by the parameter also extends the class, it is * included. * * @param node * The node. * @param clazz * The class(extending {@link VectorPublishNode}. * @return All nodes extending the class, never null. */ public static List find(TreeNode node, Class clazz) { final List list = new LinkedList<>(); find(node, clazz, list); return list; } private static void find(TreeNode node, Class clazz, final List list) { if (clazz.isInstance(node)) { list.add((T) node); } final Enumeration c = node.children(); while (c.hasMoreElements()) { final TreeNode child = (TreeNode) c.nextElement(); find(child, clazz, list); } } public static A findFirst(Collection elements) { return elements.iterator().next(); } public static A findLast(Collection keyframes) { final A[] array = (A[]) keyframes.toArray(); return array[array.length - 1]; } public static Set> nodesToImmutableIndexs( final Collection selectedNodes) { final LinkedHashSet> selections = new LinkedHashSet<>(); for (final VectorPublishNode nodeWalker : selectedNodes) { selections.add(nodeToImmutableIndex(nodeWalker)); } return Collections.unmodifiableSet(selections); } /** * Gives the indexs of the nodes that are parent to the node specified. * * @param node * The node specified. * @return The list of indexs required to find the node. If the Node is * root, {@link Collections#EMPTY_LIST an empty list} is returned. */ public static List nodeToImmutableIndex(VectorPublishNode node) { if (node.getParent() == null) { return Collections.EMPTY_LIST; } final List selection = new LinkedList<>(); while (node.getParent() != null) { final int index = node.getParent().getIndex(node); selection.add(index); node = node.getParent(); } Collections.reverse(selection); return selection; } public static void reverse(LinkedHashSet set) { synchronized (set) { final T[] array = (T[]) set.toArray(); for (int i = array.length - 1; i > -1; i--) { final T e = array[i]; set.remove(e); set.add(e); } } } public static Collection> gaps(Collection e) { T prev = null; List> gaps = e.size() > 100 ? new LinkedList<>() : new ArrayList<>(); for (T current : e) { if (prev == null) { prev = current; continue; } gaps.add(new Gap(prev, current)); prev = current; } return Collections.unmodifiableCollection(gaps); } @Deprecated private SetUtils() { }; public static boolean contains(LayerTreeNode node, Class clazz) { if (clazz.isInstance(node)) { return true; } Enumeration children = node.children(); while (children.hasMoreElements()) { VectorPublishNode vectorPublishNode = (VectorPublishNode) children.nextElement(); if (contains(vectorPublishNode, clazz)) { return true; } } return false; } /** * Find backwards up to root an {@link VectorPublishNode node} extending a * specific type. *

* Will only respect direct parents of the walker. If unfortunatelly a * null has been passed as walker or as type or both: * null will be returned. * * @param walker * The node to start from, not null. * @param type * The type the return must extend from, not null. * @return The node extending type or null if no such parent * has been found. */ @SuppressWarnings("unchecked") public static T closest(VectorPublishNode walker, Class type) { if (type == null) { return null; } while (!type.isInstance(walker) && walker != null) { walker = walker.getParent(); } return (T) walker; } /** * Returns a collection of {@link MouseParticipant} from nodes. * * @param collection * The nodes, not null . * @param clazz * The {@link MouseParticipant}, not null. * @param * The type of the class set by {@code clazz}. * @return The {@link MouseParticipant MouseParticipants} who are an * instance of T, never null. */ public static Set getParticipiants(Collection collection, Class clazz) { Set set = new LinkedHashSet<>(); for (VectorPublishNode element : collection) { MouseParticipant participant = element.getParticipant(); if (clazz.isInstance(participant)) { set.add((T) participant); } } return set; } public static TreePath createPath(TreeNode node) { final LinkedHashSet nodes = new LinkedHashSet<>(); while (node != null) { nodes.add(node); node = node.getParent(); } SetUtils.reverse(nodes); return new TreePath(nodes.toArray()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy