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

org.eclipse.equinox.p2.query.CollectionResult Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2010 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.query;

import java.lang.reflect.Array;
import java.util.*;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * This class allows to adapt java collections to a p2 a query result  and as such something queryable  
 * @since 2.0
 */
public class CollectionResult implements IQueryResult {
	private final Collection collection;

	public CollectionResult(Collection collection) {
		this.collection = collection == null ? Collections. emptySet() : collection;
	}

	public IQueryResult query(IQuery query, IProgressMonitor monitor) {
		return query.perform(iterator());
	}

	public boolean isEmpty() {
		return collection.isEmpty();
	}

	public Iterator iterator() {
		return collection.iterator();
	}

	public T[] toArray(Class clazz) {
		int size = collection.size();
		@SuppressWarnings("unchecked")
		T[] result = (T[]) Array.newInstance(clazz, size);
		if (size != 0)
			collection.toArray(result);
		return result;
	}

	public Set toSet() {
		return new HashSet(collection);
	}

	public Set toUnmodifiableSet() {
		return collection instanceof Set ? Collections. unmodifiableSet((Set) collection) : toSet();
	}

	@Override
	public String toString() {
		return collection.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy