Skip to content

Commit

Permalink
version 1.1.0: -removes creation of ArrayList in constructor, works w…
Browse files Browse the repository at this point in the history
…ith collections -adds groupBy method
  • Loading branch information
loukaspd committed Jul 22, 2019
1 parent bd5c861 commit 951081d
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 30 deletions.
4 changes: 2 additions & 2 deletions LinqArraylist/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ ext {
libraryName = 'LinqArraylist'
artifact = 'LinqArraylist'

libraryDescription = 'Linq-like functionality for Java ArrayList'
libraryVersion = '1.0.0'
libraryDescription = 'Linq-like functionality for Java Collections'
libraryVersion = '1.1.0'

siteUrl = 'https://github.com/loukaspd/LinqArraylist'
gitUrl = 'https://github.com/loukaspd/LinqArraylist.git'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,126 @@
package loukaspd.gr.linqarraylistproject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.*;

public class LinqArrayList<E> {
// ---------- Class variables - Constructor ---------- //
private final ArrayList<E> _arrayList;
private final Collection<E> _collection;
public ArrayList<E> getArrayList() {
return _arrayList;
if (_collection instanceof ArrayList) return (ArrayList<E>) _collection;
return new ArrayList<>(_collection);
}
public Collection<E> getCollection() {return _collection;}

public LinqArrayList(ArrayList<E> arrayList){
_arrayList = arrayList;
}
public LinqArrayList(Collection<E> collection) {
_arrayList = new ArrayList<E>(collection);
_collection = collection;
}


// ---------- Interfaces ---------- //
public interface VoidFunc<E> {
void lambda(E item);
};
}
public interface BooleanFunc<E> {
boolean lambda(E item);
};
}
public interface SelectFunc<E,T> {
T lambda(E item);
};
}
public interface SelectManyFunc<E,T> {
Collection<T> lambda(E item);
}
public interface SumFunc<E> {
double lambda(E item);
};
}



// ---------- Methods ---------- //
public void forEach(VoidFunc<E> func) {
for(E item : _arrayList) {
Enumeration<E> enumeration = Collections.enumeration(_collection);

while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

func.lambda(item);
}
}

public E first(BooleanFunc<E> func){
for(E item : _arrayList) {
Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

if (func.lambda(item)) return item;
}
throw new IllegalStateException();
}

public E firstOrDefault(BooleanFunc<E> func){
for(E item : _arrayList) {
Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

if (func.lambda(item)) return item;
}
return null;
}

public LinqArrayList<E> where(BooleanFunc<E> func) {
ArrayList<E> result = new ArrayList<E>();
for(E item : _arrayList) {
Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

if (func.lambda(item)) result.add(item);
}
return new LinqArrayList<E>(result);
}

public <T> LinqArrayList<T> select(SelectFunc<E,T> func) {
ArrayList<T> result = new ArrayList<T>();
for(E item : _arrayList) {

Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

result.add(func.lambda(item));
}
return new LinqArrayList<T>(result);
}

public <T> LinqArrayList<T> selectMany(SelectManyFunc<E,T> func) {
ArrayList<T> result = new ArrayList<T>();

Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

Collection<T> collection = func.lambda(item);
if (collection == null) continue;
result.addAll(collection);
}

return new LinqArrayList<T>(result);
}

public boolean any(BooleanFunc<E> func) {
for(E item : _arrayList) {
Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

if (func.lambda(item)) return true;
}
return false;
}

public boolean remove(BooleanFunc<E> func) {
for(E item : _arrayList) {
Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

if (func.lambda(item)) {
_arrayList.remove(item);
_collection.remove(item);
return true;
}
}
Expand All @@ -90,7 +129,11 @@ public boolean remove(BooleanFunc<E> func) {

public LinqArrayList<E> distinct() {
ArrayList<E> result = new ArrayList<>();
for(E item : _arrayList) {

Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

if(!result.contains(item)) {
result.add(item);
}
Expand All @@ -100,9 +143,32 @@ public LinqArrayList<E> distinct() {

public double sum(SumFunc<E> func) {
double result = 0;
for(E item : _arrayList) {

Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

result += func.lambda(item);
}
return result;
}

public <T> Hashtable<T,ArrayList<E>> groupBy(SelectFunc<E,T> func) {
Hashtable<T,ArrayList<E>> dictionary = new Hashtable<T,ArrayList<E>>();

Enumeration<E> enumeration = Collections.enumeration(_collection);
while(enumeration.hasMoreElements()) {
E item = enumeration.nextElement();

T key = func.lambda(item);
if (dictionary.containsKey(key)) dictionary.get(key).add(item);
else {
ArrayList<E> group = new ArrayList<>();
group.add(item);
dictionary.put(key, group);
}
}

return dictionary;
}
}
11 changes: 7 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# LinqArraylist

use Linq-like functions to Java ArrayLists
[ ![Download](https://api.bintray.com/packages/loukaspd/LinqArraylist/LinqArraylist/images/download.svg) ](https://bintray.com/loukaspd/LinqArraylist/LinqArraylist/_latestVersion)
[![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-LinqArraylist-green.svg?style=flat )]( https://android-arsenal.com/details/1/7060 )
use Linq-like functions to Java Collections


```
implementation 'gr.loukaspd:LinqArraylist:1.0.0'
implementation 'gr.loukaspd:LinqArraylist:{latest version}'
```
where `{latest version}` corresponds to published version in [ ![Download](https://api.bintray.com/packages/loukaspd/LinqArraylist/LinqArraylist/images/download.svg) ](https://bintray.com/loukaspd/LinqArraylist/LinqArraylist/_latestVersion)
[![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-LinqArraylist-green.svg?style=flat )]( https://android-arsenal.com/details/1/7060 )

## Usage
1. Instantiate a LinqArrayList object by passing it your original ArrayList
1. Instantiate a LinqArrayList object by passing it your original Collection
2. Apply linq methods (even chaining) like select, where etc..
3. Get the resulting ArrayList by calling `getArrayList()` method
3. Get the resulting Collection by calling `getCollection` or `getArrayList()` to get a new ArrayList from the collection


## Examples
Expand Down
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Sat Jul 21 00:42:45 EEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-rc-1-all.zip
Loading

0 comments on commit 951081d

Please sign in to comment.