Android LINQ


Source link: https://github.com/zbra-solutions/android-linq

Android LINQ

Manipulate collections easily using C# LINQ style queries and Java 8 closures.

Description

Android LINQ is a small subset of collection manipulation utilities inspired by Microsoft C# LINQ library and targeted at Android developers looking to use new Java 8 Stream() API.

By using Retrolambda for Android, developers can leverage the power of closures and other new Java 8 features. Unfortunately, it doesn't allow the usage of the Stream API which is arguably its most awesome feature. However, by using it in conjunction with Android LINQ, its possible to perform powerful collection manipulation in just a few lines of code.

Android LINQ has little to no impact on performance because it does not make use of reflection or proxies. As its C# counterpart it's based on the monads concept, which is a fancy word to describe a sort of Decorator pattern implementation, and many sorting and ordering are just making calls to the default Java API.

Anyway, you need not to worry. Just add this to your Gradle/Maven and suffer with manual collection iteration no more!

Usage

Latest Version

Android

To use Android LINQ, first, go and setup Retrolambda for Android so we can use those fancy closures from Java 8 (don't worry, its just some extra lines on your build.gradle file).

Now, just add this line to your project build.gradle (files are hosted in Bintray jCenter, so don't forget to add it to the repositories list too).

Gradle

... repositories {

  jcenter() 
}
 ... 
compile 'br.com.zbra:android-linq:1.1.0' 

Standard Java 8

Android LINQ uses standard Java and therefore can also be used outside Android.

Maven

<repositories>
  ...
  <repository>

 <id>jcenter</id>

 <url>http://jcenter.bintray.com </url>

 ...
  </repository>
  ... </repositories> 
<dependency>
<groupId>br.com.zbra</groupId>
<artifactId>android-linq</artifactId>
<version>1.1.0</version> </dependency> 

Examples

Get names from contacts

List<String> contactNames =

  stream(contacts)

  .select(c -> c.getName())

  .toList();
 

Get contacts who are 27 years or older

List<Contact> contacts =

  stream(contacts)

  .where(c -> c.getAge() >= 27)

  .toList();
 

Sort contacts by name, then by age

List<Contact> contactNames =

  stream(contacts)

  .orderBy(c -> c.getName())

  .thenBy(c -> c.Age())

  .toList();
 

Group products by category

Map<Category, Stream<Product>> productsByCategory

 stream(products)

  .groupBy(p -> p.getCategory())

  .toMap(g -> g.getKey() /* Category */, g.getElements() /* Stream<Product> */) 

Calculate the total price of a purchase

double total =

  stream(purchase.getItems())

  .sum((Purchase p) -> p.getPrice());
 

There are many more methods: first(), single(), distinct(), any(), aggregate(), count(), take(), skip() and reverse() are all available. Have fun!

Pull Requests Are Welcome!

Please, send feedback and pull requests a plenty! If you find a bug, a missing feature or have an improvement suggestion, don't be afraid to file an issue and we will do our best to attend it.

Resources

An extension to Moshi that provides general purpose, simple and yet useful JsonAdapters that are not present in the main library.

A pure MVC base library on android. Using it, your code can be very clean in MVC pattern: light Activity, light View, light Model.

A download progressbar with cool animation.

A fluent API to integrate with Strava on Android apps.

A painless way to pick future time.

BottomSheetPickers is a set of new time pickers for Android that can be used in place of the stock time picker.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes