Created
December 8, 2015 19:59
-
-
Save haskellcamargo/01745e75291b6145761f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.business.util.seq; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 08/12/2015 | |
*/ | |
public interface Function<Ret, Arg> { | |
Ret call(Arg arg); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.business.util.seq; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 08/12/2015 | |
*/ | |
public class Seq<T> { | |
private List<T> value; | |
@SafeVarargs | |
public Seq(T ...args) { | |
value = new ArrayList<>(); | |
Collections.addAll(value, args); | |
} | |
public void add(T item) { | |
this.value.add(item); | |
} | |
public <Ret> Seq<Ret> map(Function<Ret, T> fn) { | |
Seq<Ret> buffer = new Seq(); | |
for (T item : this.value) { | |
buffer.add(fn.call(item)); | |
} | |
return buffer; | |
} | |
public Seq<T> filter(Function<Boolean, T> fn) { | |
Seq<T> buffer = new Seq<>(); | |
for (T item : this.value) { | |
if (fn.call(item)) { | |
buffer.add(item); | |
} | |
} | |
return buffer; | |
} | |
public ArrayList<T> toArrayList() { | |
return (ArrayList<T>) this.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment