Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
*
*
* Copyright (c) 2002-2006 IBM Corporation 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:
* IBM - Initial API and implementation
*
*
*
* $Id: ECollections.java,v 1.8 2008/12/13 15:54:18 emerks Exp $
*/
package org.eclipse.emf.common.util;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* Support for {@link #EMPTY_ELIST empty} and {@link #unmodifiableEList unmodifiable} ELists.
*/
public class ECollections
{
// Suppress default constructor for noninstantiability.
private ECollections()
{
super();
}
/**
* Moves the object to the new position, if is in the list.
* @param list
* @param newPosition the position of the object after the move.
* @param object the object to move.
*/
public static void move(List list, int newPosition, T object)
{
if (list instanceof EList>)
{
((EList)list).move(newPosition, object);
}
else
{
list.remove(object);
list.add(newPosition, object);
}
}
/**
* Moves the object from the old position to the new position.
* @param list
* @param targetIndex the position of the object after the move.
* @param sourceIndex the position of the object before the move.
* @return the moved object
*/
public static T move(List list, int targetIndex, int sourceIndex)
{
if (list instanceof EList>)
{
return ((EList)list).move(targetIndex, sourceIndex);
}
else
{
T object = list.remove(sourceIndex);
list.add(targetIndex, object);
return object;
}
}
/**
* Reverses the order of the elements in the specified EList.
*/
public static void reverse(EList> list)
{
int last = list.size() - 1;
for (int i = 0; i < last; i++)
{
list.move(i, last);
}
}
/**
* Searches for the first occurrence of the given argument in list starting from
* a specified index. The equality is tested using the operator == and
* the equals method.
* @param list
* @param o an object (can be null)
* @param fromIndex
* @return the index of the first occurrence of the argument in this
* list (where index>=fromIndex); returns -1 if the
* object is not found.
* @since 2.1.0
*/
public static int indexOf(List> list, Object o, int fromIndex)
{
if (fromIndex < 0)
{
fromIndex = 0;
}
int size = list.size();
for (int i = fromIndex; i < size; i++)
{
Object element = list.get(i);
if (o == null)
{
if (element == null)
{
return i;
}
}
else if (o == element || o.equals(element))
{
return i;
}
}
return -1;
}
/**
* Sorts the specified list. Use this method instead of
* {@link Collections#sort(java.util.List)} to
* avoid errors when sorting unique lists.
* @since 2.1.0
*/
public static void sort(EList> list)
{
Object[] listAsArray = list.toArray();
Arrays.sort(listAsArray);
for (int i=0; i < listAsArray.length; i++)
{
int oldIndex = indexOf(list, listAsArray[i], i);
if (i != oldIndex)
{
list.move(i, oldIndex);
}
}
}
/**
* Sorts the specified list based on the order defined by the
* specified comparator. Use this method instead of
* {@link Collections#sort(java.util.List, java.util.Comparator)} to
* avoid errors when sorting unique lists.
* @since 2.1.0
*/
public static void sort(EList list, Comparator super T> comparator)
{
Object[] listAsArray = list.toArray();
@SuppressWarnings("unchecked") Comparator