Java Code Examples for org.apache.commons.lang3.ArrayUtils#swap()

The following examples show how to use org.apache.commons.lang3.ArrayUtils#swap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamicValueSortedTreeMapTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveRandomly() {
	final int COUNT = 100;
	Random rand = new Random();
	DynamicValueSortedTreeMap<String, Integer> queue = new DynamicValueSortedTreeMap<>();
	HashSet<String> all = new HashSet<>();
	for (int i = 0; i < COUNT; i++) {
		queue.put("Element" + i, rand.nextInt(50));
		all.add("Element" + i);
	}
	checkConsistent(queue);

	String[] shuffled = all.toArray(new String[all.size()]);
	for (int i = 0; i < shuffled.length; i++) {
		ArrayUtils.swap(shuffled, i, i + rand.nextInt(shuffled.length - i));
	}
	for (String s : shuffled) {
		queue.remove(s);
		checkConsistent(queue);
	}
	assertTrue(queue.isEmpty());
	assertTrue(queue.size() == 0);
}
 
Example 2
Source File: WorldHopperPlugin.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
private void insertMenuEntry(MenuEntry newEntry, MenuEntry[] entries, boolean after)
{
	MenuEntry[] newMenu = ObjectArrays.concat(entries, newEntry);

	if (after)
	{
		int menuEntryCount = newMenu.length;
		ArrayUtils.swap(newMenu, menuEntryCount - 1, menuEntryCount - 2);
	}

	client.setMenuEntries(newMenu);
}
 
Example 3
Source File: HiscorePlugin.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
private void insertMenuEntry(MenuEntry newEntry, MenuEntry[] entries)
{
	MenuEntry[] newMenu = ObjectArrays.concat(entries, newEntry);
	int menuEntryCount = newMenu.length;
	ArrayUtils.swap(newMenu, menuEntryCount - 1, menuEntryCount - 2);
	if (client != null)
	{
		client.setMenuEntries(newMenu);
	}
}
 
Example 4
Source File: DynamicValueSortedTreeMapTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueIndices() {
	final int ROUNDS = 1000;
	Random rand = new Random();
	DynamicValueSortedTreeMap<String, Integer> queue = new DynamicValueSortedTreeMap<>();
	int[] vals = // 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22
		new int[] { 0, 0, 1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 10 };
	for (int r = 0; r < ROUNDS; r++) {
		for (int i = 0; i < vals.length; i++) {
			ArrayUtils.swap(vals, i, i + rand.nextInt(vals.length - i));
		}
		for (int i = 0; i < vals.length; i++) {
			queue.put("Element" + i, vals[i]);
		}
		checkConsistent(queue);
		assertEquals(0, queue.values().indexOf(0));
		assertEquals(1, queue.values().lastIndexOf(0));
		assertEquals(2, queue.values().indexOf(1));
		assertEquals(4, queue.values().lastIndexOf(1));
		assertEquals(5, queue.values().indexOf(2));
		assertEquals(5, queue.values().lastIndexOf(2));
		assertEquals(6, queue.values().indexOf(3));
		assertEquals(6, queue.values().lastIndexOf(3));
		assertEquals(7, queue.values().indexOf(4));
		assertEquals(8, queue.values().lastIndexOf(4));
		assertEquals(9, queue.values().indexOf(5));
		assertEquals(10, queue.values().lastIndexOf(5));
		assertEquals(11, queue.values().indexOf(6));
		assertEquals(19, queue.values().lastIndexOf(6));
		assertEquals(-1, queue.values().indexOf(7));
		assertEquals(-1, queue.values().lastIndexOf(7));
		assertEquals(20, queue.values().indexOf(8));
		assertEquals(21, queue.values().lastIndexOf(8));
		assertEquals(-1, queue.values().indexOf(9));
		assertEquals(-1, queue.values().lastIndexOf(9));
		assertEquals(22, queue.values().indexOf(10));
		assertEquals(22, queue.values().lastIndexOf(10));
	}
}
 
Example 5
Source File: HiscorePlugin.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void insertMenuEntry(MenuEntry newEntry, MenuEntry[] entries)
{
	MenuEntry[] newMenu = ObjectArrays.concat(entries, newEntry);
	int menuEntryCount = newMenu.length;
	ArrayUtils.swap(newMenu, menuEntryCount - 1, menuEntryCount - 2);
	client.setMenuEntries(newMenu);
}
 
Example 6
Source File: WorldHopperPlugin.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void insertMenuEntry(MenuEntry newEntry, MenuEntry[] entries, boolean after)
{
	MenuEntry[] newMenu = ObjectArrays.concat(entries, newEntry);

	if (after)
	{
		int menuEntryCount = newMenu.length;
		ArrayUtils.swap(newMenu, menuEntryCount - 1, menuEntryCount - 2);
	}

	client.setMenuEntries(newMenu);
}
 
Example 7
Source File: ArrayUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenArray_whenSwapingElementsWithinARange_thenCorrect() {
    int[] originalArray = { 1, 2, 3, 4, 5 };
    ArrayUtils.swap(originalArray, 0, 3, 2);
    int[] expectedArray = { 4, 5, 3, 1, 2 };
    assertArrayEquals(expectedArray, originalArray);
}
 
Example 8
Source File: ArrayUtilsUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenArray_whenSwapingElementsAtSpecifiedPositions_thenCorrect() {
    int[] originalArray = { 1, 2, 3, 4, 5 };
    ArrayUtils.swap(originalArray, 0, 3);
    int[] expectedArray = { 4, 2, 3, 1, 5 };
    assertArrayEquals(expectedArray, originalArray);
}