Pass two parameters to a function that accepts only one parameter

6

I've been making a code to generate a pdf from the framework Itext in Java. I came across the following situation, I needed to define a phrase like negrito and sublinhado . After searching Google, I found a user's suggestion to use the logical operator ou "|" of short circuito . Because this function in the parameter accepts integer values, it only had only one input.

So the code looks like this.

FontFactory.getFont(FontFactory.defaultEncoding, 10, com.lowagie.text.Font.BOLD | com.lowagie.text.Font.UNDERLINE)

Since this function getFont in this only accept three parameters, and in the third parameter I passed 2 using the "or" short circuit, then I would like to understand the purpose and why the language accepted.

com.lowagie.text.Font.BOLD and com.lowagie.text.Font.UNDERLINE are an enum and respectively have the values 1 and 4 .

    
asked by anonymous 13.09.2014 / 15:20

2 answers

9

What you have encountered is a bitfield and is a common way to represent Boolean parameter sets in a compact way.

The way it works is that each bit (that is, power of two) of the value represents a different formatting category. For example:

NEGRITO    = 1 (decimal) = 0001 (binário)
ITALICO    = 2 (decimal) = 0010 (binário)
SUBLINHADO = 4 (decimal) = 0100 (binário)

Each number then represents a different set, depending on which bits are 1:

6 (decimal) = 0110 (binário) = SUBLINHADO e ITALICO
3 (decimal) = 0011 (binário) = ITALICO e NEGRITO
0 (decimal) = 0000 (binário) = nenhuma formatação

The | operator is the "or binary" operator. For each bit, the answer bit will be 1 if the bit is 1 in at least one of the operands and 0 if it is 0 in both. In your case where numbers represent sets of formatting, | acts as the set joining operator:

0100 | 001 = 0101  -- SUBLINHADO | NEGRITO = (SUBLINHADO e NEGRITO)
0110 | 010 = 0110  -- (SUBLINHADO e ITALICO) | ITALICO = (SUBLINHADO e ITALICO)
    
13.09.2014 / 15:33
5

You are not passing two parameters. There is no way to pass more parameters than the accepted method. You are doing a mathematical expression with two operands and passing the result as a parameter. Here's how:

FontFactory.getFont(FontFactory.defaultEncoding, 10, bold + underline)

This is not the same thing, but shows with arithmetic the exact same shape you used in your example. Let's consider that the variable bold is worth 1, and the underline is worth 2, so you will be passing 3 to the third parameter of this method, only this.

You've probably just met the logical operator | called " logical OR " described in Boolean algebra .

This is a simple way to manipulate bits . You can bind all the necessary bits in an operation. The expected parameters is a byte or sequence of bytes representing several logical states. Each state is a single bit that can obviously be turned on or off. If you search this enumeration com.lowagie.text.Font you will see that its values are exponential (0, 1, 2, 4, 8, 16). And the logical OR is just saying to bind these values. In the background is a sum. If, for example, the BOLD vale 2 e o UNDERLINE 'is 4, the logical OR will ensure that these bits are turned on, resulting in 6.

I will not go into this calculation details, this the hugomg answer already explains and the AP posted a comment link ( Using Bit Flags and EnumSets in Java ). The important thing for your question is that the | operator does not accept more than one parameter (actually the correct name is argument since you are passing, parameter is what you receive), it is only doing a calculation before sending. Another way to see:

int config = com.lowagie.text.Font.BOLD | com.lowagie.text.Font.UNDERLINE;
FontFactory.getFont(FontFactory.defaultEncoding, 10, config);

It is clear that it only has three parameters.

You can read more about the operator on this answer .

    
13.09.2014 / 15:33