-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
} |
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
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; | ||
} | ||
} |
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
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; | ||
} | ||
} |