Search in sources :

Example 1 with ThreadLocalRandom

use of io.netty.util.internal.ThreadLocalRandom in project netty by netty.

the class LongPriorityQueueTest method mustReturnValuesInOrder.

@Test
public void mustReturnValuesInOrder() {
    ThreadLocalRandom tlr = ThreadLocalRandom.current();
    int initialValues = tlr.nextInt(5, 30);
    ArrayList<Long> values = new ArrayList<Long>();
    for (int i = 0; i < initialValues; i++) {
        values.add(tlr.nextLong(0, Long.MAX_VALUE));
    }
    LongPriorityQueue pq = new LongPriorityQueue();
    assertTrue(pq.isEmpty());
    for (Long value : values) {
        pq.offer(value);
    }
    Collections.sort(values);
    int valuesToRemove = initialValues / 2;
    ListIterator<Long> itr = values.listIterator();
    for (int i = 0; i < valuesToRemove; i++) {
        assertTrue(itr.hasNext());
        assertThat(pq.poll()).isEqualTo(itr.next());
        itr.remove();
    }
    int moreValues = tlr.nextInt(5, 30);
    for (int i = 0; i < moreValues; i++) {
        long value = tlr.nextLong(0, Long.MAX_VALUE);
        pq.offer(value);
        values.add(value);
    }
    Collections.sort(values);
    itr = values.listIterator();
    while (itr.hasNext()) {
        assertThat(pq.poll()).isEqualTo(itr.next());
    }
    assertTrue(pq.isEmpty());
    assertThat(pq.poll()).isEqualTo(LongPriorityQueue.NO_VALUE);
}
Also used : ArrayList(java.util.ArrayList) ThreadLocalRandom(io.netty.util.internal.ThreadLocalRandom) Test(org.junit.jupiter.api.Test)

Example 2 with ThreadLocalRandom

use of io.netty.util.internal.ThreadLocalRandom in project netty by netty.

the class LongPriorityQueueTest method internalRemoveOfAllElements.

@Test
public void internalRemoveOfAllElements() {
    ThreadLocalRandom tlr = ThreadLocalRandom.current();
    int initialValues = tlr.nextInt(5, 30);
    ArrayList<Long> values = new ArrayList<Long>();
    LongPriorityQueue pq = new LongPriorityQueue();
    for (int i = 0; i < initialValues; i++) {
        long value = tlr.nextLong(0, Long.MAX_VALUE);
        pq.offer(value);
        values.add(value);
    }
    for (Long value : values) {
        pq.remove(value);
    }
    assertTrue(pq.isEmpty());
    assertThat(pq.poll()).isEqualTo(LongPriorityQueue.NO_VALUE);
}
Also used : ArrayList(java.util.ArrayList) ThreadLocalRandom(io.netty.util.internal.ThreadLocalRandom) Test(org.junit.jupiter.api.Test)

Example 3 with ThreadLocalRandom

use of io.netty.util.internal.ThreadLocalRandom in project netty by netty.

the class LongLongHashMapTest method randomOperations.

@Test
public void randomOperations() {
    int operations = 6000;
    ThreadLocalRandom tlr = ThreadLocalRandom.current();
    Map<Long, Long> expected = new HashMap<Long, Long>();
    LongLongHashMap actual = new LongLongHashMap(-1);
    OfLong itr = tlr.longs(0, operations).limit(operations * 50).iterator();
    while (itr.hasNext()) {
        long value = itr.nextLong();
        if (expected.containsKey(value)) {
            assertThat(actual.get(value)).isEqualTo(expected.get(value));
            if (tlr.nextBoolean()) {
                actual.remove(value);
                expected.remove(value);
                assertThat(actual.get(value)).isEqualTo(-1);
            } else {
                long v = expected.get(value);
                assertThat(actual.put(value, -v)).isEqualTo(expected.put(value, -v));
            }
        } else {
            assertThat(actual.get(value)).isEqualTo(-1);
            assertThat(actual.put(value, value)).isEqualTo(-1);
            expected.put(value, value);
        }
    }
}
Also used : OfLong(java.util.PrimitiveIterator.OfLong) HashMap(java.util.HashMap) OfLong(java.util.PrimitiveIterator.OfLong) ThreadLocalRandom(io.netty.util.internal.ThreadLocalRandom) Test(org.junit.jupiter.api.Test)

Example 4 with ThreadLocalRandom

use of io.netty.util.internal.ThreadLocalRandom in project async-http-client by AsyncHttpClient.

the class WebSocketUtils method getWebSocketKey.

public static String getWebSocketKey() {
    byte[] nonce = new byte[16];
    ThreadLocalRandom random = ThreadLocalRandom.current();
    for (int i = 0; i < nonce.length; i++) {
        nonce[i] = (byte) random.nextInt(256);
    }
    return Base64.getEncoder().encodeToString(nonce);
}
Also used : ThreadLocalRandom(io.netty.util.internal.ThreadLocalRandom)

Example 5 with ThreadLocalRandom

use of io.netty.util.internal.ThreadLocalRandom in project Glowstone by GlowstoneMC.

the class GlowEnderPearl method collide.

/**
 * Process teleportation when collide with a block.
 *
 * @param block the block that the ender pearl collides with
 */
@Override
public void collide(Block block) {
    ProjectileSource source = getShooter();
    if (source instanceof Entity) {
        Location destination = getLocation();
        Entity entity = (Entity) source;
        Location entityLocation = entity.getLocation();
        // Add 0.3 to Y value. Otherwise the eneity will get stuck inside a block.
        destination.add(0, 0.3, 0);
        // Renew the pitch and yaw value right before teleportation.
        destination.setPitch(entityLocation.getPitch());
        destination.setYaw(entityLocation.getYaw());
        entity.teleport(destination, PlayerTeleportEvent.TeleportCause.ENDER_PEARL);
        // Give fall damage to the eneity that threw this ender pearl.
        if (source instanceof LivingEntity) {
            ((LivingEntity) entity).damage(ENDER_PEARL_DAMAGE, EntityDamageEvent.DamageCause.FALL);
        }
    }
    // Spawn endermite for 5% chance.
    ThreadLocalRandom random = ThreadLocalRandom.current();
    if (random.nextInt(100) < 5) {
        getWorld().spawn(location, GlowEndermite.class, CreatureSpawnEvent.SpawnReason.ENDER_PEARL);
    }
    remove();
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) ThreadLocalRandom(io.netty.util.internal.ThreadLocalRandom) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Location(org.bukkit.Location)

Aggregations

ThreadLocalRandom (io.netty.util.internal.ThreadLocalRandom)6 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)1 OfLong (java.util.PrimitiveIterator.OfLong)1 Location (org.bukkit.Location)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 ProjectileSource (org.bukkit.projectiles.ProjectileSource)1