Coder Social home page Coder Social logo

fraunhoferfokus / xtensions Goto Github PK

View Code? Open in Web Editor NEW
3.0 10.0 1.0 10.02 MB

This library provides utility extension methods, mostly for JDK 8 classes.

Home Page: https://fraunhoferfokus.github.io/Xtensions/

License: Eclipse Public License 2.0

Xtend 91.33% Java 8.67%
xtend utility-library

xtensions's People

Contributors

boereck avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

boereck

xtensions's Issues

Add generate and iterate factory methods for LongIterable and DoubleIterable

Provide factory methods generate and iterate analogous to the ones already provided to IntIterable.

For LongIterable:

  • public static LongIterable generate(final LongSupplier s)
  • public static LongIterable iterate(final long seed, final LongUnaryOperator f)
  • public static LongIterable iterate(final long seed, final LongPredicate hasNext, final LongUnaryOperator next)

For DoubleIterable:

  • public static DoubleIterable generate(final DoubleSupplier s)
  • public static DoubleIterable iterate(final double seed, final DoubleUnaryOperator f)
  • public static DoubleIterable iterate(final double seed, final DoublePredicate hasNext, final DoubleUnaryOperator next)

Fixup JavaDocs

  • Add links in Xtensions JavaDocs to Xbase library JavaDocs
  • Fix missing parameter documentation
  • Fix missing generics documentation

Add `combinations` methods for cartesian products

Every now and then it is handy to produce a cartesian product of two iterables and maybe filter them or define how the combination of two elements should be computed in the product.

For Iterables implement the following extension methods:

  • static def <X,Y> Iterable<Pair<X,Y>> combinations(Iterable<X> iterable, Iterable<Y> other)
  • static def <X,Y,Z> Iterable<Z> combinations(Iterable<X> iterable, Iterable<Y> other, (X,Y)=>Z merger)
  • static def <X,Y> Iterable<Pair<X,Y>> combinationsWhere(Iterable<X> iterable, Iterable<Y> other, BiPredicate<X,Y> where)
  • static def <X,Y> Iterable<Z> combinationsWhere(Iterable<X> iterable ,Iterable<Y> other, BiPredicate<X,Y> where,(X,Y)=>Z merger)

For Iterators implement the following extension methods:

  • static def <X,Y> Iterator<Pair<X,Y>> combinations(Iterator<X> iterator, Iterable<Y> other)
  • static def <X,Y,Z> Iterator<Z> combinations(Iterator<X> iterator, Iterable<Y> other,(X,Y)=>Z merger)
  • static def <X,Y> Iterator<Pair<X,Y>> combinationsWhere(Iterator<X> iterator, Iterable<Y> other, BiPredicate<X,Y> where)
  • static def <X,Y> Iterator<Z> combinationsWhere(Iterator<X> iterator, Iterable<Y> other, BiPredicate<X,Y> where,(X,Y)=>Z merger)

Add conditional extension methods with callbacks for Boolean

Add extension methods to Boolean that test it's value for a condition and pass a callback to be executed if condition matches.

  • static def void ifTrue(Boolean b, ()=>void action)
  • static def void ifNullOrTrue(Boolean b, ()=>void action)
  • static def void ifFalse(Boolean b, ()=>void action)
  • static def void ifNullOrFalse(Boolean b, ()=>void action)

Add mapOrGet extension method to Optional

On java.util.Optional the callchain optional.map[...].orElseGet[...] is pretty common, but it is a little long and may perform one unnecessary object allocation during the map call. So instead a mapOrGet method would be beneficial.

Add forEach extension method for object arrays

Add forEach method to iterate over array elements without wrapping it into an Iterable and creating an Iterator, as done currently with the forEach method built into Xtend. This is analogous to the forEach methods for primitive arrays.

Add general extension methods for Objects

Add extension methods that can be applied to objects in general.
Consider the following methods

  • asType to cast to a given type or return null if not possible
  • ifNotNull tests an object on null and if it is not invokes a given consumer with the object
  • recoverNull tests an object on null and if it is null returns a fallback else the object itself. Provide a Java 9 API forward compatible alias named requireNonNullElse​
  • recoverNull tests an object on null and if it is null returns a fallback computed from from a provider else the object itself. Provide a Java 9 API forward compatible alias named requireNonNullElseGet​

Provide a TryIterator<R> class for possibly failed results

Provide a class TryIterator which can iterate over possibly failed results.

It may be created as following extension methods from a regular Iterator<T>:

  • static def <T,Y> TryIterator<Y> tryMap(Iterator<T> context, (T)=>Y mapper)
  • static def <T,Y> TryIterator<Y> tryMapNullable(Iterator<T> context, (T)=>Y mapper)
  • static def <T> TryIterator<T> tryFilter(Iterator<T> context, Predicate<T> predicate)
  • static def <T> TryIterator<T> tryFilterNullable(Iterator<T> context, Predicate<T> predicate)

The following instance methods are expected to be implemented:

  • Methods to filter out empty (null) elements, and failures.
  • Methods to map successful values (and catch exceptions during mapping)
  • Methods to filter out successful values based on predicate (and catch exceptions during predicate evaluation)
  • Methods to filter out successful values based on the class of the value
  • Methods to recover from failures
  • Methods to get a list of all successful values (and specify what happens on failures)
  • Methods to get a set of all successful values (and specify what happens on failures)
  • Methods to collect successful values via a java.util.stream.Collector (and specify what happens on failures)
  • For-each methods to iterate over all values (successful, empty and failed)

Provide partitioning of Iterator/Iterable by class of contained objects

Create a partitioning function, to separate elements provided by an Iterator/Iterable by classes they are instance of. This is beneficial for model-to-model transformations, especially when traversing a model to find instances of class X that are referenced by instances of class Y.
This could be the API of such a method:

static def <T> ClassGrouping groupBy(Iterable<T> iterable, Class<?>... classes)
public interface ClassGrouping {
	def List<Class<?>> getGroupingClasses();
 	def <T> List<T> get(Class<T> clazz) throws NoSuchElementException;
	def Map<Class<?>,List<?>> asMap();
}

Add toUnmodifiableList extension method for Iterable and Iterator

Add toUnmodifiableList method for Iterable and Iterator.

  • static def <T> List<T> toUnmodifiableList(Iterable<T>)
  • static def <T> List<T> toUnmodifiableList(Iterator<T>)

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

Add withoutAll extension method to IteratorExtensions and IterableExtensions

Add the extension methods

  • IteratorExtensions#withoutAll(Iterator<T> self, Iterable<?> other)
  • IterableExtensions#withoutAll(Iteratable<T> self, Iterable<?> other)

This method is basically a shortcut for self.filter[el| !other.exists[el == it]] but being more performant if other is instance of Collection.

Add partitionBy extension method to Iterable and Iterator

Add a partitionBy extension method to Iterable and Iterator which partitions an input into two groups according to a predicate. This should be similar to the version provided by the Java 8 java.util.stream.Collector to feel comfortable to Java users.

  • static def <T> Map<Boolean,List<T>> partitionBy(Iterable<T>, Predicate<T>)
  • static def <T, A, C> Map<Boolean,C> partitionBy(Iterable<T>, Collector<? super T,A,C>, Predicate<T>)
  • static def <T> Map<Boolean,List<T>> partitionBy(Iterator<T>, Predicate<T>)
  • static def <T, A, C> Map<Boolean,C> partitionBy(Iterator<T>, Collector<? super T,A,C>, Predicate<T>)

Think about providing an interface Partitions to get around boxing booleans:

interface Partitions<C> extends Map<Boolean,C> { 
   def C getTrue(); 
   def C getFalse();
}

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

Summary extension methods for PrimitiveIterators

Support some basic aggregation methods for primitive iterators:

  • static def IntSummaryStatistics summarize(PrimitiveIterator.OfInt)
  • static def LongSummaryStatistics summarize(PrimitiveIterator.OfLong)
  • static def DoubleSummaryStatistics summarize(PrimitiveIterator.OfDouble)

Add into(Collection) extension method to Iterator/Iterable

Add a into extension method adding the references stored in on Iterable/provided by an Iterator to a given collection. This allows for more compact code. The equivalent of collection.into(target) currently has to be written as collection.forEach[target.add(it)] which looks a bit more convoluted. If Xtend would support method references this could look something like: this collection.forEach(target#add). This would look a bit better.

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

EDIT: It may also be desirable to have a into(Collection...) method.

Tasks:

  • Implementation
  • JavaDocs
  • Unit Tests
  • Add documentation to gitbook

Updating the Xtext Community Website

Hello Xtensions team,

The Xtext team would like to update the Xtext community website listing all the cool projects people are building around Xtext/Xtend.

See also the corresponding GitHub issue and Bugzilla ticket.

If you are interested that the Xtensions project is listed there, please provide the following information as a comment to this issue:

<tr>
	<td><a href="project_link">project_name</a></td>
	<td>project_description_max_2_sentences</td>
	<td>license</td>
	<td>category</td>
	<td>author(s)</td>
</tr>

We will then update the Xtext community website and get it online as soon as possible.

Thanks!
Tamás

Provide an OptionalBoolean class

Provide an optional variant for boolean, similar to how the JDK provides for other primitives like OptionalInt for the primitive int.

  • Provide methods analogous to the ones provided by Java's built in Optional
    • isPresent
    • ifPresent
    • ifPresentOrElse
    • orElse
    • orElseGet
    • orElseGetThrow
    • empty static factory method
    • ofNullable static factory method
    • ofTrue static factory method
    • ofFalse static factory method
  • Provide methods specific to booleans
    • isTrue
    • ifTrue
    • isTrueOrEmpty
    • ifTrueOrEmpty
    • isFalse
    • ifFalse
    • isFalseOrEmpty
    • ifFalseOrEmpty
  • In class Primitives provide extension method
    • optionalBool to create an OptionalBoolean mapping to a bool and null to an empty optional
  • In class OptionalExtensions create extension methods
    • mapBool similar how it is already done for other other primitive types
    • test checking a value wrapped in an optional for a condition

Also:

  • Update book to include section about OptionalBoolean

Try class encapsulating results that may have failed

Provide a Try class that represents three states of a computation result:

  • Success: A result value was computed successfully and is wrapped in the Try type
  • Empty: A null result resulting of a successful computation
  • Failure: The computation failed with an exception. This exception is wrapped in the resulting variant of Try

Users of that type should be able to

  • create Success/Empty/Failure elements directly
  • perform a computation and based on the outcome (Exception thrown, null returned, value returned) create a Try
  • perform a computation providing an auto-closeable resource and based on the outcome (Exception thrown, null returned, value returned) create a Try. Automatically close the resource on end of method
  • isEmpty, isSuccess, isFailure methods to check for Try variant
  • Extract methods to get successful results and exception for failures
  • Map method for "happy path" coding
  • Recovery from failures and empty results

Add peek extension method for `Iterable` and `Iterator`

Analogous to the JDK method java.lang.Stream#peek(java.util.function.Consumer) introduce a peek extension method for Iterable and Iterator. This method just consumes an element and produces an Iterable/Iterator providing the same elements as the source. This method is mainly interesting for debugging purposes.

  • static def <T> Iterable<T> peek(Iterable<T> iterable, (T)=>void action)
  • static def <T> Iterator<T> peek(Iterator<T> iterator, (T)=>void action)

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

Optimize stream creation from own PrimitiveIterator implementations

To allow better conversion between PrimitiveInterator sub-types and primitive streams, introduce internal interfaces for the primitive types int, long and double providing a stream method for creation of primitive streams. Let all own implementations of primitive iterators implement these interfaces.
Adjust the streamRemaining methods in class PrimitiveIteratorExtensions to check if iterators are instance of these interfaces and create streams from the stream method when available.
This allows building up a stream pipeline entirely from the iterators, allowing the stream implementation (and JVM's JIT) to do it's magic.

zip extension method for Iterator and Iterable

Implement a zip extension method for Iterable and Iterator. This method will produce a list of pairs from both sources by their index. This is known from other languages and frameworks, such as Haskell, Scala, Kotlin, or Eclipse Collections.

  • static def <T,V> Iterable<Pair<T,V>> zip(Iterable<? extends T>, Iterable<? extends V>)
  • static def <T,V,Y> Iterable<Y> zip(Iterable<? extends T>, Iterable<? extends V>, (T,V)=>Y merger)
  • static def <T,V> Iterator<Pair<T,V>> zip(Iterator<? extends T>, Iterator<? extends V>)
  • static def <T,V,Y> Iterator<Y> zip(Iterator<? extends T>, Iterator<? extends V>, (T,V)=>Y merger)

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

Add partitionBy methods by class for Iterable and Iterator

Add a new partitionBy extension method for Iterable and Iterator to partition the elements provided by being instance of a given class or not.

  • static def <T,Y> Pair<List<Y>,List<T>> partitionBy(Iterable<T>, Class<Y>)
  • static def <T,Y,AT,AY,DT,DY> Pair<DY,DT> partitionBy(Iterable<T>, Class<Y>, Collector<? super T, AT, DT>, Collector<? super Y, AY, DY>)
  • static def <T,Y> Pair<List<Y>,List<T>> partitionBy(Iterator<T>, Class<Y>)
  • static def <T,Y,AT,AY,DT,DY> Pair<DY,DT> partitionBy(Iterator<T>, Class<Y>, Collector<? super T, AT, DT>, Collector<? super Y, AY, DY>)

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

Add specialized PrimitiveIterator combinator methods.

Add a couple of extension combinator methods / terminal Methods to the PrimitiveIterator sub-interfaces.
The following methods seem to be the most interesting:

  • static def boolean anyMatch(PrimitiveIterator.OfInt intIterator, IntPredicate test)

  • static def boolean allMatch(PrimitiveIterator.OfInt intIterator, IntPredicate test)

  • static def boolean noneMatch(PrimitiveIterator.OfInt intIterator, IntPredicate test)

  • static def OptionalInt findFirst​(PrimitiveIterator.OfInt intIterator, IntPredicate test)

  • static def OptionalInt findFirst​(PrimitiveIterator.OfInt intIterator)

  • static def PrimitiveIterator.OfInt filter(PrimitiveIterator.OfInt intIterator, IntPredicate test)

  • static def OptionalInt reduce(PrimitiveIterator.OfInt intIterator, IntBinaryOperator op)

  • static def int reduce(PrimitiveIterator.OfInt intIterator, int identity, IntBinaryOperator op)

  • static def boolean anyMatch(PrimitiveIterator.OfLong longIterator, LongPredicate test)

  • static def boolean allMatch(PrimitiveIterator.OfLong longIterator, LongPredicate test)

  • static def boolean noneMatch(PrimitiveIterator.OfLong longIterator, LongPredicate test)

  • static def OptionalLong findFirst​(PrimitiveIterator.OfLong longIterator, LongPredicate test)

  • static def OptionalLong findFirst​(PrimitiveIterator.OfLong longIterator)

  • static def PrimitiveIterator.OfLong filter(PrimitiveIterator.OfLong longIterator, LongPredicate test)

  • static def OptionalLong reduce(PrimitiveIterator.OfLong longIterator, LongBinaryOperator op)

  • static def long reduce(PrimitiveIterator.OfLong longIterator, long identity, LongBinaryOperator op)

  • static def boolean anyMatch(PrimitiveIterator.OfDouble doubleIterator, DoublePredicate test)

  • static def boolean allMatch(PrimitiveIterator.OfDouble doubleIterator, DoublePredicate test)

  • static def boolean noneMatch(PrimitiveIterator.OfDouble doubleIterator, DoublePredicate test)

  • static def OptionalLong findFirst​(PrimitiveIterator.OfDouble doubleIterator, DoublePredicate test)

  • static def OptionalLong findFirst​(PrimitiveIterator.OfDouble doubleIterator)

  • static def PrimitiveIterator.OfDouble filter(PrimitiveIterator.OfDouble doubleIterator, DoublePredicate test)

  • static def OptionalDouble reduce(PrimitiveIterator.OfDouble doubleIterator, DoubleBinaryOperator op)

  • static def double reduce(PrimitiveIterator.OfDouble doubleIterator, double identity, DoubleBinaryOperator op)

If you want this ticket to be implemented, please give feedback, since it is only implemented on demand.

Add support for primitive values in null-safe navigation chains

Add functionality to work with primitive values that are at the end of null-safe navigation chains. This can include the following functionality:

  • Boxing the primitive values, so that a null reference leads to a null boxed primitive
  • Converting primitive values into Optional variants, where a null reference leads to an empty Optional
  • Test functions to check the primitive value adheres to a condition if the reference-chain leading to the value is not null
  • Testing boxed Boolean values for being true/false and being or not being null
  • Allow computation of default values for boxed primitive values that are null

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.