Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomemory authored and Ciobanu, Andrei-Nicolin (UK - EDC) committed May 11, 2017
1 parent c77c74a commit d269af7
Show file tree
Hide file tree
Showing 140 changed files with 2,175 additions and 400 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/andreinc/mockneat/MockNeat.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.andreinc.mockneat;

/*
/**
* Copyright 2017, Andrei N. Ciobanu
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.andreinc.mockneat.interfaces;

/*
/**
* Copyright 2017, Andrei N. Ciobanu
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
Expand Down
62 changes: 59 additions & 3 deletions src/main/java/net/andreinc/mockneat/interfaces/MockUnit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.andreinc.mockneat.interfaces;

/*
/**
* Copyright 2017, Andrei N. Ciobanu
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
Expand Down Expand Up @@ -33,8 +33,10 @@

import static java.lang.String.format;
import static java.util.stream.IntStream.range;
import static java.util.stream.Stream.generate;
import static net.andreinc.aleph.AlephFormatter.template;
import static net.andreinc.mockneat.utils.LoopsUtils.loop;
import static net.andreinc.mockneat.utils.MockUnitUtils.ifSupplierNotNullDo;
import static net.andreinc.mockneat.utils.MockUnitUtils.put;
import static net.andreinc.mockneat.utils.ValidationUtils.*;

Expand All @@ -45,8 +47,28 @@ public interface MockUnit<T> {
// Functional Method
Supplier<T> supplier();

/**
* It's a 'closing' method that returns an arbitrary value of type 'T'.
*
* @return the exact value mocked by the MockUnit<T> . The value is of type <T>.
*/
default T val() { return supplier().get(); }


default T valOrElse(T alternateVal) {
T result = supplier().get();
if (null==result)
return alternateVal;
return result;
}

/**
* Serialize an arbitary object generated by the MockUnit<T> to the disk.
* T should implement Serializable.
*
* @param strPath The path where the file is written to disk.
*
*/
// TODO Document
default void serialize(String strPath) {
T object = supplier().get();
Expand All @@ -58,20 +80,47 @@ default void serialize(String strPath) {
catch (FileNotFoundException e) { throw new UncheckedIOException(e); }
}

/**
* It's a closing method that returns an arbitray value of type <T>
*
* @param function The function that is applied to the final value before being returned.
* @param <R> The Returning type of the the applied Function<T, R>.
* @return
*/
default <R> R val(Function<T, R> function) {
notNull(function, "function");
return function.apply(supplier().get());
}

/**
* Consumes the returning value (of type <T>).
* So instead of returning the arbitrary value generated by the MockUnit<T>,
* this is getting consumed.
*
* @param consumer The consumer method.
*/
default void consume(Consumer<T> consumer) {
notNull(consumer, "consumer");
consumer.accept(val());
}

/**
* Returns the toString() representation of the arbitrary data generated by the MockUnit<T>.
* If the generated value is NULL, the default "" - empty string is generated.
*
* @return the toString() representation value of the data generated by the MockUnit<T>.
*/
default String valStr() {
return valStr("");
}

/**
* Retuns the toString() representation of the arbitrary genrated by the MockUnit<T>.
* If the generated value is NULL it's replaced with a default String value.
*
* @param valueIfNull The default value that is returned if the generated value is null.
* @return the toString() representation value of the data generated by the MockUnit<T>.
*/
default String valStr(String valueIfNull) {
Object val = supplier().get();
if (null == val) {
Expand All @@ -80,6 +129,13 @@ default String valStr(String valueIfNull) {
return val.toString();
}

/**
* This method is used to transform the existing MockUnit<T> mock unit into
* an MockUnit<R> through the function received as parameter (Function<T,R>).
*
* @param function The transforming function.
* @return
*/
default <R> MockUnit<R> map(Function<T, R> function) {
notNull(function, "function");
Supplier<R> supp = () -> function.apply(supplier().get());
Expand Down Expand Up @@ -111,11 +167,11 @@ default MockUnitString mapToString(Function<T, String> function) {
}

default MockUnitString mapToString() {
return () -> val()::toString;
return () -> ifSupplierNotNullDo(supplier(), s -> val().toString());
}

default MockUnit<Stream<T>> stream() {
Supplier<Stream<T>> supp = () -> Stream.generate(supplier());
Supplier<Stream<T>> supp = () -> generate(supplier());
return () -> supp;
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/andreinc/mockneat/interfaces/MockUnitDays.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
import java.util.function.Supplier;

import static java.time.format.TextStyle.FULL_STANDALONE;
import static net.andreinc.mockneat.utils.MockUnitUtils.ifSupplierNotNullDo;
import static net.andreinc.mockneat.utils.ValidationUtils.notNull;

public interface MockUnitDays extends MockUnit<DayOfWeek> {

default MockUnitString display(TextStyle textStyle, Locale locale) {
notNull(textStyle, "textStyle");
notNull(locale, "locale");
Supplier<String> supp =
() -> supplier().get().getDisplayName(textStyle, locale);
return () -> supp;
return () ->
ifSupplierNotNullDo(supplier(), s -> s.getDisplayName(textStyle, locale));
}

default MockUnitString display(TextStyle textStyle) {
Expand All @@ -40,6 +40,6 @@ default MockUnitString display(TextStyle textStyle) {
}

default MockUnitString display() {
return display(TextStyle.FULL_STANDALONE);
return display(FULL_STANDALONE);
}
}
25 changes: 21 additions & 4 deletions src/main/java/net/andreinc/mockneat/interfaces/MockUnitFloat.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package net.andreinc.mockneat.interfaces;

import net.andreinc.mockneat.utils.ValidationUtils;
/**
* Copyright 2017, Andrei N. Ciobanu
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, FREE_TEXT OF OR PARAM CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS PARAM THE SOFTWARE.
*/

import java.util.function.Supplier;
import java.util.stream.DoubleStream;

import static java.util.stream.DoubleStream.generate;
import static java.util.stream.IntStream.range;
import static net.andreinc.mockneat.utils.ValidationUtils.SIZE_BIGGER_THAN_ZERO;
import static net.andreinc.mockneat.utils.ValidationUtils.isTrue;

//TODO add it in documentation
public interface MockUnitFloat extends MockUnit<Float> {

default MockUnit<DoubleStream> doubleStream() {
Supplier<DoubleStream> supp = () -> DoubleStream.generate(supplier()::get);
Supplier<DoubleStream> supp = () -> generate(supplier()::get);
return () -> supp;
}

default MockUnit<float[]> arrayPrimitive(int size) {
isTrue(size>=0, ValidationUtils.SIZE_BIGGER_THAN_ZERO);
isTrue(size>=0, SIZE_BIGGER_THAN_ZERO);
Supplier<float[]> supp = () -> {
final float[] result = new float[size];
range(0, size).forEach(i -> result[i] = val());
Expand All @@ -26,7 +43,7 @@ default MockUnit<float[]> arrayPrimitive(int size) {
return () -> supp;
}
default MockUnit<Float[]> array(int size) {
isTrue(size>=0, ValidationUtils.SIZE_BIGGER_THAN_ZERO);
isTrue(size>=0, SIZE_BIGGER_THAN_ZERO);
Supplier<Float[]> supp = () -> {
final Float[] result = new Float[size];
range(0, size).forEach(i -> result[i] = val());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package net.andreinc.mockneat.interfaces;

/**
* Copyright 2017, Andrei N. Ciobanu
Permission is hereby granted, free of charge, to any user obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, FREE_TEXT OF OR PARAM CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS PARAM THE SOFTWARE.
*/

import java.time.LocalDate;

public interface MockUnitLocalDate extends MockUnit<LocalDate> {
Expand Down

This file was deleted.

Loading

0 comments on commit d269af7

Please sign in to comment.