Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary search API #385

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.smallrye.common.function;

import java.util.Objects;

/**
* A function which operates on an object and an integer, yielding an object.
*
* @param <T> the first argument type
* @param <R> the result type
* @param <E> the exception type
*/
public interface ExceptionObjIntFunction<T, R, E extends Exception> {
/**
* Applies this function to the given arguments.
*
* @param arg1 the first argument
* @param arg2 the second argument
* @return the function result
* @throws E if an exception occurs
*/
R apply(T arg1, int arg2) throws E;

/**
* {@return a function that applies the given function to the result of this function}
*
* @param after the next function (must not be {@code null})
* @param <V> the final return type
*/
default <V> ExceptionObjIntFunction<T, V, E> andThen(ExceptionFunction<? super R, ? extends V, ? extends E> after) {
Objects.requireNonNull(after);
return (T t, int u) -> after.apply(apply(t, u));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.smallrye.common.function;

import java.util.Objects;

/**
* A predicate that operates on an object and an integer.
*
* @param <T> the first argument type
* @param <E> the exception type
*/
public interface ExceptionObjIntPredicate<T, E extends Exception> {
/**
* Evaluate this predicate on the given arguments.
*
* @param object the first argument
* @param value the second argument
* @return {@code true} if the predicate passes, {@code false} otherwise
* @throws E if an exception occurs
*/
boolean test(T object, int value) throws E;

/**
* {@return a predicate which is {@code true} only when this and the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ExceptionObjIntPredicate<T, E> and(ExceptionObjIntPredicate<? super T, ? extends E> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) && other.test(t, i);
}

/**
* {@return a predicate which is {@code true} when this predicate is {@code false}, and vice-versa}
*/
default ExceptionObjIntPredicate<T, E> negate() {
return (t, i) -> !test(t, i);
}

/**
* {@return a predicate which is {@code true} when either this or the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ExceptionObjIntPredicate<T, E> or(ExceptionObjIntPredicate<? super T, ? extends E> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) || other.test(t, i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.smallrye.common.function;

import java.util.Objects;

/**
* A function which operates on an object and a long integer, yielding an object.
*
* @param <T> the first argument type
* @param <R> the result type
* @param <E> the exception type
*/
public interface ExceptionObjLongFunction<T, R, E extends Exception> {
/**
* Applies this function to the given arguments.
*
* @param arg1 the first argument
* @param arg2 the second argument
* @return the function result
* @throws E if an exception occurs
*/
R apply(T arg1, long arg2) throws E;

/**
* {@return a function that applies the given function to the result of this function}
*
* @param after the next function (must not be {@code null})
* @param <V> the final return type
*/
default <V> ExceptionObjLongFunction<T, V, E> andThen(ExceptionFunction<? super R, ? extends V, ? extends E> after) {
Objects.requireNonNull(after);
return (T t, long u) -> after.apply(apply(t, u));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.smallrye.common.function;

import java.util.Objects;

/**
* A predicate that operates on an object and a long integer.
*
* @param <T> the first argument type
* @param <E> the exception type
*/
public interface ExceptionObjLongPredicate<T, E extends Exception> {
/**
* Evaluate this predicate on the given arguments.
*
* @param object the first argument
* @param value the second argument
* @return {@code true} if the predicate passes, {@code false} otherwise
* @throws E if an exception occurs
*/
boolean test(T object, long value) throws E;

/**
* {@return a predicate which is {@code true} only when this and the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ExceptionObjLongPredicate<T, E> and(ExceptionObjLongPredicate<? super T, ? extends E> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) && other.test(t, i);
}

/**
* {@return a predicate which is {@code true} when this predicate is {@code false}, and vice-versa}
*/
default ExceptionObjLongPredicate<T, E> negate() {
return (t, i) -> !test(t, i);
}

/**
* {@return a predicate which is {@code true} when either this or the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ExceptionObjLongPredicate<T, E> or(ExceptionObjLongPredicate<? super T, ? extends E> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) || other.test(t, i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.smallrye.common.function;

import java.util.Objects;
import java.util.function.Function;

/**
* A function which operates on an object and an integer, yielding an object.
*
* @param <T> the first argument type
* @param <R> the result type
*/
public interface ObjIntFunction<T, R> {
/**
* Applies this function to the given arguments.
*
* @param arg1 the first argument
* @param arg2 the second argument
* @return the function result
*/
R apply(T arg1, int arg2);

/**
* {@return a function that applies the given function to the result of this function}
*
* @param after the next function (must not be {@code null})
* @param <V> the final return type
*/
default <V> ObjIntFunction<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, int u) -> after.apply(apply(t, u));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.smallrye.common.function;

import java.util.Objects;

/**
* A predicate that operates on an object and an integer.
*
* @param <T> the first argument type
*/
public interface ObjIntPredicate<T> {
/**
* Evaluate this predicate on the given arguments.
*
* @param object the first argument
* @param value the second argument
* @return {@code true} if the predicate passes, {@code false} otherwise
*/
boolean test(T object, int value);

/**
* {@return a predicate which is {@code true} only when this and the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ObjIntPredicate<T> and(ObjIntPredicate<? super T> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) && other.test(t, i);
}

/**
* {@return a predicate which is {@code true} when this predicate is {@code false}, and vice-versa}
*/
default ObjIntPredicate<T> negate() {
return (t, i) -> !test(t, i);
}

/**
* {@return a predicate which is {@code true} when either this or the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ObjIntPredicate<T> or(ObjIntPredicate<? super T> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) || other.test(t, i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.smallrye.common.function;

import java.util.Objects;
import java.util.function.Function;

/**
* A function which operates on an object and a long integer, yielding an object.
*
* @param <T> the first argument type
* @param <R> the result type
*/
public interface ObjLongFunction<T, R> {
/**
* Applies this function to the given arguments.
*
* @param arg1 the first argument
* @param arg2 the second argument
* @return the function result
*/
R apply(T arg1, long arg2);

/**
* {@return a function that applies the given function to the result of this function}
*
* @param after the next function (must not be {@code null})
* @param <V> the final return type
*/
default <V> ObjLongFunction<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, long u) -> after.apply(apply(t, u));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.smallrye.common.function;

import java.util.Objects;

/**
* A predicate that operates on an object and a long integer.
*
* @param <T> the first argument type
*/
public interface ObjLongPredicate<T> {
/**
* Evaluate this predicate on the given arguments.
*
* @param object the first argument
* @param value the second argument
* @return {@code true} if the predicate passes, {@code false} otherwise
*/
boolean test(T object, long value);

/**
* {@return a predicate which is {@code true} only when this and the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ObjLongPredicate<T> and(ObjLongPredicate<? super T> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) && other.test(t, i);
}

/**
* {@return a predicate which is {@code true} when this predicate is {@code false}, and vice-versa}
*/
default ObjLongPredicate<T> negate() {
return (t, i) -> !test(t, i);
}

/**
* {@return a predicate which is {@code true} when either this or the given predicate return {@code true}}
*
* @param other the other predicate (must not be {@code null})
*/
default ObjLongPredicate<T> or(ObjLongPredicate<? super T> other) {
Objects.requireNonNull(other);
return (t, i) -> test(t, i) || other.test(t, i);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<module>os</module>
<module>ref</module>
<module>resource</module>
<module>search</module>
<module>version</module>
<module>vertx-context</module>
</modules>
Expand Down
56 changes: 56 additions & 0 deletions search/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>smallrye-common-search</artifactId>

<name>SmallRye Common: Search</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>smallrye-common-function</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>coverage</id>
<properties>
<argLine>@{jacocoArgLine}</argLine>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>


</project>
Loading