Hazelcast Predicate in clause - hazelcast

Can anyone tell me how to 'in' clause with Hazelcast predicate. I want to use the following,
Predicates.in('column',value);
I want to pass the value as an ArrayList of values. But it is throwing error as it expects object that implements comparable. Is there any workaround for this?

Predicates.in takes arguments of types (String, Comparable...).
So for the column name you pass a String as you have done.
Comparable... means you can pass the individual values in a comma-separated list, but you can also pass them in an array. An ArrayList won't automatically be converted, but you can do it as follows:
ArrayList<String> values = new ArrayList<>(Arrays.asList("one", "two", "three");
Predicates.in("column", values.toArray(new String[]));
The (new String[]) argument is just to ensure you get back an array of Strings, otherwise you'll get an array of Objects.

Related

(Groovy) Add multiple values to an ArrayList

like the title says, I want to add multiple values to my arrayList in Groovy.
But it is not accepted.
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add("first","second");
If there is only a single value, it works perfectly.
Thanks in advance!
Use addAll: actorCollection.addAll("first","second")
Note: Groovy's list literal will give you an array list. So could just write def actorCollection = [] or even ... = ["first", "second"] to fill the list with the values right from the beginning.

How can I pass an Apache POI function to the Java Comparator class in a Groovy script

Attempting to perform a sorting of the rows in an Excel file based on the value in a column in SoapUI via a Groovy script since POI doesn't have any in-built row sorting functions. One method of doing the sorting I've come across uses the Comparator method. But the Comparator.comparing() requires a "keyExtractor" function to be passed into it. The keyExtractor function is the function that would return a Comparable sort key. (Ref: Comparator.comparing() method discription)
Tried to pass in the function as shown in the example of that document :
Row tempRow = rows.get(1) //Header row exists in the data
Comparator<Row> sortByColumn = Comparator.comparing((tempRow.getCell(colNum)::getStringCellValue))
rows.sort(Comparator.comparing(sortByColumn))
which throws an error in SoapUI Groovy saying that the :: is an unexpected token. Using the .& operator in place of the :: throws back a
groovy.lang.MissingMethodException: No signature of method: static java.util.Comparator.comparing() is applicable for argument types: (java.util.Comparator$$Lambda$225/23399299) values: [java.util.Comparator$$Lambda$225/23399299#748b00]
Any suggestions.??
Thanks
Comparator.comparing is a factory method that is used to generate a comparator from a lambda or method reference that returns the "sort key". You can also create a comparator class using an anonymous inner class or a Groovy closure. However, Groovy offers extension methods for sorting that use closures directly in much the same way that Comparator.comparing works.
int colNum = ... // the column to sort on
// use "tail()" to exclude the header row from the sorted result
def sorted = rows.tail().sort { row -> row.getCell(colNum).stringCellValue }

Getting the values and column names of an INSERT object in the Java Cassandra Driver

I have a Insert object created as follows:
Insert insert = QueryBuilder.insertInto("demo", "users");
insert.value("name", name);
insert.value("sport", "test");
insert.value("years", 2);
insert.value("vegetarian", true);
Somewhere else in my code, I need to get the list of names and values associated with this INSERT object. When I debug the code I can see two "values" and "names" ArrayLists that contain the information I need, however they are private and I cannot access them.
While insert.getObject(0); gets the object from the values ArrayList, I can't map the value to a column name. Furthermore insert.getValues(ProtocolVersion.V4, CodecRegistry.DEFAULT_INSTANCE); seems to serialize the objects and put them into a ByteBuffer which is not desirable.
I recommend to use the PreparedStatement & BoundStatement instead of simple Insert. First, you can get better performance if you're inserting a lot of data. And second - you'll be able to get a list of variables defined in prepared statement, together with associated values

Unable to get an object from a map giving d/t result for hardcoded value and dynamic value but having equal value

I dont know how to describe the problem, so weird. I have function like this:
long getPersonId(...){
//...
}
The above function returns Id of a person based on some arguments.
So I logged the return value of the function and it is 1.
Then I have code like this:
person = myMap.get(getPersonId(..))
which returns null object but this returns a valid Person object, why?:
person = myMap.get(1)
But as I described before getPersonId(..) returns 1, which basically means
myMap.get(getPersonId(..)) == myMap.get(1)
myMap is typed as Map<Long, Person> myMap
What is happening here?
In Groovy, as in Java, 1 is an int literal, not a long, so
myMap.get(1)
is attempting to look up the key Integer.valueOf(1), whereas
myMap.get(getPersonId(..))
is looking up the key Long.valueOf(getPersonId(...)). You need to make sure that when you populate the map you are definitely using Long keys rather than Integer ones, e.g.
myMap.put(1L, somePerson)
In your original version of this question you were calling the GORM get method on a domain class rather than the java.util.Map.get method, and that should work as required as the GORM method call converts the ID to the appropriate type for you before passing it on to Hibernate.
I am so sorry the problem was when I initialize the map myMap
Map<Long, Person> myMap = [1, new Person()]
when you say something like this the key is an integerbut not a long still groovy not complaining.
So the problem is my method was returning a long value (1L) but my actual key on the map is integer value(1).
So changing my map init to Map<Long, Person> myMap = [1L, new Person()] solved the problem.
Probably this due to dynamic nature groovy but irritating unless you know how dynamic langs behave lol.

Casting to types that are know only at Runtime by their string names. C#

I've got a problem here. (C#)
There's a collection in another assembly (I cannot change it) that takes a string as parameter and returns an object.
Like:
object Value = ThatCollection.GetValue("ParameterName");
The problem is, for each parameter string, it returns a DIFFERENT type as object.
What I want is to cast those objects to their respective types, knowing the types only at runtime by their string names.
I need to do some operations with those returned values.
And for that I need to cast them properly in order to access their members and so.
Limitations:
I cannot use "dynamic" since my code needs to be done in an older framework: 3.5 (because of interop issues).
I need to do operations with MANY returned values of different types (no common interfaces nor base classes, except "object", of course)
All I have is a table (containing string values) correlating the parameter names with their returned types.
Yes, I could transform that table into a biiig "switch" statement, not very nice, don't want that.
Any hints??
You want to look into reflection, something like the following should work to cast an object to type T. Set up a simple cast method:
public static T CastToType<T>(object o)
{
return (T)o;
}
Invoke this using reflection:
Type t = Type.GetType(stringName)
MethodInfo castTypeMethod = this.GetType().GetMethod("CastToType").MakeGenericMethod(t);
object castedObject = castTypeMethod .Invoke(null, new object[] { obj });

Resources