Search in sources :

Example 41 with Predicate

use of java.util.function.Predicate in project LiveLessons by douglascraigschmidt.

the class TestDataFactory method getSharedInput.

/**
     * Return the input data in the given @a filename as a list of
     * SharedStrings.
     */
public static List<CharSequence> getSharedInput(String filename, String splitter) {
    try {
        // Convert the filename into a pathname.
        URI uri = ClassLoader.getSystemResource(filename).toURI();
        // Open the file and get all the bytes.
        CharSequence bytes = new String(Files.readAllBytes(Paths.get(uri)));
        return // the file into a list of Strings.
        Pattern.compile(splitter).splitAsStream(bytes).filter(((Predicate<String>) String::isEmpty).negate()).map(string -> new SharedString(string.toCharArray())).collect(toList());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Files(java.nio.file.Files) Paths(java.nio.file.Paths) Predicate(java.util.function.Predicate) URI(java.net.URI) Pattern(java.util.regex.Pattern) URI(java.net.URI)

Example 42 with Predicate

use of java.util.function.Predicate in project LiveLessons by douglascraigschmidt.

the class TestDataFactory method getSharedInput.

/**
     * Return the input data in the given @a filename as a list of
     * SharedStrings.
     */
public static List<CharSequence> getSharedInput(String filename, String splitter) {
    try {
        // Convert the filename into a pathname.
        URI uri = ClassLoader.getSystemResource(filename).toURI();
        // Open the file and get all the bytes.
        CharSequence bytes = new String(Files.readAllBytes(Paths.get(uri)));
        return // the file into a list of Strings.
        Pattern.compile(splitter).splitAsStream(bytes).filter(((Predicate<String>) String::isEmpty).negate()).map(string -> new SharedString(string.toCharArray())).collect(toList());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) SharedString(livelessons.utils.SharedString) Files(java.nio.file.Files) Paths(java.nio.file.Paths) Predicate(java.util.function.Predicate) URI(java.net.URI) Pattern(java.util.regex.Pattern) SharedString(livelessons.utils.SharedString) SharedString(livelessons.utils.SharedString) URI(java.net.URI)

Example 43 with Predicate

use of java.util.function.Predicate in project protoman by spotify.

the class SemverSchemaVersioner method hasMinorVersionChange.

/**
 * File additions, removals and renames are considered minor version changes. As are any
 * formatting/comment changes to proto files. I.e. adding a type, adding a field, etc. are
 * considered minor version changes.
 */
private static boolean hasMinorVersionChange(final String protoPackage, final DescriptorSet current, final DescriptorSet candidate) {
    final Predicate<FileDescriptor> predicate = fileDescriptor -> Objects.equals(fileDescriptor.protoPackage(), protoPackage);
    final ImmutableMap<String, FileDescriptor> currentFileDescriptors = current.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    final ImmutableMap<String, FileDescriptor> candidateFileDescriptors = candidate.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    if (!Objects.equals(currentFileDescriptors.keySet(), candidateFileDescriptors.keySet())) {
        // .proto files were added or removed
        return true;
    }
    // descriptors. I.e. formatting changes are usually not considered minor version changes.
    return currentFileDescriptors.keySet().stream().anyMatch(key -> {
        // No files were added or removed, thus currentFd and candidateFd should always be
        // non-null
        final FileDescriptor currentFd = currentFileDescriptors.get(key);
        final FileDescriptor candidateFd = candidateFileDescriptors.get(key);
        assert currentFd != null && candidateFd != null;
        final DescriptorProtos.FileDescriptorProto currentFdProto = currentFd.toProto().toBuilder().clearSourceCodeInfo().build();
        final DescriptorProtos.FileDescriptorProto candidateFdProto = candidateFd.toProto().toBuilder().clearSourceCodeInfo().build();
        return !Objects.equals(currentFdProto, candidateFdProto);
    });
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor) Function(java.util.function.Function) Objects(java.util.Objects) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Matcher(java.util.regex.Matcher) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Nullable(javax.annotation.Nullable) DescriptorProtos(com.google.protobuf.DescriptorProtos) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor)

Example 44 with Predicate

use of java.util.function.Predicate in project protoman by spotify.

the class SemverSchemaVersioner method hasPatchVersionChange.

private static boolean hasPatchVersionChange(final String protoPackage, final DescriptorSet current, final DescriptorSet candidate) {
    final Predicate<FileDescriptor> predicate = fileDescriptor -> Objects.equals(fileDescriptor.protoPackage(), protoPackage);
    final ImmutableMap<String, FileDescriptor> currentFileDescriptors = current.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    final ImmutableMap<String, FileDescriptor> candidateFileDescriptors = candidate.fileDescriptors().stream().filter(predicate).collect(toImmutableMap(FileDescriptor::fullName, Function.identity()));
    return currentFileDescriptors.keySet().stream().anyMatch(key -> {
        final FileDescriptor currentFd = currentFileDescriptors.get(key);
        final FileDescriptor candidateFd = candidateFileDescriptors.get(key);
        assert currentFd != null && candidateFd != null;
        return !Objects.equals(currentFd.toProto(), candidateFd.toProto());
    });
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor) Function(java.util.function.Function) Objects(java.util.Objects) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Matcher(java.util.regex.Matcher) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) DescriptorSet(com.spotify.protoman.descriptor.DescriptorSet) Nullable(javax.annotation.Nullable) FileDescriptor(com.spotify.protoman.descriptor.FileDescriptor)

Example 45 with Predicate

use of java.util.function.Predicate in project JGroups by belaban.

the class MessageBatchTest method testRemoveWithFilter.

public void testRemoveWithFilter() {
    Predicate<Message> filter = msg -> msg != null && msg.isTransientFlagSet(Message.TransientFlag.OOB_DELIVERED);
    MessageBatch batch = new MessageBatch(10);
    for (int i = 1; i <= 10; i++) {
        Message msg = new Message(null, i);
        if (i % 2 == 0)
            msg.setTransientFlag(Message.TransientFlag.OOB_DELIVERED);
        batch.add(msg);
    }
    System.out.println("batch = " + batch);
    assert batch.size() == 10;
    batch.remove(filter);
    System.out.println("batch = " + batch);
    assert batch.size() == 5;
    for (int i = 0; i < 5; i++) batch.add(new Message(null, i).setTransientFlag(Message.TransientFlag.OOB_DELIVERED));
    System.out.println("batch = " + batch);
    batch.replace(filter, null, false);
    assert batch.size() == 9;
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) DataInputStream(java.io.DataInputStream) java.util(java.util) Util(org.jgroups.util.Util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Predicate(java.util.function.Predicate) BiFunction(java.util.function.BiFunction) Test(org.testng.annotations.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Message(org.jgroups.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) Global(org.jgroups.Global) ClassConfigurator(org.jgroups.conf.ClassConfigurator) Address(org.jgroups.Address) org.jgroups.protocols(org.jgroups.protocols) MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Aggregations

Predicate (java.util.function.Predicate)120 List (java.util.List)49 Map (java.util.Map)34 Collectors (java.util.stream.Collectors)31 Test (org.junit.Test)30 IOException (java.io.IOException)28 ArrayList (java.util.ArrayList)26 Arrays (java.util.Arrays)25 Collections (java.util.Collections)23 Set (java.util.Set)15 Function (java.util.function.Function)14 Assert.assertEquals (org.junit.Assert.assertEquals)14 java.util (java.util)13 Optional (java.util.Optional)13 Supplier (java.util.function.Supplier)13 HashMap (java.util.HashMap)12 Before (org.junit.Before)12 Objects (java.util.Objects)11 InputStream (java.io.InputStream)10 AssertExtensions (io.pravega.test.common.AssertExtensions)9