jtulach@303: /*
jtulach@303: * To change this template, choose Tools | Templates
jtulach@303: * and open the template in the editor.
jtulach@303: */
jtulach@303:
jtulach@303: package org.apidesign.codeinjection;
jtulach@303:
jtulach@303: import java.util.Iterator;
jtulach@303: import java.util.ServiceLoader;
jtulach@303: import org.apidesign.codeinjection.spi.CountDownExtender;
jtulach@303:
jtulach@303: /** Second version to {@link CountDownImplV1} - imagine someone wants to
jtulach@303: * reimplement {@link #down()} method to decrement by two not just by one.
jtulach@303: * You do not like that idea at all, but you do not want to prevent such
jtulach@303: * behaviour. You just do not want to maintain such bloody code. What can
jtulach@303: * you do? You need to allow code injection via the
jtulach@303: * {@link CountDownExtender} class!
jtulach@303: *
jtulach@303: * @author Jaroslav Tulach
jtulach@303: */
jtulach@303: final class CountDownImplV2 extends CountDown {
jtulach@303: private int cnt;
jtulach@303:
jtulach@303: CountDownImplV2(int initial) {
jtulach@303: this.cnt = initial;
jtulach@303: }
jtulach@303:
jtulach@303: // BEGIN: codeinjection.v2
jtulach@303: public void down() {
jtulach@397: Iterator it =
jtulach@397: ServiceLoader.load(CountDownExtender.class).iterator();
jtulach@303: if (it.hasNext()) {
jtulach@303: // injected behaviour
jtulach@303: cnt = it.next().decrement(cnt);
jtulach@303: } else {
jtulach@303: // common behaviour of 1.0 version
jtulach@303: cnt--;
jtulach@303: }
jtulach@303: }
jtulach@303:
jtulach@303: public boolean isDown() {
jtulach@303: return cnt <= 0;
jtulach@303: }
jtulach@303: // END: codeinjection.v2
jtulach@303: }