Skip to content

Commit

Permalink
Add few java utils
Browse files Browse the repository at this point in the history
  • Loading branch information
manojlds committed Nov 10, 2018
1 parent 256dee3 commit 1bf5406
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/indix/mlflow_gocd/utils/Function.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.indix.mlflow_gocd.utils;

public interface Function<I, O> {
public O apply(I input);
}
19 changes: 19 additions & 0 deletions src/main/java/com/indix/mlflow_gocd/utils/Functions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.indix.mlflow_gocd.utils;

public class Functions {
public static abstract class VoidFunction<I> implements Function<I, Void> {
public abstract void execute(I input);

public Void apply(I input) {
execute(input);
return null;
}
}

public static abstract class Predicate<T> implements Function<T, Boolean> {
public abstract Boolean execute(T input);
public Boolean apply(T input){
return execute(input);
}
}
}
66 changes: 66 additions & 0 deletions src/main/java/com/indix/mlflow_gocd/utils/Lists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.indix.mlflow_gocd.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Lists {

public static <T, K> void foreach(List<T> originalList, Function<T, Void> function) {
for (T item : originalList) {
function.apply(item);
}
}

public static <T, K> void foreach(T[] array, Function<T, Void> function) {
foreach(Arrays.asList(array), function);
}

public static <T, K> List<K> flatMap(List<T> originalList, Function<T, List<K>> transformer) {
ArrayList<K> flatMapped = new ArrayList<K>();
for (T item : originalList) {
flatMapped.addAll(transformer.apply(item));
}
return flatMapped;
}

public static <T, K> List<K> flatMap(T[] array, Function<T, List<K>> transformer) {
return flatMap(Arrays.asList(array), transformer);
}

public static <T> List<T> of(T... items) {
ArrayList<T> listToReturn = new ArrayList<T>();
Collections.addAll(listToReturn, items);
return Collections.unmodifiableList(listToReturn);
}

public static <T,K> List<K> map(T[] array, Function<T, K> transformer) {
return map(Arrays.asList(array), transformer);
}

public static <T,K> List<K> map(List<T> elements, Function<T, K> transformer) {
List<K> mapped = new ArrayList<K>();
for (T item: elements) {
mapped.add(transformer.apply(item));
}
return mapped;
}

public static <T> List<T> filter(List<T> elements, Functions.Predicate<T> predicate) {
List<T> filtered = new ArrayList<T>();
for(T element: elements){
if(predicate.execute(element))
filtered.add(element);
}
return filtered;
}

public static <T> Boolean exists(List<T> elements, Functions.Predicate<T> predicate) {
for (T element: elements) {
if(predicate.execute(element))
return true;
}
return false;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/indix/mlflow_gocd/utils/Maps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.indix.mlflow_gocd.utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Maps {
public static <K, V> MapBuilder<K, V> builder() {
return new MapBuilder<K, V>();
}
public static <K, V> boolean isEmpty(Map<K, V> map) {
return map.isEmpty();
}

public static class MapBuilder<K, V> {
private Map<K, V> internal = new HashMap<K, V>();

public MapBuilder<K, V> with(K key, V value) {
internal.put(key, value);
return this;
}

public MapBuilder<K, V> remove(K key) {
internal.remove(key);
return this;
}

public Map<K, V> build() {
return Collections.unmodifiableMap(internal);
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/indix/mlflow_gocd/utils/Tuple2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.indix.mlflow_gocd.utils;

public class Tuple2<Left, Right> {
private Left _1;
private Right _2;

public Tuple2(Left _1, Right _2) {
this._1 = _1;
this._2 = _2;
}

public Left _1() {
return _1;
}

public Right _2() {
return _2;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Tuple2 tuple2 = (Tuple2) o;

if (!_1.equals(tuple2._1)) return false;
if (!_2.equals(tuple2._2)) return false;

return true;
}

@Override
public int hashCode() {
int result = _1.hashCode();
result = 31 * result + _2.hashCode();
return result;
}
}

0 comments on commit 1bf5406

Please sign in to comment.