io.vavr.Function1 Java Examples

The following examples show how to use io.vavr.Function1. 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: TagWriteProtocol.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
 * @param name The qualified name of the tag to write, or none() to have the last item of [getters] deliver a {@link QName}.
 * @param getters Getter function for each sub-protocol to write (and additional first element delivering a QName, if name == none())
 * @param protocols Protocols to use to write each of the getter elements
 */
public TagWriteProtocol(Option<QName> name, Vector<? extends WriteProtocol<XMLEvent,?>> protocols, Vector<Function1<T, ?>> g) {
    if (name.isDefined() && (protocols.size() != g.size()) ||
        name.isEmpty() && (protocols.size() != g.size() - 1)) {
        throw new IllegalArgumentException ("Number of protocols and getters does not match");
    }
    this.name = name;
    this.getName = t -> name.getOrElse(() -> (QName) g.head().apply(t));
    
    Vector<Function1<T, ?>> getters = (name.isEmpty()) ? g.drop(1) : g;
    
    Tuple2<Vector<Tuple2<WriteProtocol<XMLEvent,?>, Function1<T, ?>>>, Vector<Tuple2<WriteProtocol<XMLEvent,?>, Function1<T, ?>>>> partition =
        ((Vector<WriteProtocol<XMLEvent,?>>)protocols).zip(getters)
        .partition(t -> Attribute.class.isAssignableFrom(t._1.getEventType()));
    
    this.attrProtocols = partition._1().map(t -> t._1());
    this.attrGetters = partition._1().map(t -> t._2());
    this.otherProtocols = partition._2().map(t -> t._1());
    this.otherGetters = partition._2().map(t -> t._2());
}
 
Example #2
Source File: ReadProtocol.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
 * Maps the protocol into a different type, invoking [onRead] after reading.
 */
public default <U> ReadProtocol<E,U> map(Function1<T,U> onRead) {
    final ReadProtocol<E,T> parent = this;
    
    return new ReadProtocol<E,U>() {
        @Override
        public Reader<E, U> reader() {
            return parent.reader().map(onRead);
        }
        
        @Override
        public Try<U> empty() {
            return parent.empty().map(onRead);
        }
    };
}
 
Example #3
Source File: StringProtocol.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
 * Returns an XMLProtocol that only reads strings that match the given regular expression.
 * During writing, the given function is called. It's the callers responsibility to ensure that
 * the result of the function matches the regex.
 */
public <T> Protocol<E,T> matching(Regex<T> regex, Function1<T,String> onWrite) {
    StringProtocol<E> parent = this;
    return new Protocol<E,T>() {
        @Override
        public Reader<E,T> reader() {
            return parent.reader().flatMap(s -> regex.match(s).toTry().orElse(() -> Try.failure(new ValidationException(parent.toString() + " should match " + regex))));
        }

        @Override
        public Writer<E,T> writer() {
            return parent.writer().compose(onWrite);
        }
        
        @Override
        public Class<? extends E> getEventType() {
            return parent.getEventType();
        }
        
        @Override
        public String toString() {
            return parent.toString() + " matching " + regex;
        }
    };
}
 
Example #4
Source File: Protocol.java    From ts-reaktive with MIT License 6 votes vote down vote up
/**
 * Maps the protocol into a different type, invoking [onRead] after reading and [beforeWrite] before writing.
 */
public default <U> Protocol<E,U> map(Function1<T,U> onRead, Function1<U,T> beforeWrite) {
    Protocol<E,T> parent = this;
    return new Protocol<E,U>() {
        @Override
        public Reader<E,U> reader() {
            return parent.reader().map(onRead);
        }

        @Override
        public Writer<E,U> writer() {
            return parent.writer().compose(beforeWrite);
        }
        
        @Override
        public Try<U> empty() {
            return parent.empty().map(onRead);
        }
        
        @Override
        public Class<? extends E> getEventType() {
            return parent.getEventType();
        }
    };
}
 
Example #5
Source File: ACL.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Creates a new, empty, ACL, based on various lambda expressions for R and C.
 * @param getTargetId Function that yields a target UUID for a change (or none() if that change is to be ignored)
 * @param getGranted Function that yields a right that was granted for a change (or none() if the change doesn't grant rights)
 * @param getRevoked Function that yields a right that was revoked for a change (or none() if the change doesn't revoke rights)
 */
public static <R,C> ACL<R,C> empty(
    Function1<C, Option<UUID>> getTargetId,
    Function1<C, Option<R>> getGranted,
    Function1<C, Option<R>> getRevoked
) {
    return ACLBuilder.of(getTargetId, getGranted, getRevoked).empty();
}
 
Example #6
Source File: ObjectProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
public ObjectProtocol(
    List<Protocol<JSONEvent, ?>> protocols,
    Function<List<?>, T> produce,
    List<Function1<T, ?>> getters
) {
    this.read = new ObjectReadProtocol<>(Vector.ofAll(protocols), produce, Vector.empty());
    this.write = new ObjectWriteProtocol<>(Vector.ofAll(protocols), getters, Vector.empty());
}
 
Example #7
Source File: ObjectWriteProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
ObjectWriteProtocol(
    Seq<WriteProtocol<JSONEvent, ?>> protocols,
    List<Function1<T, ?>> getters,
    Seq<WriteProtocol<JSONEvent, ConstantProtocol.Present>> conditions
) {
    this.protocols = protocols;
    this.getters = getters;
    this.conditions = conditions;
    if (protocols.size() != getters.size()) {
        throw new IllegalArgumentException("protocols must match getters");
    }
}
 
Example #8
Source File: TagWriteProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
private TagWriteProtocol(Option<QName> name, Function<T, QName> getName, Seq<WriteProtocol<XMLEvent,?>> attrProtocols,
    Seq<WriteProtocol<XMLEvent,?>> otherProtocols, Seq<Function1<T, ?>> attrGetters, Seq<Function1<T, ?>> otherGetters) {
    this.name = name;
    this.getName = getName;
    this.attrProtocols = attrProtocols;
    this.otherProtocols = otherProtocols;
    this.attrGetters = attrGetters;
    this.otherGetters = otherGetters;
}
 
Example #9
Source File: Writer.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Invokes the given function before calling this writer.
 */
public default <U> Writer<E,U> compose(Function1<U,T> f) {
    Writer<E,T> parent = this;
    return new Writer<E,U>() {
        @Override
        public Seq<E> apply(U value) {
            return parent.apply(f.apply(value));
        }
        
        @Override
        public Seq<E> reset() {
            return parent.reset();
        }
    };
}
 
Example #10
Source File: Reader.java    From ts-reaktive with MIT License 5 votes vote down vote up
public default <U> Reader<E,U> flatMap(Function1<T,Try<U>> f) {
    Reader<E,T> parent = this;
    return new Reader<E,U>() {
        @Override
        public Try<U> reset() {
            return parent.reset().flatMap(f);
        }

        @Override
        public Try<U> apply(E evt) {
            return parent.apply(evt).flatMap(f);
        }
    };
}
 
Example #11
Source File: GroupedUserACL.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Creates a new, empty, ACL, based on various lambda expressions for R and C.
 * @param rightsType The type of enum for which this ACL checks access rights
 * @param getUserId Function that yields a target user UUID for a change (or none() if that change doesn't target a user)
 * @param getUserGroupId Function that yields a target user group UUID for a change (or none() if that change doesn't target a user group)
 * @param getGranted Function that yields a right that was granted for a change (or none() if the change doesn't grant rights)
 * @param getRevoked Function that yields a right that was revoked for a change (or none() if the change doesn't revoke rights)
 */
public static <R extends Enum<R>,C> GroupedUserACL<R,C> empty(
    Class<R> rightsType,
    Function1<C, Option<UUID>> getUserId,
    Function1<C, Option<UUID>> getUserGroupId,
    Function1<C, Option<R>> getGranted,
    Function1<C, Option<R>> getRevoked
) {
    return new GroupedUserACL<>(rightsType, ACLBuilder.of(getUserId, getGranted, getRevoked).empty(), ACLBuilder.of(getUserGroupId, getGranted, getRevoked).empty());
}
 
Example #12
Source File: CsvProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns a read/write protocol for a CSV format consisting of several nested protocols (probably created
 * using {@link #column(String)} ), invoking the [produce] function on reading, and using the
 * specified getters for writing.
 */
public static <T,F1,F2> Protocol<CsvEvent, T> csv(
    Protocol<CsvEvent,F1> p1, Protocol<CsvEvent,F2> p2,
    Function2<F1,F2,T> produce,
    Function1<T,F1> g1, Function1<T,F2> g2) {
    return multi(Vector.of(p1, p2), args -> produce.apply((F1) args.get(0), (F2) args.get(1)), Vector.of(g1, g2));
}
 
Example #13
Source File: WriteProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Maps the protocol into a different type, invoking [beforeWrite] before writing.
 */
public default <U> WriteProtocol<E,U> compose(Function1<U,T> beforeWrite) {
    WriteProtocol<E,T> parent = this;
    return new WriteProtocol<E,U>() {
        @Override
        public Writer<E,U> writer() {
            return parent.writer().compose(beforeWrite);
        }
        
        @Override
        public Class<? extends E> getEventType() {
            return parent.getEventType();
        }
    };
}
 
Example #14
Source File: CsvProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns a read/write protocol for a CSV format consisting of several nested protocols (probably created
 * using {@link #column(String)} ), invoking the [produce] function on reading, and using the
 * specified getters for writing.
 */
public static <T,F1,F2,F3> Protocol<CsvEvent, T> csv(
    Protocol<CsvEvent,F1> p1, Protocol<CsvEvent,F2> p2, Protocol<CsvEvent,F3> p3,
    Function3<F1,F2,F3,T> produce,
    Function1<T,F1> g1, Function1<T,F2> g2, Function1<T,F3> g3) {
    return multi(Vector.of(p1, p2, p3), args -> produce.apply((F1) args.get(0), (F2) args.get(1), (F3) args.get(2)), Vector.of(g1, g2, g3));
}
 
Example #15
Source File: CsvProtocol.java    From ts-reaktive with MIT License 5 votes vote down vote up
/**
 * Returns a read/write protocol for a CSV format consisting of several nested protocols (probably created
 * using {@link #column(String)} ), using the
 * specified getters for writing.
 */
public static <T,F1,F2,F3> WriteProtocol<CsvEvent, T> csv(
    Function1<T,F1> g1, Protocol<CsvEvent,F1> p1,
    Function1<T,F2> g2, Protocol<CsvEvent,F2> p2,
    Function1<T,F3> g3, Protocol<CsvEvent,F3> p3) {
    return multi(Vector.of(p1, p2, p3), Vector.of(g1, g2, g3));
}
 
Example #16
Source File: Reader.java    From ts-reaktive with MIT License 5 votes vote down vote up
public default <U> Reader<E,U> map(Function1<T,U> f) {
    Reader<E,T> parent = this;
    return new Reader<E,U>() {
        @Override
        public Try<U> reset() {
            return parent.reset().mapTry(f::apply);
        }

        @Override
        public Try<U> apply(E evt) {
            return parent.apply(evt).mapTry(f::apply);
        }
    };
}
 
Example #17
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F1,F2,F3,F4,T> TagProtocol<T> tag(QName qname, Protocol<XMLEvent,F1> p1, Protocol<XMLEvent,F2> p2, Protocol<XMLEvent,F3> p3, Protocol<XMLEvent,F4> p4, Function4<F1,F2,F3,F4,T> f, Function1<T,F1> g1, Function1<T,F2> g2, Function1<T,F3> g3, Function1<T,F4> g4) {
    return new TagProtocol<>(Option.of(qname), Vector.of(p1, p2, p3, p4), args -> f.apply((F1) args.get(0), (F2) args.get(1), (F3) args.get(2), (F4) args.get(3)), Vector.of(g1, g2, g3, g4));
}
 
Example #18
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag with any name and inner protocols using [p*], getting values using [g*] for writing.
 */
public static <F2,F3,T> TagWriteProtocol<T> tagName(Function1<T,QName> g1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2, Function1<T,F3> g3, WriteProtocol<XMLEvent,F3> p3) {
    return new TagWriteProtocol<>(Option.none(), Vector.of(p2, p3), Vector.of(g1, g2, g3));
}
 
Example #19
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag with any name and inner protocols using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F2,F3,T> TagProtocol<T> tagName(Protocol<XMLEvent,F2> p2, Protocol<XMLEvent,F3> p3, Function3<QName,F2,F3,T> f, Function1<T,QName> g1, Function1<T,F2> g2, Function1<T,F3> g3) {
    return new TagProtocol<>(Option.none(), Vector.of(p2, p3), args -> f.apply((QName) args.get(0), (F2) args.get(1), (F3) args.get(2)), Vector.of(g1, g2, g3));
}
 
Example #20
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads a tag with any name, creating its result using [f].
 */
public static <T> TagReadProtocol<T> readTagName(Function1<QName,T> f) {
    return new TagReadProtocol<>(Option.none(), Vector.empty(), args -> f.apply((QName) args.get(0)));
}
 
Example #21
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag with any name and inner protocols using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F2,T> TagProtocol<T> tagName(Protocol<XMLEvent,F2> p2, Function2<QName,F2,T> f, Function1<T,QName> g1, Function1<T,F2> g2) {
    return new TagProtocol<>(Option.none(), Vector.of(p2), args -> f.apply((QName) args.get(0), (F2) args.get(1)), Vector.of(g1, g2));
}
 
Example #22
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and with any name, getting values using [g1] for writing.
 */
public static <T> TagWriteProtocol<T> writeTagName(Function1<T,QName> g1) {
    return new TagWriteProtocol<>(Option.none(), Vector.empty(), Vector.of(g1));
}
 
Example #23
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag with any name and inner protocols using [p*], getting values using [g*] for writing.
 */
public static <F2,T> TagWriteProtocol<T> tagName(Function1<T,QName> g1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2) {
    return new TagWriteProtocol<>(Option.none(), Vector.of(p2), Vector.of(g1, g2));
}
 
Example #24
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and child elements (tag or attribute) using [p*], getting values using [g*] for writing.
 */
public static <F1,F2,F3,F4,T> TagWriteProtocol<T> tag(QName qname, Function1<T,F1> g1, WriteProtocol<XMLEvent,F1> p1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2, Function1<T,F3> g3, WriteProtocol<XMLEvent,F3> p3, Function1<T,F4> g4, WriteProtocol<XMLEvent,F4> p4) {
    return new TagWriteProtocol<>(Option.of(qname), Vector.of(p1, p2, p3, p4), Vector.of(g1, g2, g3, g4));
}
 
Example #25
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and one child element (tag or attribute) using [p1], getting values using [g1] for writing.
 */
public static <F1,T> TagWriteProtocol<T> tag(QName qname, Function1<T,F1> g1, WriteProtocol<XMLEvent,F1> p1) {
    return new TagWriteProtocol<>(Option.of(qname), Vector.of(p1), Vector.of(g1));
}
 
Example #26
Source File: VavrUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenVavrFunction_whenWorks_thenCorrect() {
    Function1<Integer, Integer> square = (num) -> num * num;
    Integer result = square.apply(2);
    assertEquals(Integer.valueOf(4), result);
}
 
Example #27
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and child elements (tag or attribute) using [p*], getting values using [g*] for writing.
 */
public static <F1,F2,F3,T> TagWriteProtocol<T> tag(QName qname, Function1<T,F1> g1, WriteProtocol<XMLEvent,F1> p1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2, Function1<T,F3> g3, WriteProtocol<XMLEvent,F3> p3) {
    return new TagWriteProtocol<>(Option.of(qname), Vector.of(p1, p2, p3), Vector.of(g1, g2, g3));
}
 
Example #28
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F1,F2,F3,T> TagProtocol<T> tag(QName qname, Protocol<XMLEvent,F1> p1, Protocol<XMLEvent,F2> p2, Protocol<XMLEvent,F3> p3, Function3<F1,F2,F3,T> f, Function1<T,F1> g1, Function1<T,F2> g2, Function1<T,F3> g3) {
    return new TagProtocol<>(Option.of(qname), Vector.of(p1, p2, p3), args -> f.apply((F1) args.get(0), (F2) args.get(1), (F3) args.get(2)), Vector.of(g1, g2, g3));
}
 
Example #29
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Writes a tag and child elements (tag or attribute) using [p*], getting values using [g*] for writing.
 */
public static <F1,F2,T> TagWriteProtocol<T> tag(QName qname, Function1<T,F1> g1, WriteProtocol<XMLEvent,F1> p1, Function1<T,F2> g2, WriteProtocol<XMLEvent,F2> p2) {
    return new TagWriteProtocol<>(Option.of(qname), Vector.of(p1, p2), Vector.of(g1, g2));
}
 
Example #30
Source File: XMLProtocol.java    From ts-reaktive with MIT License 4 votes vote down vote up
/**
 * Reads and writes a tag and child elements (tag or attribute) using [p*], using [f] to create the result on reading, getting values using [g*] for writing.
 */
public static <F1,F2,T> TagProtocol<T> tag(QName qname, Protocol<XMLEvent,F1> p1, Protocol<XMLEvent,F2> p2, Function2<F1,F2,T> f, Function1<T,F1> g1, Function1<T,F2> g2) {
    return new TagProtocol<>(Option.of(qname), Vector.of(p1, p2), args -> f.apply((F1) args.get(0), (F2) args.get(1)), Vector.of(g1, g2));
}