PatternOrString interface source

Asked by scprotz

Hi again,

So I've been reading through the source code and I see reference to PatternOrString. I tend to use Eclipse for everything (Kepler today), and it can't find the PatternOrString interface declaration, even after a brute search for the string. I really just find it in the Region class only.

Is this some kind of SCALA interface that has been tacked on to the Java jar file - I didn't see it in the referenced libraries? or is there a bit of source code I'm missing? or is this some Java magic I've never come across in my years of Java programming - maybe some trick with Generics?

Thanks for insight
-scprotz

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
scprotz
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Not a trick, but some defined Java language construct (should be from Generics stuff), to avoid Object parameters and be more declarative.

So instead of saying:
public someMethod(Object parm) { }

you can use
public <PatternOrString> void someMethod(PatternOrString parm) { }

Nevertheless you have to cast parm to the correct type inside after having evaluated the type somehow (e.g. with instanceof)

Be Aware: Do not dive too deep into version 1.0.1 though, with version 1.1.0 things are changing dramatically inside at some places (e.g. class Region)

Revision history for this message
scprotz (scprotz) said :
#2

Is 1.1.0 fleshed out enough to use as an early adopter (Is it at a Beta stage yet?) If so, I might just go ahead and transition if it is usable.

Revision history for this message
scprotz (scprotz) said :
#3

And as for the "OR", if you know any java documentation on this, I've never seen it and would like to read up on it.

Thanks so much
-scprotz

Revision history for this message
RaiMan (raimund-hocke) said :
#4

--at comment #3
It is really simply Generics<T>, where you can use anything you want as T (at least a valid Java name), in this case we use the text PatternOrString, to say:
the parameter might be a an object of type Pattern or an object of type String (with the "promise": we will check it internally and complain if it is neither String nor Pattern)

We might have used <PleaseUseOnlyPatternOrStringBecauseThisIsWhatIsExpected>

or <YouMightGuessWhatParameterTypesAreAllowed> ;-) but this would not be fair for someone looking at the method signature ;-)

So the Or is simply a meaningful word in this context, no operator or something like this (you might have thought off ;-)

--at comment #2:
You might look at and try to work with version 1.1.0 from the GitHub repo: https://github.com/RaiMan/SikuliX-2014

This only makes sense if you are willing to use Maven.

If not, you have to wait:
I will start with Beta versions in a few days on launchpad (sikuli-setup-1.1.jar, usage similar to version 1.0.1)

Revision history for this message
scprotz (scprotz) said :
#5

Thanks. I didn't see the obvious with the <generics> naming. I was thinking there was some magic "Or" operator, but it makes complete sense now (I guess people get used to seeing <T> and forget that is just convention, not set in stone).

Thanks, and I'll consider working with the Maven version of 1.1.0 to get a jumpstart on it.