diff -r 000000000000 -r 77b6002451c4 samples/codeinjection/src/org/apidesign/codeinjection/CountDownImplV2.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/codeinjection/src/org/apidesign/codeinjection/CountDownImplV2.java Sat Jan 10 21:29:22 2009 +0100 @@ -0,0 +1,44 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.apidesign.codeinjection; + +import java.util.Iterator; +import java.util.ServiceLoader; +import org.apidesign.codeinjection.spi.CountDownExtender; + +/** Second version to {@link CountDownImplV1} - imagine someone wants to + * reimplement {@link #down()} method to decrement by two not just by one. + * You do not like that idea at all, but you do not want to prevent such + * behaviour. You just do not want to maintain such bloody code. What can + * you do? You need to allow code injection via the + * {@link CountDownExtender} class! + * + * @author Jaroslav Tulach + */ +final class CountDownImplV2 extends CountDown { + private int cnt; + + CountDownImplV2(int initial) { + this.cnt = initial; + } + +// BEGIN: codeinjection.v2 + public void down() { + Iterator it = ServiceLoader.load(CountDownExtender.class).iterator(); + if (it.hasNext()) { + // injected behaviour + cnt = it.next().decrement(cnt); + } else { + // common behaviour of 1.0 version + cnt--; + } + } + + public boolean isDown() { + return cnt <= 0; + } +// END: codeinjection.v2 +}