I have the following route:
.choise()
.when()
.simple("${body.messageType} == 105")
.to(...)
.when()
.simple("${body.messageType} == 106")
.to(...)
I want to use this enum to compare.
package myPackage;
public enum Code {
CODE_A("105"),
CODE_B("106")
private String value;
Code(String value) {
this.value = value;
}
public String value() {
return value;
}
}
I tried to do something like this, but it doesn't works..
.simple("${body.messageType} == ${type:myPackage.Code.CODE_A.value()}")
Any idea to solve this problem?
According to http://camel.apache.org/simple.html you can use just
.simple("${body.messageType} == ${type:myPackage.Code.CODE_A}")
In example you have tried you are using
myPackage.Code.value()
not
myPackage.Code.CODE_A
You can solve it using Groovy expressions. Add camel-groovy to pom/gradle.
Use the following expression:
.when().groovy("body.messageType == myPackage.CODE_A.value()")
I solved it by doing the following:
.setHeader("codeA",constant(myPackage.Code.CODE_A.value()))
.setHeader("codeB",constant(myPackage.Code.CODE_B.value()))
.choise()
.when()
.simple("${body.messageType} == ${header.codeA}")
.to(...)
.when()
.simple("${body.messageType} == ${header.codeB}")
.to(...)
Thanks!
My simple syntax looks like:
//one value
simple(String.format("${body.key} == '%1s'", Code.CODE_A.value())
// multiple values using 'in' operator
simple(String.format("${body.key} in '%1s,%2s'", Code.CODE_A.value(), Code.CODE_B.value())
I like the use of String.format() instead of concatenating all parts as it makes the simple statement more readable
Related
I am trying to make a template that determines whether the arguments are arrays holding the same unqualified type.
template sameUnqualArrays(A, B) {
enum bool sameUnqualArrays = isArray!A && isArray!B &&
is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B));
}
unittest {
static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[])));
static assert(!sameUnqualArrays!(ubyte[], immutable(byte[])));
static assert(!sameUnqualArrays!(ubyte[], ubyte));
}
Unfortunately the last assert doesn't pass but gives the error:
/usr/include/dmd/phobos/std/traits.d(6062,9): Error: invalid foreach aggregate cast(ubyte)0u
Is this a bug? How to fix this? Is there an easier way to achieve this?
Hmm, it looks like short circuit evaluation is not working here? It might be a bug, but the workaround is easy enough:
import std.traits;
template sameUnqualArrays(A, B) {
static if (isArray!A && isArray!B)
enum bool sameUnqualArrays =
is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B));
else
enum bool sameUnqualArrays = false;
}
unittest {
static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[])));
static assert(!sameUnqualArrays!(ubyte[], immutable(byte[])));
static assert(!sameUnqualArrays!(ubyte[], ubyte));
}
I have the following code:
public fun findSomeLikeThis(): ArrayList<T>? {
val result = Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>
if (result == null) return null
return ArrayList(result)
}
If I call this like:
var list : ArrayList<Person>? = p1.findSomeLikeThis()
for (p2 in list) {
p2.delete()
p2.commit()
}
It would give me the error:
For-loop range must have an 'iterator()' method
Am I missing something here?
Your ArrayList is of nullable type. So, you have to resolve this. There are several options:
for (p2 in list.orEmpty()) { ... }
or
list?.let {
for (p2 in it) {
}
}
or you can just return an empty list
public fun findSomeLikeThis(): List<T> //Do you need mutable ArrayList here?
= (Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>)?.toList().orEmpty()
try
for(p2 in 0 until list.count()) {
...
...
}
I also face this problem when I loop on some thing it is not an array.
Example
fun maximum(prices: Array<Int>){
val sortedPrices = prices.sort()
for(price in sortedPrices){ // it will display for-loop range must have iterator here (because `prices.sort` don't return Unit not Array)
}
}
This is different case to this question but hope it help
This can also happen in Android when you read from shared preferences and are getting a (potentially) nullable iterable object back like StringSet. Even when you provide a default, the compiler is not able to determine that the returned value will never actually be null. The only way I've found around this is by asserting that the returned expression is not null using !! operator, like this:
val prefs = PreferenceManager.getDefaultSharedPreferences(appContext)
val searches = prefs.getStringSet("saved_searches", setOf())!!
for (search in searches){
...
}
I want to get rid of the xml declaration when I use libxml2's function xmlSaveFile. How can I accomplish that ?
Is there any macro to do that or can I use another function (I tried with xmlSaveToFilename from xmlsave.h but I do not know how it works) ?
Something like this should work:
xmlSaveCtxtPtr saveCtxt = xmlSaveToFilename(filename, NULL, XML_SAVE_NO_DECL);
if (saveCtxt == NULL) {
// Error handling
}
if (xmlSaveDoc(saveCtxt, doc) < 0) {
// Error handling
}
xmlSaveClose(saveCtxt);
The documentation of the xmlsave module can be found here.
I have a property in my controller that I would like to test:
public List<SelectOption> exampleProperty {
get {
//Do something;
}
}
I am not sure how to cover this code in my test class. Any ideas?
There is direct way, just invoke the property from test method
List<SelectOption> temp = obj.method;
You may need to directly test your properties, especially if you use lazy initialization - a smart pattern for making code efficient and readable.
Here's a list example of this pattern:
Integer[] lotteryNumbers {
get {
if (lotteryNumbers == null) {
lotteryNumbers = new Integer[]{};
}
return lotteryNumbers;
}
set;
}
If you wanted full coverage of the pattern (which may be a good idea while you're getting used to it), you would need to do something like the following:
static testMethod void lotteryNumberFactoryText() {
// test the null case
System.assert(lotteryNumbers.size() == 0);
Integer[] luckyNumbers = new Integer[]{33,8};
lotteryNumbers.addAll(luckyNumbers);
// test the not null case
System.assert(lotteryNumbers == luckyNumbers);
}
First off, do you really want to have an attribute named "method"? Seems a helluva confusing. Anyway, to cover the code, just call
someObject.get(method);
But code coverage should be a side effect of writing good tests - not the goal. You should think about what the code is supposed to do, and write tests to check (i.e. assert) that it is working.
Any of you please advise me on how to overload a list with an associative array in it, using "=" operator. C++/CLI is the laungauge i am using.
I want ot achieve something like this.
`myList[0]["Key"] = "1";`
where myList is
System::Collections::Generic::List<AssociativeArray<String^> ^> ^myList
Can any of you please give me some clues for this.
Thanks in Advance,
Peter K John
Why don't you just use List<Dictionary<String^, String^>^>^?
That syntax is already supported.
If however, Dictionary doesn't meet your needs (e.g. you want a trie structure instead of hashtable), the indexer implementation would look something like this:
generic<typename T>
ref class AssociativeArray
{
// implementation details
// ...
public:
property T default[String^]
{
T get(String^ key)
{
T result;
if (!TryGetValue(key, result)) throw gcnew KeyNotFoundException(key);
return result;
}
void set(String^ key, T val)
{
SetValue(key, val);
}
}
};