I can't search for | in Google. If you had found it in a software source code that you are trying to interpret, you didn't know what it does and you couldn't ask other people for help, how would you find out what it does?
views:
394answers:
7The pipe operator in this case means "use both SWT.APPLICATION_MODAL and SWT.OK as options/flags for my popup box." It's a very commonly used idiom with bitfield configuration identifiers, esp. in windowing systems like SWT or Win32.
How it works
The pipe (|) operator is the bitwise OR operator, that is, it computes an OR operation of the two binary integer values. If you check out where APPLICATION_MODAL and OK are defined, you'll find they are something like this:
...
SWT.OK = 1, // 00000001 in binary
SWT.ABORT_RETRY_IGNORE = 2, // 00000010 in binary
SWT.OK_CANCEL = 4; // 00000100 in binary
...
SWT.APPLICATION_MODAL = 32; // 00100000 in binary
... (and so on...)
When you bitwise OR two (or more) of these numbers together, individual bits will be set for each of the options:
int style = SWT.OK | SWT.APPLICATION_MODAL = 00000001 | 00100000 = 00100001
The windowing toolkit that goes to interpret style will be able to tell exactly what you wanted (a popup box that is Modal and has an OK button) by doing a bitwise AND like this:
...
if(style & SWT.OK)
{
// we want an OK box
}
if(style & SWT.ABORT_RETRY_IGNORE)
{
// we want an Abort/Retry/Ignore box
}
if(style & SWT.OK_CANCEL)
{
// we want an OK/Cancel box
}
...
if(style & SWT.APPLICATION_MODAL)
{
// We want a modal box
}
...
Kinda clever, in my humble opinion. It allows you to select/represent multiple configuration options in a single variable. The trick is in the integer definitions of the options, and ensuring that they are only powers of 2.
| is the OR operator. Those two values are probably flags that are ints with 1 bit set, and the resulting value is which style values to use.
With most things, there's a bit of a learning curve. If I was learning a new language, I would assume that | was an operator, and would search for 'Java operators'.
It is a bitwise OR, it provides a means for you to pass a number of flags to the SWT component as a single int, rather than having to overload the type with loads of setter methods.
The two properties you list indicate you have a dialog or other window that should be displayed modally over the parent shell, and use the ok button. You could combine it with SWT.CANCEL to display an OK and Cancel button for instance
In answer to your question about finding out what it does, I would first reason to myself whether it was a symbol or an operator. That alone helps me figure out what to search for in Google. The types of the variables being used also give me a clue - it's an operator that's working on int types.
Hope that helps.
Unfortunately, Google is lousy at symbols. Instead, I'd start by looking at the Wikipedia page, and search on there for "|", which is at:
- bitwise logic (~, &, |, ^)
and that page has a pretty good overview of what's what and how to use it.