I'm wondering how does lambdas external references work. Let me explain:
Suppose i have this supplier implementation and this model class :
public class TestSupplierImpl implements Supplier<Boolean> {
public Predicate<Integer> predicate;
public TestSupplierModel model;
public TestSupplierImpl() {
this.predicate = i -> model.something.equals(i);
}
#Override
public Boolean get() {
return predicate.test(3);
}
}
class TestSupplierModel {
public Integer something;
public TestSupplierModel(Integer something) {
this.something = something;
}
}
Then i execute the following code:
TestSupplierImpl test = new TestSupplierImpl(); // line 1
test.model = new TestSupplierModel(3); // line 2
Boolean resultado = test.get(); // line 3
Line 1: creating a new instance of TestSupplierImpl. This new instance's predicate has a null reference of model. This makes sense because at the moment of creation of the predicate, model reference is null.
Line 2: assign to variable model a new instance of TestSupplierModel.
Line 3: test.predicate now has model reference with the new assigned value. Why is this ?
I don't understand why ,when I changed model reference, the predicate updates its model reference to the new one. How is that ?
Thanks in advance !
Does it make sense if you rewrote your TestSupplierImpl() constructor as follows?
public Predicate<Integer> predicate;
public TestSupplierModel model;
public TestSupplierImpl() {
// same effect as this.predicate = i -> model.something.equals(i);
this.predicate = new Predicate<Integer>() {
public boolean test(Integer i) {
return model.something.equals(i);
}
};
}
#Override
public Boolean get() {
return predicate.test(3);
}
So here is the order of things.
// the constructor is run and the test method defined BUT NOT executed.
TestSupplierImpl test = new TestSupplierImpl(); // line 1
// Now you define model
test.model = new TestSupplierModel(3); // line 2
// Then you execute the predictate via get()
Boolean resultado = test.get(); // line 3
model and something aren't required until you issue the get() method. By that time they are already defined.
Related
I have a Java class like below
class MyClass {
public boolean rule1() {...}
public boolean rule2() {...}
public boolean rule3() {...}
}
now for instance I want to check the rule of above class in many ways such as :
MyClass myClass = new MyClass();
if (myClass.rule1() && myClass.rule2 || myClass.rule3) {}
and Now I am wondering that how can I implement above line with like this one?
if (myClass.rule1().and().rule2().or().rule3().accept()) {}
The cleaner way would be to use the functional interface Predicate:
Type Parameters:
T - the type of the input to the predicate Functional
Interface: This is a functional interface and can therefore be used as
the assignment target for a lambda expression or method reference.
public class A {
public Predicate rule1() {
return //some operation that returns a boolean;
}
public Predicate rule2() {
return //some operation that returns a boolean;
}
public Predicate rule3() {
return //some operation that returns a boolean;
}
}
But the if chain of method calls would not look like you are looking for, namely:
if (myClass.rule1().and().rule2().or().rule3().accept()) {}
Otherwise, you would have to implement the Builder pattern, and implement the and(), or(), and accept methods. For instance:
public class BooleanEvaluator {
List<String> rules = new ArrayList<>();
public BooleanEvaluator and() {
rules.add("&&");
return this;
}
public BooleanEvaluator or() {
rules.add("or");
return this;
}
public boolean accept() {
int i = 0;
boolean result = Boolean.parseBoolean(rules.get(0));
while (i < rules.size() - 1) {
if(rules.get(i).equals("&&")){
result = result && Boolean.parseBoolean(rules.get(i + 1));
i+=2;
}
else if(rules.get(i).equals("||")){
result = result || Boolean.parseBoolean(rules.get(i + 1));
i+=2;
}
}
return false;
}
public BooleanEvaluator rule1() {
boolean result = // apply the rule 1
rules.add(String.valueOf(result));
return this;
}
public BooleanEvaluator rule2() {
boolean result = // apply the rule 2
rules.add(String.valueOf(result));
return this;
}
public BooleanEvaluator rule3() {
boolean result = // apply the rule 3
rules.add(String.valueOf(result));
return this;
}
void some_method(){
if (this.rule1().and().rule2().or().rule3().accept()) {
// ...
}
}
}
Naturally, the accept method would have to be much more robust, this is just to show what would the design look like.
Have a look at Predicate<T> and its and or or.
MyClass instance = new MyClass();
Predicate<MyClass> predicate = MyClass::rule1;
if (predicate.and(MyClass::rule2).or(MyClass::rule3).test(instance)) {
// todo
}
It might look less readable than the version you mentioned, renaming MyClass to something more meaningful would help. If it doesn't work for you, consider writing a Builder. Good complex examples of which can be found in, let's say, dynamic SQL builders (for example, jooq).
I have some models which implement an interface, i.e. they all implement a specific method, let's call it method "A". Now I have a class with method "B" which is supposed to call method "A". So I defined it so it gets the interface as an input argument, and all is well. The problem is when I give the models (which implement the interface) to method "B", it says the types are different.
I tried casting the models to the interface, it gives me the "incompatible types" error.
Here is what I have:
public interface BaseObject<T> {
boolean isEqualTo(T object); //AKA: method "A"
}
public interface BaseList<T extends BaseObject> {
boolean areListsEqual(List<T> firstList, List<T> secondList); //AKA: method "B"
}
public class Helper implements BaseList<BaseObject> {
#Override
public boolean areListsEqual(List<BaseObject> firstList, List<BaseObject> secondList) {
boolean isEqual = true;
...
for (int i = 0; i < firstList.size(); i++) {
isEqual = isEqual && firstList.get(i).isEqualTo(secondList.get(i));
}
return isEqual;
...
}
}
// I have different ones
public class MyModel implements BaseObject<MyModel>{
...
}
And here's the part which I get the errors:
List<MyModel> first = new ArrayList<>():
List<MyModel> second = new ArrayList<>():
...
new Helper().areListsEqual(first, second);
// This is another thing I tried
new Helper().areListsEqual((List<BaseObject>) first, (List<BaseObject>) second);
So I found a solution. I had to convert the list. I added the method to Helper class and converted each list before giving it to "areListsEqual" method. Here's how it goes:
public List<BaseObject> convertList(List inputList){
List<BaseObject> baseList = new ArrayList<>();
if (inputList != null && inputList.size() > 0)
baseList.addAll(inputList);
return baseList;
}
And this is how I call it:
Helper helper = new Helper();
helper.areListsEqual(helper.convertList(first), helper.convertList(second));
So, I've got an object constructor:
public Func(Function<Var[], Var<T>> function, Var... arguments) {
// Function is a standart 1.8 class
//...
//secret stuff
}
I call it like that:
return new Func(new Function<Var[], Var>() {
#Override
public Var apply(Var[] args) {
return instance.getAttribute(args[0].value());
}
}, arguments[0].unpack(instance) // unpack(...) returns Var object
);
And it works. Now, my IDE (Intellij IDEA) suggests me to replace Function declaration with lambda. Okay, lets do it:
return new Func(
args -> instance.getAttribute(args[0].value()),
arguments[0].unpack(instance)
);
Now I have an error on args:
Array type expected; found: 'java.lang.Object'
So, apparently args now is Object. Why? Is that a bug in IDE or what?
Entire code:
Template:
public class Template {
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
System.out.println(someMethod(someClass).value());
}
private static class SomeClass {
Var[] var = new Var[12];
SomeClass() {
var = new Var[12];
for ( int i = 0; i < var.length; i++) {
var[i] = new Var<>(i * 4);
}
}
Var getAttribute(int index) {
return var[index];
}
}
public static Var someMethod(SomeClass instance) {
return new Func(new Function<Var[], Var>() {
#Override
public Var apply(Var[] args) {
return instance.getAttribute((int)args[0].value());
}
}, new Var(4));
}
}
Var.java:
public class Var<T> {
private T value;
public Var(T value) {
this.value = value;
}
public T value() {
return value;
}
}
Func.java:
public class Func<T> extends Var<T> {
private Function<Var[], Var<T>> function;
private Var[] args;
public Func(Function<Var[], Var<T>> function, Var... args) {
super(null);
this.function = function;
this.args = args;
}
#Override
public T value() {
return function.apply(args).value();
}
}
The error message appers also in the Eclipse-IDE:
The type of the expression must be an array type but it resolved to Object
I think it is not an IDE-bug, neither in IntelliJ nor in Eclipse. The Compiler needs for the processing of a lambda expression always a target type which is a functional interface.
In the case of
args -> instance.getAttribute((int)args[0].value())
the target type is determined by the first argument of the Func-constructor
Function<Var[], Var<T>> function
However, this functional interface is a generic interface. Java compiles generics using type erasure which means the replacement of the generic parameter-types by the Object-type. Thus, the interface is compiled like
interface Function {
public Object apply(Object args);
}
and this is applied as target type. Thus, for args an Object-type instead of a Var[]-type is expected which results in an error message.
In case of an anonymous class this is different since more informations are provided for the determination of the target type.
new Function<Var[], Var>(){...}
explicitly contains the type-information. Because of this args is expected of Var[]-type and no error message is shown.
There are two possibilities to fix the error:
1) In the getAttribut-method cast args explicitly to Var[], i.e. replace
(int)args[0].value()
with
(int)((Var[])args)[0].value()
or 2) Don't use a generic interface i.e. change the interface to
interface Function {
public Var apply(Var[] args);
}
Then type information is preserved. Of course the rest of the code has to be adapted accordingly.
I have a test in which I have a set of specific values for which two different methods will execute once for each value in the set. I need to check that the two methods are called in a specific order in relation to each other, but not in relation to the order of the set of values. For example:
String[] values = { "A", "B", "C" };
for (...<loop over values...) {
methodOne(value);
methodTwo(value);
}
It does not matter which order values is in, but I need to verify that methodOne() and methodTwo() are called for each value in the set AND that methodOne() is always called before methodTwo().
I know that I can create a control and expect methodOne() and methodTwo() for each value, then do control.verify(), but this depends on values being in a specific order.
Is there an elegant way to do this?
Thanks
You can do this using andAnswer().
Basically, inside the andAnswer() from methodOne() you set some variable to hold what the passed in value was.
Then in the andAnswer() for methodTwo() you assert that the same argument matches what you saved from your methodOne answer.
Since each call to methodOne will modify this variable it will make sure methodTwo() is always called after methodOne().
Note this solution is not thread safe
First you need something to hold the variable from the methodOne call. This can be a simple class with a single field or even an array of one element. You need this wrapper object because you need to reference it in the IAnswer which requires a final or effectively final field.
private class CurrentValue{
private String methodOneArg;
}
Now your expectations. Here I called the class that you are testing (The System Under Test) sut:
String[] values = new String[]{"A", "B", "C"};
final CurrentValue currentValue = new CurrentValue();
sut.methodOne(isA(String.class));
expectLastCall().andAnswer(new IAnswer<Void>() {
#Override
public Void answer() throws Throwable {
//save the parameter passed in to our holder object
currentValue.methodOneArg =(String) EasyMock.getCurrentArguments()[0];
return null;
}
}).times(values.length); // do this once for every element in values
sut.methodTwo(isA(String.class));
expectLastCall().andAnswer(new IAnswer<Void>() {
#Override
public Void answer() throws Throwable {
String value =(String) EasyMock.getCurrentArguments()[0];
//check to make sure the parameter matches the
//the most recent call to methodOne()
assertEquals(currentValue.methodOneArg, value);
return null;
}
}).times(values.length); // do this once for every element in values
replay(sut);
... //do your test
verify(sut);
EDIT
you are correct that if you are using EasyMock 2.4 + you can use the new Capture class to get the argument value in a cleaner way for methodOne(). However, you may still need to use the andAnswer() for methodTwo() to make sure the correct values are called in order.
Here is the same code using Capture
Capture<String> captureArg = new Capture<>();
sut.methodOne(and(capture(captureArg), isA(String.class)));
expectLastCall().times(values.length);
sut.methodTwo(isA(String.class));
expectLastCall().andAnswer(new IAnswer<Void>() {
#Override
public Void answer() throws Throwable {
String value =(String) EasyMock.getCurrentArguments()[0];
assertEquals(captureArg.getValue(), value);
return null;
}
}).times(values.length);
replay(sut);
For those interested, I solved this issue using intended EasyMock functionality. The solution was to make a custom IArgumentMatcher to verify against a collection of values and to enforce how many times each value is matched consecutively. The custom matcher, in addition to using strict mocking exactly solves the original problem.
public class SetMatcher implements IArgumentMatcher {
private List<String> valuesToMatch;
private List<String> remainingValues;
private String currentValue = null;
private int timesMatched = 0;
private int setMatches;
public SetMatcher(final List<String> valuesToMatch, final int times) {
this.valuesToMatch = new ArrayList<String>(valuesToMatch);
this.remainingValues = new ArrayList<String>(valuesToMatch);
this.setMatches = times;
}
public String use() {
EasyMock.reportMatcher(this);
return null;
}
public void appendTo(StringBuffer buffer) {
if (this.remainingValues.size() == 0) {
buffer.append("all values in " + this.valuesToMatch + " already matched " + this.setMatches + " time(s)");
} else {
buffer.append("match " + this.valuesToMatch + " " + this.setMatches + " time(s) each");
}
}
public boolean matches(Object other) {
if (this.timesMatched >= this.setMatches) {
this.currentValue = null;
this.timesMatched = 0;
}
if (null == this.currentValue) {
if (this.remainingValues.contains(other)) {
this.currentValue = (String) other;
this.timesMatched = 1;
this.remainingValues.remove(other);
return true;
}
} else if (this.currentValue.equals(other)) {
this.timesMatched++;
return true;
}
return false;
}
}
The class being tested:
public class DataProcessor {
private ServiceOne serviceOne;
private ServiceTwo serviceTwo;
public DataProcessor(ServiceOne serviceOne, ServiceTwo serviceTwo) {
this.serviceOne = serviceOne;
this.serviceTwo = serviceTwo;
}
public void processAll(List<String> allValues) {
List<String> copy = new ArrayList<String>(allValues);
for (String value : copy) {
this.serviceOne.preProcessData(value);
this.serviceTwo.completeTransaction(value);
}
}
}
And the test:
public class DataProcessorTest {
List<String> TEST_VALUES = Arrays.asList("One", "Two", "Three", "Four", "Five");
#Test
public void test() {
IMocksControl control = EasyMock.createStrictControl();
ServiceOne serviceOne = control.createMock(ServiceOne.class);
ServiceTwo serviceTwo = control.createMock(ServiceTwo.class);
SetMatcher matcher = new SetMatcher(TEST_VALUES, 2);
for (int i = 0; i < TEST_VALUES.size(); i++) {
serviceOne.preProcessData(matcher.use());
serviceTwo.completeTransaction(matcher.use());
}
control.replay();
DataProcessor dataProcessor = new DataProcessor(serviceOne, serviceTwo);
dataProcessor.processAll(TEST_VALUES);
control.verify();
}
}
The test will fail for any of the following:
ServiceOne and ServiceTwo are called in the wrong order
ServiceOne and ServiceTwo are not called consecutively with the same value
ServiceOne or ServiceTwo are called with a value that is not in the specified value list
A call is made beyond the number of expected times for a value in the list
I have a Java class with following implementation:
class Some {
private ArrayList<SomeObject> list = new...
public void addToList(Long t) {
SomeObject so = new SomeObject(new Date, t)
list.add(so)
}
private float fun1(ArrayList<SomeObject> x) {
//some operations on list "list",
//res - result of float calculations based on list "x"
return res
}
public float publicFun() {
//some other operations on private list "list"
return fun1(list);
}
The question is how to test function publicFun() using Mockito, PowerMock or other testing tool ? To run this public function I have to mock private List but how can I do it ?
In this example there are several problems caused by unwelcome dependencies:
1 new Date()
To solve it I suggest to introduce new interface
interface CurrentTimeProvider {
Date getCurrentDate();
}
Implementation is obvious (I skip it for briefness)
2 Is new ArrayList()
You can replace it with you own interface (containing only method you
need)
You can mock ArrayList itself
You can use real impl of ArrayList and test it altogether
In result we get something like this:
class Some {
private CurrentTimeProvider timeProvider;
private ArrayList<SomeObject> list = new ArrayList<SomeObject>();
public void setTimeProvider(CurrentTimeProvider timeProvider) {
this.timeProvider = timeProvider;
}
public void addToList(Long t) {
SomeObject so = new SomeObject(timeProvider.getCurrentDate(), t)
list.add(so)
}
public float publicFun() {
//some other operations on private list "list"
return fun1(list);
}
And test look look this:
CurrentTimeProvider timeProvider = mock(CurrentTimeProvider.class);
Some some = new Some();
some.setTimeProvider(timeProvider);
when(timeProvider.getCurrentDate).thenReturn(mock(Date.class));
//Invoke you method
some.publicFun();
//Put assert and verify here