Git Product home page Git Product logo

streamsupport's Introduction

Total alerts Language grade: Java Maven Central javadoc.io

streamsupport

streamsupport is a backport of the Java 8 java.util.function (functional interfaces) and java.util.stream (streams) API for Android and users of Java 6 or 7 supplemented with selected additions from java.util.concurrent which didn't exist back in Java 6.

Due to the lack of default interface methods and static interface methods in pre-Java 8 the API had to be slightly adjusted in these areas but still covers the full functionality scope of Java 8. In detail, static and default interface methods have been moved to companion classes in the same package that bear the identical name as the interface but with an "s" appended (e.g. Comparator -> Comparators).

For ease of use, the default methods for most of the functional interfaces were NOT retained as abstract methods in the redefined interfaces (keeping them single method interfaces) - the missing default (and static) methods can always be found in the corresponding companion class.

The streamsupport API lives in the packages java8.util.* and java8.lang respectively. So, it's not possible to simply import the java.util.stream package in your code - you'd rather have to use java8.util.stream instead (see Readme.txt for details). While that is fine as long as you have full control over your source code there is the other common scenario of using a binary 3rd party dependency that has been compiled against the standard Java 8 java.util.stream API. In the latter case bytecode rewriting via ProGuard might be an option. ProGuard supports most Java 8 language features and the latest release can also replace the standard Java 8 stream API by the the streamsupport backport (cf. the Proguard documentation, especially the section titled "Java 8 stream API support").

The current stable release of streamsupport is streamsupport-1.7.4.

Want also lambdas? https://github.com/orfjackal/retrolambda

Note that the streamsupport sourceforge site has been discontinued. New developments, if any, will take place here.

Please give feedback here if you experience any problems.

Features

  • Java 8 / Java 9 Streams library backport
  • Java 8 / Java 9 CompletableFuture backport
  • Java 8 Parallel array operations backport
  • Java 8 Functional interfaces backport
  • Further java.util.concurrent enhancements from Java 7/8 backported to Java 6
  • Includes miscellaneous Java 8 / Java 9 goodies (Optional, StringJoiner, ...)
  • Supports Android - all versions, starting from Ice Cream Sandwich

build.gradle:

dependencies {
    implementation 'net.sourceforge.streamsupport:streamsupport:1.7.4'
}

Maven:

<dependency>
    <groupId>net.sourceforge.streamsupport</groupId>
    <artifactId>streamsupport</artifactId>
    <version>1.7.4</version>
</dependency>

All-in-One JAR:

Contains streamsupport core + atomic + cfuture + flow

<dependency>
    <groupId>net.sourceforge.streamsupport</groupId>
    <artifactId>streamsupport_all</artifactId>
    <version>1.7.4</version>
</dependency>

Example usage

import java.util.List;
import java8.util.stream.IntStreams;
import java8.util.stream.StreamSupport;
import static java8.util.stream.Collectors.toList;

List<Integer> list = IntStreams.of(1, 2, 3, 4).boxed()
        .collect(toList());

List<Integer> incremented = StreamSupport.stream(list)
        .map(i -> i + 1)
        .collect(toList());

Forks

Android developers using the Android Studio 3.x toolchain should have a look at the streamsupport forks:

Release Notes

Release Notes

LICENSE

GNU General Public License, version 2, with the Classpath Exception (and CC0 1.0 for JSR-166 derived code)

streamsupport's People

Contributors

retrostreams avatar stefan-zobel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

streamsupport's Issues

Provide an uberjar as an automatic module without split packages

Is it possible to merge streamsupport-cfuture into the main streamsupport jar and publish it on maven repository?
We need to use streamsupport including CompletableFuture in modules that could compile both for java 8 and java 9 environments.
Currently it's ok in java8 but isn't possible in java 9 environment since streamsupport and streamsupport.cfuture automatic modules have split package (they both have java8.util.concurrent package).

Add automatic module name

For better compatibility with JPMS and to prevent warnign like

"Required filename-based automodules detected: [streamsupport-1.7.2.jar]. Please don't publish this project to a public artifact repository!"

SUGGESTION: example of usage

Hi.

I am not a 100% clear how this is to be used. Once I add the dependency am I to code against your API instead of native java? if so can you give an example of what that would look like?
typical examples such as

 java8.util.J8Arrays.stream

License

Is there any chance to release this library under more liberal license (e.g. LGPL v3)?
It's almost impossible to include it to most commercial products.

Some use of reflection will not work anymore in Android P

Android P reports various warnings when using the streamsupport library:

zygote : Accessing hidden field Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe; (light greylist, reflection)
zygote : Accessing hidden field Ljava/lang/Integer;->value:I (light greylist, reflection)
zygote : Accessing hidden field Ljava/lang/Thread;->contextClassLoader:Ljava/lang/ClassLoader; (dark greylist, reflection)
zygote : Accessing hidden field Ljava/util/concurrent/atomic/AtomicInteger;->value:I (dark greylist, reflection)

The problematic classes are TLRandom which accesses the contextClassLoader and Striped64 which accesses a private field in the AtomicInteger class. There might be other classes as well as our test cases do not cover everything of the streamsupport library.

Basically, warnings wrt light greylist are merely a warning for now, but might be restricted in later versions. Warnings wrt dark greylist will most likely be restricted in Android P and will not work anymore.

See also https://developer.android.com/preview/restrictions-non-sdk-interfaces.html for a more detailed explanation.

Crash with IBM J9 1.6

Hi,

Crash with IBM J9 1.6. There is no such field "size" for ArrayList.
The exception:
Caused by: java.lang.NoSuchFieldException: size
at java.lang.Class.getDeclaredFieldImpl(Native Method)
at java.lang.Class.getDeclaredField(Class.java:510)
at java8.util.ArrayListSpliterator.(ArrayListSpliterator.java:199)
... 41 more

In the meantime I fixed by removing the specifc Unsafe and calling "size()".

Apply classpath exception to source files

Hi, thanks for maintaining this useful library!

You're publishing this library under "GPL + Classpath Exception" (as discussed in #3 and #4). The license text is as follows:

"CLASSPATH" EXCEPTION TO THE GPL

Certain source files distributed by Oracle America and/or its affiliates are
subject to the following clarification and special exception to the GPL, but
only where Oracle has expressly included in the particular source file's header
the words "Oracle designates this particular file as subject to the "Classpath"
exception as provided by Oracle in the LICENSE file that accompanied this code."

    Linking this library statically or dynamically with other modules is making
    a combined work based on this library.  Thus, the terms and conditions of
    the GNU General Public License cover the whole combination.

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent modules,
    and to copy and distribute the resulting executable under terms of your
    choice, provided that you also meet, for each linked independent module,
    the terms and conditions of the license of that module.  An independent
    module is a module which is not derived from or based on this library.  If
    you modify this library, you may extend this exception to your version of
    the library, but you are not obligated to do so.  If you do not wish to do
    so, delete this exception statement from your version.

This leads to a few questions:

  • If this library was based on code written by Oracle or its affiliates, were the corresponding files marked as subject to the classpath exception? If yes, could this header be re-added to all files involved?
  • Would you qualify as an affiliate of Oracle? If not, could the text be adjusted to include "maintainers of the streamsupport library at <github URL>"?

I only looked at the cfuture library (https://github.com/stefan-zobel/streamsupport/tree/master/src/cfuture). In that library, there is a package-info.java file which contains the license header and the classpath exception, however the exception explicitly mentions "Oracle designates this particular file as subject to the "Classpath" exception as provided by Oracle in the LICENSE file that accompanied this code". I'm not a lawyer, but based on this license text, it seems that the code is not effectively licensed under the classpath exception, only under the GPLv2.

Since that does not seem to match the intention of the developers (@stefan-zobel), the header (including the exception) should probably be added to all source files.

This is not a bug issue!

Hi,
I am curious about your code implementation. I notice that the parameter type of the constructor of class "java8.util.DelegatingSpliterator" is "java.util.Spliterator" which is only supported by jdk8+.
Why I can use this when my project is compiled by jdk6 or jdk7?

Also, when I de-compile file "streamsupport-1.6.1.jar" you release, we find there are no java.util.Spliterator.class file in this jar file, so I am quite interested in the principle how program run this codes when it is on jdk7 vm?

waiting for your explanation. thanks very much.

best regards,

Dongjie He

Test Smell: it is not a good practice to introduce nondeterministic randomness in test code

Hi!

We notice that the random number generator (RNG) is used to produce test code in your project.
For example, a random generator in the test method named ''testSort() '' in ''DoublePrimitiveOpsTests.java''
image

But generating random data in test code is not a good test practice. Since Using non-deterministic randomized data may lead to unexpected unit test failure and once the test case fails, it is hard to trace what test data causes it.

Solution:
There are two solutions:

set a random seed number for your random generator
using a hash method to generate random data

How to handle ConcurrentHashMap.keySet() signature change of Java 8?

I have a piece of code written for Java 8 that uses java.util.concurrent.ConcurrentHashMap and its keySet() method. I could use retrolambda to downgrade the class files to the Java-7 byte code successfully.

But at run time, I get:
java.lang.NoSuchMethodError: java.util.concurrent.ConcurrentHashMap.keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;

I think this is because the keySet() method of Java 7 returns the interface Set<K> while the same method returns the inner class ConcurrentHashMap.KeySetView<K,V>, which implements Set. Because the code is compiled with JDK 8, it uses the method signature that returns KeySetView<K,V>. But at run time, the Java 7 JDK library only has the keySet() that returns Set<K>, and this code results in the NoSuchMethodError.

I see StreamSupport has CHM.java. The name sounds like from ConcurrentHashMap but it's under the alternative directory. Does this mean it's an Android variant of ConcurrentHashMap and not a direct replacement?

Do you have any suggestion on how to handle this situation?

Can not use stream.of API

i didn't find a way to use the original Stream.of(T) API. Is there a way to use it in streamsupport?

Question: using with 3rd party libraries that expose CompletableFuture<T> in their API

Hi,

Here is my scenario.

Library M (my plain vanilla Java code) depends on JAR N (3rd party open source JAR from e.g., maven). I have Project A which is an Android app that has to support some old Android API versions; and Project B which is a Java 8 server backend deployed to infrastructure which I control. Both depend on Library M.

JAR N exports the following type:

import java.util.concurrent.CompletableFuture;

public interface LibraryN {
CompletableFuture<String> getTheString();
}

Can you confirm that this streamsupport project does not help me build my Android app with its dependency on LibraryN, because one of my dependencies (specifically Library N) compiles against java.util.concurrent.* and that it is not possible for me to 'replace' the java.util.concurrent.* reference with a reference to the classes provided by this project?

If not, it seems worth adding a caveat to the readme along the lines of "This library cannot help you if one of your project's dependencies depends on Java 8". Maybe this is obvious and maybe I'm kinda dumb, but I can't imagine I'm the only one who will ever make this mistake.

Otherwise—is there a way I can make my scenario work?

Thank you for your time and consideration.

Inclusion of streamsupport:android-retrofuture causes nnio not to include in final apk

`import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

apply plugin: 'com.android.application'
apply plugin: 'org.anarres.jarjar'
apply plugin: "com.github.johnrengelman.shadow"

android {
compileSdkVersion 28
buildToolsVersion "29.0.3"

defaultConfig {
    applicationId "org.geysermc.app.android"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 100007 // Format 00.00.000    EG 1.0.1 -> 01.00.001 -> 0100001 -> 100001
    versionName "1.0.7-BETA"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def newName = outputFileName

            newName = newName.replace("app-", "${variant.mergedFlavor.applicationId}-")
            newName = newName.replace("-release", "-${variant.versionName}")
            newName = newName.replace("-debug", "-${variant.versionName}-debug")

            outputFileName = newName
        }
    }
}

compileOptions {
    coreLibraryDesugaringEnabled true

    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
    exclude 'META-INF/INDEX.LIST'
    exclude 'META-INF/io.netty.versions.properties'
}

configurations {
    nnio
    streamsupport
}

}

repositories {
maven {
url = uri('https://repo.nukkitx.com/maven-releases/')
}

maven {
    url = uri('https://repo.nukkitx.com/maven-snapshots/')
}

maven {
    url = uri('https://jitpack.io')
}

maven {
    url = uri('https://oss.sonatype.org/content/repositories/snapshots/')
}

mavenCentral()

}

task nnioJar (type: ShadowJar) {
relocate 'org.lukhnos.nnio.file', 'java.nio.file'
relocate 'org.lukhnos.nnio.channels', 'java.nio.channels'

configurations = [project.configurations.nnio]

}

task streamsupportJar (type: ShadowJar) {
relocate 'java9.util.concurrent', 'java.util.concurrent'

configurations == [project.configurations.streamsupport]

}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'androidx.navigation:navigation-ui:2.3.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.preference:preference:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'

compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'

implementation "com.fasterxml.jackson.core:jackson-databind:2.9.8"

// Remove epoll, kqueue and fastutil from the protocol
implementation ("com.nukkitx.protocol:bedrock-v408:2.6.0-SNAPSHOT") {
    exclude group: 'com.nukkitx.fastutil', module:'fastutil-common'
    exclude group: 'com.nukkitx.network', module:'common'
    exclude group: 'io.netty', module:'netty-transport-native-epoll'
    exclude group: 'io.netty', module:'netty-transport-native-kqueue'
}

// Include the updated common with the android fixes
implementation 'com.nukkitx.network:common:1.6.20'

// Implement the cleaned fastutil manually to prevent the error on release builds
implementation files("${buildDir}/jarjar/fastutil-cleaned.jar") { builtBy 'jarjar-repackage_fastutil-cleaned.jar' }

// Remove the duplicate classes and include fastutil-common
compileOnly jarjar.repackage('fastutil-cleaned.jar') {
    from 'com.nukkitx.fastutil:fastutil-common:8.3.1'

    classDelete "it.unimi.dsi.fastutil.ints.IntIterator"
    classDelete "it.unimi.dsi.fastutil.longs.LongIterator"
    classDelete "it.unimi.dsi.fastutil.objects.ObjectIterator"
    classDelete "it.unimi.dsi.fastutil.booleans.BooleanIterator"
    classDelete "it.unimi.dsi.fastutil.bytes.ByteIterator"
    classDelete "it.unimi.dsi.fastutil.doubles.DoubleIterator"
    classDelete "it.unimi.dsi.fastutil.floats.FloatIterator"
}

// Core Geyser
implementation ('org.geysermc:connector:1.1.0') {
    exclude group: 'com.nukkitx.fastutil', module:'fastutil-common'
}

// Load a remake of nnio to support earlier Android versions
nnio 'com.github.rtm516:nnio:c7b291f4ca'
implementation tasks.nnioJar.outputs.files

// Load streamsupport to support earlier Android versions
streamsupport 'net.sourceforge.streamsupport:android-retrofuture:1.7.2'
implementation tasks.streamsupportJar.outputs.files

// slf4j port for Android
implementation 'org.slf4j:slf4j-android:1.7.30'

}`

When I include stream support nnio just stops packaging inside the apk, if I remove the relevant stream support lines it works again. No errors in gradle log

Task :app:dexBuilderDebug failed cause of YandexMetrica

I am using MQTT in my project and I can't build the app cause of

Task :app:dexBuilderDebug
AGPBI: {"kind":"error","text":"com.android.tools.r8.internal.r8: Class content provided for type descriptor com.yandex.metrica.impl.ob.j actually defines class com.yandex.metrica.impl.ob.J","sources":[{"file":"D:\\ThisProgect\\app\\build\\intermediates\\transforms\\androidRetroFix\\debug\\67.jar"}],"tool":"D8"}
AGPBI: {"kind":"error","text":"com.android.tools.r8.internal.r8: Class content provided for type descriptor com.yandex.metrica.impl.ob.lf actually defines class com.yandex.metrica.impl.ob.Lf","sources":[{"file":"D:\\ThisProgect\\app\\build\\intermediates\\transforms\\androidRetroFix\\debug\\67.jar"}],"tool":"D8"}
AGPBI: {"kind":"error","text":"com.android.tools.r8.internal.r8: Class content provided for type descriptor com.yandex.metrica.impl.ob.fk actually defines class com.yandex.metrica.impl.ob.Fk","sources":[{"file":"D:\\ThisProgect\\app\\build\\intermediates\\transforms\\androidRetroFix\\debug\\67.jar"}],"tool":"D8"}
AGPBI: {"kind":"error","text":"com.android.tools.r8.internal.r8: Class content provided for type descriptor com.yandex.metrica.impl.ob.d3 actually defines class com.yandex.metrica.impl.ob.D3","sources":[{"file":"D:\\ThisProgect\\app\\build\\intermediates\\transforms\\androidRetroFix\\debug\\67.jar"}],"tool":"D8"}
AGPBI: {"kind":"error","text":"com.android.tools.r8.internal.r8: Class content provided for type descriptor com.yandex.metrica.impl.ob.Xf actually defines class com.yandex.metrica.impl.ob.xf","sources":[{"file":"D:\\ThisProgect\\app\\build\\intermediates\\transforms\\androidRetroFix\\debug\\67.jar"}],"tool":"D8"}

full
yandexIssue.txt

Gradle files are configured as described in docs

my build.gradle, i'm using gradle-7.0.2 (tried with java 1.8 and 11):

buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'gradle.plugin.com.github.sgtsilvio.gradle:android-retrofix:0.4.1'
    }
}

apply plugin: 'com.github.sgtsilvio.gradle.android-retrofix'

android {
    compileSdkVersion 31
    buildToolsVersion '31.0.0'

    configurations {
        all {
            exclude module: 'commons-logging'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
//        sourceCompatibility JavaVersion.VERSION_1_8
//        targetCompatibility JavaVersion.VERSION_1_8
    }

   defaultConfig {
        applicationId "com.thisproject.project"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 296
        versionName '6.2.2'
        multiDexEnabled true
        ndk.abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
    }

   debug {
            applicationIdSuffix ".debug"
            debuggable true
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
            multiDexKeepFile file('multidex-config.txt')
   }

dependencies {
   ...
   implementation 'com.hivemq:hivemq-mqtt-client:1.3.0'
   implementation 'net.sourceforge.streamsupport:android-retrostreams:1.7.4'
   implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.4'
   implementation 'com.yandex.android:mobmetricalib:4.2.0'
   ...
}

Hope you can help me.

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.