author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Thu, 30 Oct 2014 21:30:10 +0100 | |
changeset 409 | 40cabcdcd2be |
parent 304 | adb083bd81c5 |
permissions | -rw-r--r-- |
jtulach@303 | 1 |
/* |
jtulach@303 | 2 |
* To change this template, choose Tools | Templates |
jtulach@303 | 3 |
* and open the template in the editor. |
jtulach@303 | 4 |
*/ |
jtulach@303 | 5 |
|
jtulach@303 | 6 |
package org.apidesign.codeinjection; |
jtulach@303 | 7 |
|
jtulach@303 | 8 |
import java.util.Iterator; |
jtulach@303 | 9 |
import java.util.ServiceLoader; |
jtulach@303 | 10 |
import org.apidesign.codeinjection.spi.CountDownExtender; |
jtulach@303 | 11 |
|
jtulach@303 | 12 |
/** Second version to {@link CountDownImplV1} - imagine someone wants to |
jtulach@303 | 13 |
* reimplement {@link #down()} method to decrement by two not just by one. |
jtulach@303 | 14 |
* You do not like that idea at all, but you do not want to prevent such |
jtulach@303 | 15 |
* behaviour. You just do not want to maintain such bloody code. What can |
jtulach@303 | 16 |
* you do? You need to allow <b>code injection</b> via the |
jtulach@303 | 17 |
* {@link CountDownExtender} class! |
jtulach@303 | 18 |
* |
jtulach@303 | 19 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jtulach@303 | 20 |
*/ |
jtulach@303 | 21 |
final class CountDownImplV2 extends CountDown { |
jtulach@303 | 22 |
private int cnt; |
jtulach@303 | 23 |
|
jtulach@303 | 24 |
CountDownImplV2(int initial) { |
jtulach@303 | 25 |
this.cnt = initial; |
jtulach@303 | 26 |
} |
jtulach@303 | 27 |
|
jtulach@303 | 28 |
// BEGIN: codeinjection.v2 |
jtulach@303 | 29 |
public void down() { |
jtulach@397 | 30 |
Iterator<CountDownExtender> it = |
jtulach@397 | 31 |
ServiceLoader.load(CountDownExtender.class).iterator(); |
jtulach@303 | 32 |
if (it.hasNext()) { |
jtulach@303 | 33 |
// injected behaviour |
jtulach@303 | 34 |
cnt = it.next().decrement(cnt); |
jtulach@303 | 35 |
} else { |
jtulach@303 | 36 |
// common behaviour of 1.0 version |
jtulach@303 | 37 |
cnt--; |
jtulach@303 | 38 |
} |
jtulach@303 | 39 |
} |
jtulach@303 | 40 |
|
jtulach@303 | 41 |
public boolean isDown() { |
jtulach@303 | 42 |
return cnt <= 0; |
jtulach@303 | 43 |
} |
jtulach@303 | 44 |
// END: codeinjection.v2 |
jtulach@303 | 45 |
} |