Search in sources :

Example 46 with Function

use of java.util.function.Function in project disunity by ata4.

the class TypeTreeV3 method writeNode.

private void writeNode(DataWriter out, Node<T> node) throws IOException {
    List<T> types = new ArrayList<>();
    serializeNode(node, types, 0);
    // build string table
    AtomicInteger index = new AtomicInteger();
    Map<String, Integer> localMap = new LinkedHashMap<>();
    Map<String, Integer> commonMap = StringTable.commonStrings(revision.major()).inverse();
    Function<String, Integer> addStringOffset = typeName -> {
        if (commonMap.containsKey(typeName)) {
            return commonMap.get(typeName);
        } else if (localMap.containsKey(typeName)) {
            return localMap.get(typeName);
        } else {
            int stringIndex = index.getAndAdd(typeName.length() + 1);
            localMap.put(typeName, stringIndex);
            return stringIndex;
        }
    };
    // apply string offsets
    types.forEach(type -> {
        type.typeOffset(addStringOffset.apply(type.typeName()));
        type.nameOffset(addStringOffset.apply(type.fieldName()));
    });
    out.writeInt(types.size());
    out.writeInt(index.get());
    for (T type : types) {
        out.writeStruct(type);
    }
    for (String string : localMap.keySet()) {
        out.writeStringNull(string);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiMap(com.google.common.collect.BiMap) DataWriter(info.ata4.io.DataWriter) Node(info.ata4.util.collection.Node) IOException(java.io.IOException) SerializedFileException(info.ata4.junity.serialize.SerializedFileException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) UnityHash128(info.ata4.junity.UnityHash128) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DataReader(info.ata4.io.DataReader) UnityVersion(info.ata4.junity.UnityVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 47 with Function

use of java.util.function.Function in project pulsar by yahoo.

the class ConcurrentOpenHashMapTest method testComputeIfAbsent.

@Test
public void testComputeIfAbsent() {
    ConcurrentOpenHashMap<Integer, Integer> map = new ConcurrentOpenHashMap<>(16, 1);
    AtomicInteger counter = new AtomicInteger();
    Function<Integer, Integer> provider = key -> counter.getAndIncrement();
    assertEquals(map.computeIfAbsent(0, provider).intValue(), 0);
    assertEquals(map.get(0).intValue(), 0);
    assertEquals(map.computeIfAbsent(1, provider).intValue(), 1);
    assertEquals(map.get(1).intValue(), 1);
    assertEquals(map.computeIfAbsent(1, provider).intValue(), 1);
    assertEquals(map.get(1).intValue(), 1);
    assertEquals(map.computeIfAbsent(2, provider).intValue(), 2);
    assertEquals(map.get(2).intValue(), 2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertNull(org.testng.Assert.assertNull) Assert.fail(org.testng.Assert.fail) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) Random(java.util.Random) Test(org.testng.annotations.Test) Function(java.util.function.Function) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertTrue(org.testng.Assert.assertTrue) ConcurrentOpenHashMap(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap) Assert.assertFalse(org.testng.Assert.assertFalse) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentOpenHashMap(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Example 48 with Function

use of java.util.function.Function in project AuthMeReloaded by AuthMe.

the class MessageFileHandlerProviderTest method shouldCreateHandler.

@Test
public void shouldCreateHandler() {
    // given
    String language = "fr";
    given(settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)).willReturn(language);
    MessageFileHandlerProvider provider = Mockito.spy(handlerProvider);
    Function<String, String> fileFunction = lang -> "file_" + lang + ".txt";
    File file = new File(dataFolder, "some_file.txt");
    doReturn(file).when(provider).initializeFile(language, fileFunction);
    // when
    MessageFileHandler handler = provider.initializeHandler(fileFunction);
    // then
    assertThat(handler, not(nullValue()));
    verify(settings).getProperty(PluginSettings.MESSAGES_LANGUAGE);
    verify(provider).initializeFile(language, fileFunction);
}
Also used : BeforeClass(org.junit.BeforeClass) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Matchers.not(org.hamcrest.Matchers.not) DataFolder(fr.xephi.authme.initialization.DataFolder) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) TestHelper.getJarFile(fr.xephi.authme.TestHelper.getJarFile) Files(com.google.common.io.Files) BDDMockito.given(org.mockito.BDDMockito.given) BeforeInjecting(ch.jalu.injector.testing.BeforeInjecting) Matchers.nullValue(org.hamcrest.Matchers.nullValue) TestHelper(fr.xephi.authme.TestHelper) PluginSettings(fr.xephi.authme.settings.properties.PluginSettings) Mockito.doReturn(org.mockito.Mockito.doReturn) Description(org.hamcrest.Description) Settings(fr.xephi.authme.settings.Settings) InjectDelayed(ch.jalu.injector.testing.InjectDelayed) Test(org.junit.Test) IOException(java.io.IOException) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) Matcher(org.hamcrest.Matcher) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TemporaryFolder(org.junit.rules.TemporaryFolder) DelayedInjectionRunner(ch.jalu.injector.testing.DelayedInjectionRunner) TestHelper.getJarFile(fr.xephi.authme.TestHelper.getJarFile) File(java.io.File) Test(org.junit.Test)

Example 49 with Function

use of java.util.function.Function in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method intsDataProvider.

@DataProvider(name = "ints")
public static Object[][] intsDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Integer>, IntStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToInt(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints().limit(%d)", SIZE), () -> new SplittableRandom().ints().limit(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
    data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d)", SIZE), () -> new SplittableRandom().ints(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
    data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt())", SIZE), () -> rbsf.apply(sr -> sr.nextInt())), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final int origin = o;
            final int bound = b;
            data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().ints(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().ints(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextInt(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextInt(origin, bound))), randomAsserter(SIZE, origin, bound) });
        }
    }
    return data.toArray(new Object[0][]);
}
Also used : IntStream(java.util.stream.IntStream) LongStream(java.util.stream.LongStream) DataProvider(org.testng.annotations.DataProvider) DoubleStreamTestScenario(java.util.stream.DoubleStreamTestScenario) TestData(java.util.stream.TestData) Set(java.util.Set) Test(org.testng.annotations.Test) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) OpTestCase(java.util.stream.OpTestCase) SplittableRandom(java.util.SplittableRandom) StreamSupport(java.util.stream.StreamSupport) Spliterator(java.util.Spliterator) IntStreamTestScenario(java.util.stream.IntStreamTestScenario) LongStreamTestScenario(java.util.stream.LongStreamTestScenario) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SplittableRandom(java.util.SplittableRandom) IntStream(java.util.stream.IntStream) DataProvider(org.testng.annotations.DataProvider)

Example 50 with Function

use of java.util.function.Function in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method doublesDataProvider.

@DataProvider(name = "doubles")
public static Object[][] doublesDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Double>, DoubleStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToDouble(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles().limit(%d)", SIZE), () -> new SplittableRandom().doubles().limit(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d)", SIZE), () -> new SplittableRandom().doubles(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble())", SIZE), () -> rbsf.apply(sr -> sr.nextDouble())), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final double origin = o;
            final double bound = b;
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%f, %f).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().doubles(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d, %f, %f)", SIZE, origin, bound), () -> new SplittableRandom().doubles(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextDouble(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f, %f))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextDouble(origin, bound))), randomAsserter(SIZE, origin, bound) });
        }
    }
    return data.toArray(new Object[0][]);
}
Also used : IntStream(java.util.stream.IntStream) LongStream(java.util.stream.LongStream) DataProvider(org.testng.annotations.DataProvider) DoubleStreamTestScenario(java.util.stream.DoubleStreamTestScenario) TestData(java.util.stream.TestData) Set(java.util.Set) Test(org.testng.annotations.Test) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) OpTestCase(java.util.stream.OpTestCase) SplittableRandom(java.util.SplittableRandom) StreamSupport(java.util.stream.StreamSupport) Spliterator(java.util.Spliterator) IntStreamTestScenario(java.util.stream.IntStreamTestScenario) LongStreamTestScenario(java.util.stream.LongStreamTestScenario) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) SplittableRandom(java.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Aggregations

Function (java.util.function.Function)176 List (java.util.List)75 ArrayList (java.util.ArrayList)55 Map (java.util.Map)51 Test (org.junit.Test)47 IOException (java.io.IOException)44 HashMap (java.util.HashMap)37 Set (java.util.Set)36 Collectors (java.util.stream.Collectors)33 Arrays (java.util.Arrays)30 Collections (java.util.Collections)26 Collection (java.util.Collection)25 File (java.io.File)20 HashSet (java.util.HashSet)19 Supplier (java.util.function.Supplier)19 BiFunction (java.util.function.BiFunction)18 Consumer (java.util.function.Consumer)16 Test (org.testng.annotations.Test)16 Stream (java.util.stream.Stream)14 Assert.assertEquals (org.junit.Assert.assertEquals)13