Skip to content

Commit

Permalink
Migrating to java.util.Function
Browse files Browse the repository at this point in the history
  • Loading branch information
mplatvoet committed Dec 9, 2016
1 parent d30c557 commit 7b7d303
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 118 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Aug 17 20:36:15 CEST 2016
#Fri Dec 09 14:04:14 CET 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip
19 changes: 11 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

##############################################################################
##
Expand Down Expand Up @@ -154,16 +154,19 @@ if $cygwin ; then
esac
fi

# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
# Escape application args
save ( ) {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
APP_ARGS=$(save "$@")

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
exec "$JAVACMD" "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Function;

public class ImmutableMatrix<T> implements Matrix<T> {
private final AbstractMatrixCell[][] cells;
Expand Down Expand Up @@ -73,7 +74,7 @@ private <S> ImmutableMatrix(Matrix<S> matrix, Range range, CellMapFunction<S, T>
columnsIterable = new ArrayIterable<>(columns);
}

private ImmutableMatrix(int rows, int columns, CellFunction<T, MutableCell<T>> fill) {
private ImmutableMatrix(int rows, int columns, Function<MutableCell<T>, T> fill) {
Arguments.checkArgument(rows < 0, "row must be >= 0 but was %s", rows);
Arguments.checkArgument(columns < 0, "row must be >= 0 but was %s", columns);
Arguments.checkArgument(fill == null, "cells function cannot be null");
Expand All @@ -92,7 +93,7 @@ private ImmutableMatrix(int rows, int columns, CellFunction<T, MutableCell<T>> f
columnsIterable = new ArrayIterable<>(this.columns);
}

public static <T> Matrix<T> of(int rows, int columns, CellFunction<T, MutableCell<T>> fill) {
public static <T> Matrix<T> of(int rows, int columns, Function<MutableCell<T>, T> fill) {
return new ImmutableMatrix<>(rows, columns, fill);
}

Expand Down Expand Up @@ -148,7 +149,7 @@ private <S> void fillCells(Matrix<S> source, Range range, CellMapFunction<S, T>
}


private void fillCells(CellFunction<T, MutableCell<T>> fn) {
private void fillCells(Function<MutableCell<T>, T> fn) {
DetachedCell<T> cell = new DetachedCell<>();
for (int r = 0; r < cells.length; ++r) {
for (int c = 0; c < cells[r].length; ++c) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nl/mplatvoet/collections/matrix/Matrices.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nl.mplatvoet.collections.matrix;

import nl.mplatvoet.collections.matrix.fn.CellFunction;
import nl.mplatvoet.collections.matrix.fn.CellMapFunction;
import nl.mplatvoet.collections.matrix.range.Range;

import java.util.Comparator;
import java.util.Iterator;
import java.util.function.Function;

import static nl.mplatvoet.collections.matrix.args.Arguments.checkArgument;

Expand All @@ -27,7 +27,7 @@ public static <T> MutableMatrix<T> mutableOf(int rows, int columns, CellMapFunct
return MutableArrayMatrix.of(rows, columns, fill);
}

public static <T> Matrix<T> of(int rows, int columns, CellFunction<T, MutableCell<T>> fill) {
public static <T> Matrix<T> of(int rows, int columns, Function<MutableCell<T>, T> fill) {
return ImmutableMatrix.of(rows, columns, fill);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ private MutableArrayMatrix(T[][] source) {

}

private int maxColumn(T[][] source) {
int max = 0;
for (T[] ts : source) {
max = Math.max(max, ts.length);
}
return max;
}

@SuppressWarnings("unchecked")
private MutableArrayMatrix(int initialRows, int initialColumns, CellMapFunction<T, T> function) {
checkArgument(initialRows < 0, "initialRows must be >= 0, but was %s", initialRows);
Expand Down Expand Up @@ -125,6 +117,14 @@ public static <T, R> MutableMatrix<R> copyOf(Matrix<T> matrix, Range range, Cell
return new MutableArrayMatrix<>(matrix, range, transform);
}

private int maxColumn(T[][] source) {
int max = 0;
for (T[] ts : source) {
max = Math.max(max, ts.length);
}
return max;
}

private <S> void fillCells(Matrix<S> source, Range range, CellMapFunction<S, T> map) {
final int rOffset = range.getRowBeginIndex();
final int cOffset = range.getColumnBeginIndex();
Expand Down Expand Up @@ -847,7 +847,7 @@ private IndexMatrixCell(MutableArrayMatrix<T> matrix, int rowIndex, int columnIn
}

private IndexMatrixCell(MutableArrayMatrix<T> matrix, int rowIndex, int columnIndex) {
this(matrix, rowIndex, columnIndex, IndexMatrixCell.<T>blank());
this(matrix, rowIndex, columnIndex, IndexMatrixCell.blank());
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -880,9 +880,11 @@ public T getValue() {
}

@Override
public void setValue(T value) {
public T setValue(T value) {
assertState();
T prev = this.value;
this.value = value;
return prev;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nl.mplatvoet.collections.matrix;

public interface MutableCell<T> extends Cell<T> {
void setValue(T value);
T setValue(T value);

void clear();
}

This file was deleted.

23 changes: 4 additions & 19 deletions src/main/java/nl/mplatvoet/collections/matrix/fn/DetachedCell.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package nl.mplatvoet.collections.matrix.fn;

import nl.mplatvoet.collections.matrix.Cell;
import nl.mplatvoet.collections.matrix.MutableCell;
import nl.mplatvoet.collections.matrix.args.Arguments;

import static nl.mplatvoet.collections.matrix.args.Arguments.checkArgument;

public final class DetachedCell<T> implements MutableCell<T> {
private static final Object BLANK = new Object();
Expand Down Expand Up @@ -44,36 +40,25 @@ public int getRowIndex() {
return rowIndex;
}

public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}

public void setColumnIndex(int columnIndex) {
this.columnIndex = columnIndex;
}

@Override
public boolean isBlank() {
return value == BLANK;
}

@Override
public void setValue(T value) {
public T setValue(T value) {
T prev = this.value;
this.value = value;
return prev == BLANK ? null : prev;
}

public void apply(int row, int column) {
apply(row, column, DetachedCell.<T>blank());
apply(row, column, DetachedCell.blank());
}

public void apply(int row, int column, T value) {
this.value = value;
rowIndex = row;
columnIndex = column;
}

public void apply(Cell<? extends T> cell) {
checkArgument(cell == null, "cell cannot be null");
apply(cell.getRowIndex(), cell.getColumnIndex(), cell.getValue());
}
}
46 changes: 22 additions & 24 deletions src/main/java/nl/mplatvoet/collections/matrix/fn/Functions.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package nl.mplatvoet.collections.matrix.fn;

import nl.mplatvoet.collections.matrix.Cell;
import nl.mplatvoet.collections.matrix.MatrixCell;
import nl.mplatvoet.collections.matrix.MutableCell;
import nl.mplatvoet.collections.matrix.args.Arguments;

import java.util.function.Function;

import static nl.mplatvoet.collections.matrix.args.Arguments.checkArgument;

public class Functions {

private static final CellMapFunction<Object, Object> PASS_TROUGH_FUNCTION = new CellMapFunction<Object, Object>() {
@Override
public void apply(MatrixCell<Object> source, MutableCell<Object> dest) {
if (!source.isBlank()) {
dest.setValue(source.getValue());
}
private static final CellMapFunction<Object, Object> PASS_TROUGH_FUNCTION = (source, dest) -> {
if (!source.isBlank()) {
dest.setValue(source.getValue());
}
};

public static <T, C extends MutableCell<T>> CellFunction<T, C> cellFunctionOf(ValueFunction<T, T> valueFunction) {
return new ValueFunctionCellFunction<>(valueFunction);
public static <T, C extends MutableCell<T>> Function<C, T> cellFunctionOf(Function<T, T> valueFunction) {
return new ValueFunction<>(valueFunction);
}

public static <S, D> CellMapFunction<S, D> cellMapFunctionOf(ValueFunction<S, D> valueFunction) {
return new ValueFunctionCellMapFunction(valueFunction);
public static <S, D> CellMapFunction<S, D> cellMapFunctionOf(Function<S, D> valueFunction) {
return new ValueFunctionCellMapFunction<>(valueFunction);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -59,32 +56,33 @@ public void apply(MatrixCell<S> source, MutableCell<D> dest) {
}

private static class ValueFunctionCellMapFunction<T, R> implements CellMapFunction<T, R> {
private final ValueFunction<T, R> valueFunction;
private final Function<T, R> function;

private ValueFunctionCellMapFunction(ValueFunction<T, R> valueFunction) {
checkArgument(valueFunction == null, "valueFunction cannot be null");
this.valueFunction = valueFunction;
private ValueFunctionCellMapFunction(Function<T, R> function) {
checkArgument(function == null, "function cannot be null");
this.function = function;
}

@Override
public void apply(MatrixCell<T> source, MutableCell<R> dest) {
R result = valueFunction.apply(source.getRowIndex(), source.getColumnIndex(), source.getValue());
R result = function.apply(source.getValue());
dest.setValue(result);
}
}

private static class ValueFunctionCellFunction<T, C extends MutableCell<T>> implements CellFunction<T, C> {
private final ValueFunction<T, T> valueFunction;
private static class ValueFunction<T, C extends MutableCell<T>> implements Function<C, T> {
private final Function<T, T> function;

private ValueFunctionCellFunction(ValueFunction<T, T> valueFunction) {
checkArgument(valueFunction == null, "valueFunction cannot be null");
this.valueFunction = valueFunction;
private ValueFunction(Function<T, T> valueFunction) {
checkArgument(valueFunction == null, "function cannot be null");
this.function = valueFunction;
}

@Override
public void apply(C cell) {
T result = valueFunction.apply(cell.getRowIndex(), cell.getColumnIndex(), cell.getValue());
public T apply(C cell) {
T result = function.apply(cell.getValue());
cell.setValue(result);
return result;
}
}
}

This file was deleted.

19 changes: 8 additions & 11 deletions src/test/java/nl/mplatvoet/collections/matrix/ExampleUtil.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package nl.mplatvoet.collections.matrix;

import nl.mplatvoet.collections.matrix.fn.CellFunction;

import java.util.Random;

public class ExampleUtil {
public static Matrix<String> randomMatrix(int rows, int columns) {
return Matrices.of(rows, columns, new CellFunction<String, MutableCell<String>>() {
private final Random random = new Random();
@Override
public void apply(MutableCell<String> cell) {
if (random.nextInt(3) == 0) {
cell.setValue("*");
}
class ExampleUtil {
private static final Random RANDOM = new Random();
static Matrix<String> randomMatrix(int rows, int columns) {
return Matrices.of(rows, columns, cell -> {
if (RANDOM.nextInt(3) == 0) {
cell.setValue("*");
}
return null;
});
}

public static void printMatrix(Matrix<?> matrix) {
static void printMatrix(Matrix<?> matrix) {
for (Row<?> row : matrix.rows()) {
for (Object value : row) {
System.out.print(value == null ? " " : value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package nl.mplatvoet.collections.matrix;

import nl.mplatvoet.collections.matrix.fn.CellMapFunction;
import nl.mplatvoet.collections.matrix.fn.Functions;
import nl.mplatvoet.collections.matrix.fn.ValueFunction;

import static nl.mplatvoet.collections.matrix.ExampleUtil.randomMatrix;
import static nl.mplatvoet.collections.matrix.fn.Functions.cellMapFunctionOf;
import static nl.mplatvoet.collections.matrix.fn.Functions.noBlanks;

public class ImmutableExample {
public static void main(String[] args) {
Matrix<String> matrix = randomMatrix(25, 50);

CellMapFunction<String, String> function = Functions.noBlanks(Functions.cellMapFunctionOf(new ValueFunction<String, String>() {
@Override
public String apply(int row, int column, String value) {
return "0";
}
}));
Matrix<String> copy = matrix.map(function);
Matrix<String> copy = matrix.map(noBlanks(cellMapFunctionOf(value -> "0")));

ExampleUtil.printMatrix(matrix);
ExampleUtil.printMatrix(copy);
Expand Down
Loading

0 comments on commit 7b7d303

Please sign in to comment.