Combining functions and consumers with double-column notation - java-8

I often use the double-colon notation for brevity.
I am writing the following method that takes a short list of entities, validates them, and saves back to database.
#Override#Transactional
public void bulkValidate(Collection<Entity> transactions)
{
Consumer<Entity> validator = entityValidator::validate;
validator = validator.andThen(getDao()::update);
if (transactions != null)
transactions.forEach(validator);
}
I'd like to know if there is a shorthand syntax avoiding to instantiate the validator variable
Following syntax is invalid ("The target type of this expression must be a functional interface")
transactions.forEach((entityValidator::validate).andThen(getDao()::update));

You could do that, but you would need to cast explicitly...
transactions.forEach(((Consumer<Entity>)(entityValidator::validate))
.andThen(getDao()::update));
The thing is that a method reference like this entityValidator::validate does not have a type, it's a poly expression and it depends on the context.
You could also define a method to combine these Consumers:
#SafeVarargs
private static <T> Consumer<T> combine(Consumer<T>... consumers) {
return Arrays.stream(consumers).reduce(s -> {}, Consumer::andThen);
}
And use it:
transactions.forEach(combine(entityValidator::validate, getDao()::update))

Related

Null Pointer Error when Mocking JDBC Template execute method [duplicate]

I've been trying to get to mock a method with vararg parameters using Mockito:
interface A {
B b(int x, int y, C... c);
}
A a = mock(A.class);
B b = mock(B.class);
when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b);
assertEquals(b, a.b(1, 2));
This doesn't work, however if I do this instead:
when(a.b(anyInt(), anyInt())).thenReturn(b);
assertEquals(b, a.b(1, 2));
This works, despite that I have completely omitted the varargs argument when stubbing the method.
Any clues?
Mockito 1.8.1 introduced anyVararg() matcher:
when(a.b(anyInt(), anyInt(), Matchers.<String>anyVararg())).thenReturn(b);
Also see history for this: https://code.google.com/archive/p/mockito/issues/62
Edit new syntax after deprecation:
when(a.b(anyInt(), anyInt(), ArgumentMatchers.<String>any())).thenReturn(b);
A somewhat undocumented feature: If you want to develop a custom Matcher that matches vararg arguments you need to have it implement org.mockito.internal.matchers.VarargMatcher for it to work correctly. It's an empty marker interface, without which Mockito will not correctly compare arguments when invoking a method with varargs using your Matcher.
For example:
class MyVarargMatcher extends ArgumentMatcher<C[]> implements VarargMatcher {
#Override public boolean matches(Object varargArgument) {
return /* does it match? */ true;
}
}
when(a.b(anyInt(), anyInt(), argThat(new MyVarargMatcher()))).thenReturn(b);
Building on Eli Levine's answer here is a more generic solution:
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.VarargMatcher;
import static org.mockito.Matchers.argThat;
public class VarArgMatcher<T> extends ArgumentMatcher<T[]> implements VarargMatcher {
public static <T> T[] varArgThat(Matcher<T[]> hamcrestMatcher) {
argThat(new VarArgMatcher(hamcrestMatcher));
return null;
}
private final Matcher<T[]> hamcrestMatcher;
private VarArgMatcher(Matcher<T[]> hamcrestMatcher) {
this.hamcrestMatcher = hamcrestMatcher;
}
#Override
public boolean matches(Object o) {
return hamcrestMatcher.matches(o);
}
#Override
public void describeTo(Description description) {
description.appendText("has varargs: ").appendDescriptionOf(hamcrestMatcher);
}
}
Then you can use it with hamcrest's array matchers thus:
verify(a).b(VarArgMatcher.varArgThat(
org.hamcrest.collection.IsArrayContaining.hasItemInArray("Test")));
(Obviously static imports will render this more readable.)
I have been using the code in Peter Westmacott's answer however with Mockito 2.2.15 you can now do the following:
verify(a).method(100L, arg1, arg2, arg3)
where arg1, arg2, arg3 are varargs.
I had to use the any(Class type) method to match an array arg being passed as a varargs parameter.
ArgumentMatchers.any(Class type)
Code in the implementation is vararg aware.
reportMatcher(new InstanceOf.VarArgAware(
In my case where matching a String[] arg to a String... param the following worked:-
any(String.class)
Building on topchef's answer,
For 2.0.31-beta I had to use Mockito.anyVararg instead of Matchers.anyVararrg:
when(a.b(anyInt(), anyInt(), Mockito.<String>anyVararg())).thenReturn(b);
Adapting the answer from #topchef,
Mockito.when(a.b(Mockito.anyInt(), Mockito.anyInt(), Mockito.any())).thenReturn(b);
Per the java docs for Mockito 2.23.4, Mockito.any() "Matches anything, including nulls and varargs."
You can accomplish this by passing an ArgumentCaptor capture and then retrieving the varargs as a list using "getAllValues", see: https://stackoverflow.com/a/55621731/11342928
As the other answers make sense and make tests work obviously, I still recommend to test as if the method didn't take a vararg, but rather regular well-defined parameters instead. This helps in situations where overridden methods in connection with possible ambiguous parameters are in place, like an SLF4J-logger:
to test:
jobLogger.info("{} finished: {} tasks processed with {} failures, took {}", jobName, count, errors, duration);
This has a bunch of overrides and the important method being declared like so
Logger.info(String, Object...)
verification:
verify(loggerMock).info(anyString(), anyString(), anyInt(), anyInt(), anyString());
proof that the above works as errors is an integer and not a long, so the following wouldn't run:
verify(loggerMock).info(anyString(), anyString(), anyInt(), anyLong(), anyString());
So you can easily use when() instead of the verify()-stuff to set up the required return value.
And it probably shows more of the intent and is more readable. Captures can also be used here and they are much easier accessible this way.
Tested with Mockito 2.15
In my case the signature of the method that I want to capture its argument is:
public byte[] write(byte ... data) throws IOException;
In this case you should cast to byte array explicitly:
when(spi.write((byte[])anyVararg())).thenReturn(someValue);
I'm using mockito version 1.10.19
You can also loop over the arguments:
Object[] args = invocation.getArguments();
for( int argNo = 0; argNo < args.length; ++argNo) {
// ... do something with args[argNo]
}
for example check their types and cast them appropriately, add to a list or whatever.

How to set different WriteConcern for different conditions on the basis of some MongoAction property?

What is the possible way to set different WriteConcern values based on some condition of the MongoAction?
For example, i want if the MongoAction contains some value like "ABD", i want there should be no WriteConcern. And if it contains "PAYMENT", then WriteConcern should be SAFE.
What is the possiblie cases i can achieve this?
The above can be done implementing the WriteConcernResolver and overriding the resolve method as follows:
class MyAppWriteConcernResolver implements WriteConcernResolver {
#Override
public WriteConcern resolve(MongoAction action) {
if (action.getEntityClass().getSimpleName().contains("ADB")) {
return WriteConcern.NONE;
} else if (action.getEntityClass().getSimpleName().contains("PAYMENT")) {
return WriteConcern.SAFE;
}
return action.getDefaultWriteConcern();
}
}
The passed in argument, MongoAction, is what you use to determine the WriteConcern value to be used. MongoAction contains the collection name being written to, the java.lang.Class of the POJO, the converted DBObject, as well as the operation as an enumeration (MongoActionOperation: REMOVE, UPDATE´, ´INSERT, INSERT_LIST, SAVE) and a few other pieces of contextual information.

How do I use a custom comparer with the Linq Distinct method?

I was reading a book about Linq, and saw that the Distinct method has an overload that takes a comparer. This would be a good solution to a problem I have where I want to get the distinct entities from a collection, but want the comparison to be on the entity ID, even if the other properties are different.
According to the book, if I have a Gribulator entity, I should be able to create a comparer like this...
private class GribulatorComparer : IComparer<Gribulator> {
public int Compare(Gribulator g1, Gribulator g2) {
return g1.ID.CompareTo(g2.ID);
}
}
...and then use it like this...
List<Gribulator> distinctGribulators
= myGribulators.Distinct(new GribulatorComparer()).ToList();
However, this gives the following compiler errors...
'System.Collections.Generic.List' does not contain a definition for 'Distinct' and the best extension method overload 'System.Linq.Enumerable.Distinct(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEqualityComparer)' has some invalid arguments
Argument 2: cannot convert from 'LinqPlayground.Program.GribulatorComparer' to 'System.Collections.Generic.IEqualityComparer'
I've searched around a bit, and have seen plenty of examples that use code like this, but no complaints about compiler errors.
What am I doing wrong? Also, is this the best way of doing this? I want a one-off solution here, so don't want to start changing the code for the entity itself. I want the entity to remain as normal, but just in this one place, compare by ID only.
Thanks for any help.
You're implementing your comparer as an IComparer<T>, the LINQ method overload requires an implementation of IEqualityComparer:
private class GribulatorComparer : IEqualityComparer<Gribulator> {
public bool Equals(Gribulator g1, Gribulator g2) {
return g1.ID == g2.ID;
}
}
edit:
For clarification, the IComparer interface can be used for sorting, as that's basically what the Compare() method does.
Like this:
items.OrderBy(x => new ItemComparer());
private class ItemComparer : IComparer<Item>
{
public int Compare(Item x, Item y)
{
return x.Id.CompareTo(y.Id)
}
}
Which will sort your collection using that comparer, however LINQ provides a way to do that for simple fields (like an int Id).
items.OrderBy(x => x.Id);

NHibernate: Using a LINQ expression with IQueryOver

I'm trying to use the IQueryOver interface of a NHibernate session object together with a LINQ expression as a criteria for selecting records in a static class. The LINQ expressions are defined in a mapping class as Expression<Func<T, object>> to get a value for an object T:
public void SearchParameter(Expression<Func<T, object>>)
These parameters get added by extending the mapping class:
public MyMapping : FindMap<MyNHibernateMappedObject>
{
public MyMapping()
{
this.SearchParameter(x => x.SomeColumn);
}
}
My Find class defines static methods for getting the previous and next record of the same type on the time axis. Each search parameter has to be identical in both records.
The Find class gets the search parameters from the mapping configuration and compiles the expressions with .Compile(). So I have the GetQueryWithSearchParameters method:
private static Func<T, object> searchParameter;
...
public static IQueryOver<T, T> GetQueryWithSearchParameters(ISession session, T current)
{
var query = session.QueryOver<T>()
.Where(x => searchParameter(x) == searchParameter(current));
return query;
}
However when building the query, I get the following exception:
System.InvalidOperationException: variable 'x' of type MyNHibernateMappedObject' referenced from scope '', but it is not defined
I don't know exactly what is going on here, but I suspect that x is not available in the delegate somehow. What am I doing wrong here?
session.QueryOver().Where(...) takes an expression, so it's going to try evaluate your expression and translate it to a query - ie. it will try to convert searchParameter(x) == searchParameter(current) into a SQL query, which it won't know how to do.
To get this to work you will need to construct the expression in code (not using a lambda expression). However I think that this is going to be a painful exercise and I think you will find it much easier to build a Criterion and add that to the QueryOver.

Using an IEqualityComparer with a LINQ to Entities Except clause

I have an entity that I'd like to compare with a subset and determine to select all except the subset.
So, my query looks like this:
Products.Except(ProductsToRemove(), new ProductComparer())
The ProductsToRemove() method returns a List<Product> after it performs a few tasks. So in it's simplest form it's the above.
The ProductComparer() class looks like this:
public class ProductComparer : IEqualityComparer<Product>
{
public bool Equals(Product a, Product b)
{
if (ReferenceEquals(a, b)) return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Id == b.Id;
}
public int GetHashCode(Product product)
{
if (ReferenceEquals(product, null)) return 0;
var hashProductId = product.Id.GetHashCode();
return hashProductId;
}
}
However, I continually receive the following exception:
LINQ to Entities does not recognize
the method
'System.Linq.IQueryable1[UnitedOne.Data.Sql.Product]
Except[Product](System.Linq.IQueryable1[UnitedOne.Data.Sql.Product],
System.Collections.Generic.IEnumerable1[UnitedOne.Data.Sql.Product],
System.Collections.Generic.IEqualityComparer1[UnitedOne.Data.Sql.Product])'
method, and this method cannot be
translated into a store expression.
Linq to Entities isn't actually executing your query, it is interpreting your code, converting it to TSQL, then executing that on the server.
Under the covers, it is coded with the knowledge of how operators and common functions operate and how those relate to TSQL. The problem is that the developers of L2E have no idea how exactly you are implementing IEqualityComparer. Therefore they cannot figure out that when you say Class A == Class B you mean (for example) "Where Person.FirstName == FirstName AND Person.LastName == LastName".
So, when the L2E interpreter hits a method it doesn't recognize, it throws this exception.
There are two ways you can work around this. First, develop a Where() that satisfies your equality requirements but that doesn't rely on any custom method. In other words, test for equality of properties of the instance rather than an Equals method defined on the class.
Second, you can trigger the execution of the query and then do your comparisons in memory. For instance:
var notThisItem = new Item{Id = "HurrDurr"};
var items = Db.Items.ToArray(); // Sql query executed here
var except = items.Except(notThisItem); // performed in memory
Obviously this will bring much more data across the wire and be more memory intensive. The first option is usually the best.
You're trying to convert the Except call with your custom IEqualityComparer into Entity SQL.
Obviously, your class cannot be converted into SQL.
You need to write Products.AsEnumerable().Except(ProductsToRemove(), new ProductComparer()) to force it to execute on the client. Note that this will download all of the products from the server.
By the way, your ProductComparer class should be a singleton, like this:
public class ProductComparer : IEqualityComparer<Product> {
private ProductComparer() { }
public static ProductComparer Instance = new ProductComparer();
...
}
The IEqualityComparer<T> can only be executed locally, it can't be translated to a SQL command, hence the error

Resources