All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.aries.component.dsl.OSGi Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.apache.aries.component.dsl;

import org.apache.aries.component.dsl.function.Function10;
import org.apache.aries.component.dsl.function.Function14;
import org.apache.aries.component.dsl.function.Function16;
import org.apache.aries.component.dsl.function.Function19;
import org.apache.aries.component.dsl.function.Function2;
import org.apache.aries.component.dsl.function.Function20;
import org.apache.aries.component.dsl.function.Function25;
import org.apache.aries.component.dsl.function.Function4;
import org.apache.aries.component.dsl.function.Function6;
import org.apache.aries.component.dsl.function.Function8;
import org.apache.aries.component.dsl.function.Function9;
import org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl;
import org.apache.aries.component.dsl.internal.EffectsOSGi;
import org.apache.aries.component.dsl.internal.NothingOSGiImpl;
import org.apache.aries.component.dsl.internal.Pad;
import org.apache.aries.component.dsl.internal.ServiceReferenceOSGi;
import org.apache.aries.component.dsl.internal.ServiceRegistrationOSGiImpl;
import org.apache.aries.component.dsl.function.Function11;
import org.apache.aries.component.dsl.function.Function12;
import org.apache.aries.component.dsl.function.Function13;
import org.apache.aries.component.dsl.function.Function15;
import org.apache.aries.component.dsl.function.Function17;
import org.apache.aries.component.dsl.function.Function18;
import org.apache.aries.component.dsl.function.Function21;
import org.apache.aries.component.dsl.function.Function22;
import org.apache.aries.component.dsl.function.Function23;
import org.apache.aries.component.dsl.function.Function24;
import org.apache.aries.component.dsl.function.Function26;
import org.apache.aries.component.dsl.function.Function3;
import org.apache.aries.component.dsl.function.Function5;
import org.apache.aries.component.dsl.function.Function7;
import org.apache.aries.component.dsl.internal.BundleContextOSGiImpl;
import org.apache.aries.component.dsl.internal.BundleOSGi;
import org.apache.aries.component.dsl.internal.ChangeContextOSGiImpl;
import org.apache.aries.component.dsl.internal.ConcurrentDoublyLinkedList;
import org.apache.aries.component.dsl.internal.ConfigurationsOSGiImpl;
import org.apache.aries.component.dsl.internal.AllOSGi;
import org.apache.aries.component.dsl.internal.IgnoreImpl;
import org.apache.aries.component.dsl.internal.JustOSGiImpl;
import org.apache.aries.component.dsl.internal.OSGiImpl;
import org.apache.aries.component.dsl.internal.OnCloseOSGiImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceObjects;
import org.osgi.framework.ServiceRegistration;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author Carlos Sierra Andrés
 */
public interface OSGi extends OSGiRunnable {
	OSGiResult NOOP = () -> {};

	@SafeVarargs
	static  OSGi all(OSGi ... programs) {
		return new AllOSGi<>(programs);
	}

	static OSGi bundleContext() {
		return new BundleContextOSGiImpl();
	}

	static OSGi bundles(int stateMask) {
		return new BundleOSGi(stateMask);
	}

	static  OSGi changeContext(
		BundleContext bundleContext, OSGi program) {

		return new ChangeContextOSGiImpl<>(program, bundleContext);
	}

	static  OSGi combine(Function2 fun, OSGi a, OSGi b) {
		return b.applyTo(a.applyTo(just(fun.curried())));
	}

	static  OSGi combine(Function3 fun, OSGi a, OSGi b, OSGi c) {
		return c.applyTo(combine((A aa, B bb) -> fun.curried().apply(aa).apply(bb), a, b));
	}

	static  OSGi combine(Function4 fun, OSGi a, OSGi b, OSGi c, OSGi d) {
		return d.applyTo(combine((A aa, B bb, C cc) -> fun.curried().apply(aa).apply(bb).apply(cc), a, b, c));
	}

	static  OSGi combine(Function5 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e) {
		return e.applyTo(combine((A aa, B bb, C cc, D dd) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd), a, b, c, d));
	}

	static  OSGi combine(Function6 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f) {
		return f.applyTo(combine((A aa, B bb, C cc, D dd, E ee) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee), a, b, c, d, e));
	}

	static  OSGi combine(Function7 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g) {
		return g.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff), a, b, c, d, e, f));
	}

	static  OSGi combine(Function8 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h) {
		return h.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg), a, b, c, d, e, f, g));
	}

	static  OSGi combine(Function9 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i) {
		return i.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh), a, b, c, d, e, f, g, h));
	}

	static  OSGi combine(Function10 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j) {
		return j.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii), a, b, c, d, e, f, g, h, i));
	}

	static  OSGi combine(Function11 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k) {
		return k.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj), a, b, c, d, e, f, g, h, i, j));
	}

	static  OSGi combine(Function12 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l) {
		return l.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk), a, b, c, d, e, f, g, h, i, j, k));
	}

	static  OSGi combine(Function13 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m) {
		return m.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll), a, b, c, d, e, f, g, h, i, j, k, l));
	}

	static  OSGi combine(Function14 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n) {
		return n.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm), a, b, c, d, e, f, g, h, i, j, k, l, m));
	}

	static  OSGi combine(Function15 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o) {
		return o.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn), a, b, c, d, e, f, g, h, i, j, k, l, m, n));
	}

	static  OSGi combine(Function16 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q) { return q.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)); } static OSGi combine(Function18 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r) { return r.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)); } static OSGi combine(Function19 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s) { return s.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)); } static OSGi combine(Function20 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t) { return t.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)); } static OSGi combine(Function21 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t, OSGi u) { return u.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)); } static OSGi combine(Function22 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t, OSGi u, OSGi v) { return v.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)); } static OSGi combine(Function23 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t, OSGi u, OSGi v, OSGi w) { return w.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)); } static OSGi combine(Function24 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t, OSGi u, OSGi v, OSGi w, OSGi x) { return x.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv, W ww) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv).apply(ww), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w)); } static OSGi combine(Function25 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t, OSGi u, OSGi v, OSGi w, OSGi x, OSGi y) { return y.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv, W ww, X xx) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv).apply(ww).apply(xx), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x)); } static OSGi combine(Function26 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi

p, OSGi q, OSGi r, OSGi s, OSGi t, OSGi u, OSGi v, OSGi w, OSGi x, OSGi y, OSGi z) { return z.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo, P pp, Q qq, R rr, S ss, T tt, U uu, V vv, W ww, X xx, Y yy) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo).apply(pp).apply(qq).apply(rr).apply(ss).apply(tt).apply(uu).apply(vv).apply(ww).apply(xx).apply(yy), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y)); } static OSGi> configuration(String pid) { return new ConfigurationOSGiImpl(pid); } static OSGi> configurations(String factoryPid) { return new ConfigurationsOSGiImpl(factoryPid); } static OSGi effects(Runnable onAdding, Runnable onRemoving) { return new EffectsOSGi(onAdding, onRemoving); } static OSGi effect(Effect effect) { return new EffectsOSGi( () -> effect.getOnIncoming().accept(null), () -> effect.getOnLeaving().accept(null)); } static OSGi ignore(OSGi program) { return new IgnoreImpl(program); } static OSGi join(OSGi> program) { return program.flatMap(x -> x); } static OSGi just(S s) { return new JustOSGiImpl<>(s); } static OSGi just(Collection s) { return new JustOSGiImpl<>(s); } static OSGi just(Supplier s) { return new JustOSGiImpl<>(() -> Collections.singletonList(s.get())); } static OSGi nothing() { return new NothingOSGiImpl<>(); } static OSGi onClose(Runnable action) { return new OnCloseOSGiImpl(action); } static OSGi once(OSGi program) { return program.transform(op -> { AtomicInteger count = new AtomicInteger(); AtomicReference terminator = new AtomicReference<>(); return t -> { if (count.getAndIncrement() == 0) { terminator.set(op.apply(t)); } return () -> { if (count.decrementAndGet() == 0) { Runnable runnable = terminator.getAndSet(NOOP); runnable.run(); } }; }; }); } static OSGi> prototypes(String filterString) { return prototypes(null, filterString); } static OSGi> prototypes(Class clazz) { return prototypes(clazz, null); } static OSGi> prototypes( Class clazz, String filterString) { return bundleContext().flatMap( bundleContext -> serviceReferences(clazz, filterString).map( CachingServiceReference::getServiceReference ).map( bundleContext::getServiceObjects) ); } static OSGi> register( Class clazz, T service, Map properties) { return register(clazz, () -> service, () -> properties); } static OSGi> register( Class clazz, ServiceFactory service, Map properties) { return register(clazz, service, () -> properties); } static OSGi> register( String[] classes, Object service, Map properties) { return new ServiceRegistrationOSGiImpl( classes, () -> service, () -> properties); } static OSGi> register( Class clazz, Supplier service, Supplier> properties) { return new ServiceRegistrationOSGiImpl<>(clazz, service, properties); } static OSGi> register( Class clazz, ServiceFactory service, Supplier> properties) { return new ServiceRegistrationOSGiImpl<>(clazz, service, properties); } static OSGi> register( String[] classes, Supplier service, Supplier> properties) { return new ServiceRegistrationOSGiImpl(classes, service, properties); } static OSGi> serviceReferences( Class clazz) { return new ServiceReferenceOSGi<>(null, clazz); } static OSGi> serviceReferences( String filterString) { return new ServiceReferenceOSGi<>(filterString, null); } static OSGi> serviceReferences( Class clazz, String filterString) { return new ServiceReferenceOSGi<>(filterString, clazz); } static OSGi> serviceReferences( Class clazz, String filterString, Refresher> onModified) { return new ServiceReferenceOSGi<>(filterString, clazz, onModified); } static OSGi> serviceReferences( Class clazz, Refresher> onModified) { return new ServiceReferenceOSGi<>(null, clazz, onModified); } static OSGi> serviceReferences( String filterString, Refresher> onModified) { return new ServiceReferenceOSGi<>(filterString, null, onModified); } static OSGi services(Class clazz) { return services(clazz, null); } static OSGi services(String filterString) { return services(null, filterString); } static OSGi services(Class clazz, String filterString) { return bundleContext().flatMap( bundleContext -> serviceReferences(clazz, filterString).map( CachingServiceReference::getServiceReference ).flatMap( sr -> { T service = bundleContext.getService(sr); return onClose(() -> bundleContext.ungetService(sr)).then( just(service) ); } )); } default OSGi applyTo(OSGi> fun) { return fromOsgiRunnable((bundleContext, op) -> { ConcurrentDoublyLinkedList identities = new ConcurrentDoublyLinkedList<>(); ConcurrentDoublyLinkedList> funs = new ConcurrentDoublyLinkedList<>(); OSGiResult myResult = run( bundleContext, t -> { ConcurrentDoublyLinkedList.Node node = identities.addLast(t); List terminators = funs.stream().map( f -> op.apply(f.apply(t)) ).collect( Collectors.toList() ); return () -> { node.remove(); terminators.forEach(Runnable::run); }; } ); OSGiResult funRun = fun.run( bundleContext, f -> { ConcurrentDoublyLinkedList.Node node = funs.addLast(f); List terminators = identities.stream().map( t -> op.apply(f.apply(t)) ).collect( Collectors.toList() ); return () -> { node.remove(); terminators.forEach(Runnable::run); }; }); return () -> { myResult.close(); funRun.close(); }; }); } default OSGi choose( Function> chooser, Function, OSGi> then, Function, OSGi> otherwise) { return fromOsgiRunnable((bundleContext, publisher) -> { Pad thenPad = new Pad<>(bundleContext, then, publisher); Pad elsePad = new Pad<>(bundleContext, otherwise, publisher); OSGiResult result = run( bundleContext, t -> chooser.apply(t).run( bundleContext, b -> { if (b) { return thenPad.publish(t); } else { return elsePad.publish(t); } } )); return () -> { thenPad.close(); elsePad.close(); result.close(); }; }); } default OSGi distribute(Function, OSGi> ... funs) { return fromOsgiRunnable((bundleContext, publisher) -> { List> pads = Arrays.stream( funs ).map( fun -> new Pad<>(bundleContext, fun, publisher) ).collect( Collectors.toList() ); OSGiResult result = run( bundleContext, t -> { List terminators = pads.stream().map(p -> p.publish(t)).collect( Collectors.toList()); return () -> terminators.forEach(Runnable::run); }); return () -> { result.close(); pads.forEach(Pad::close); }; }); } default OSGi effects( Consumer onAdded, Consumer onRemoved) { return fromOsgiRunnable((bundleContext, op) -> run( bundleContext, t -> { onAdded.accept(t); Runnable terminator; try { terminator = op.apply(t); } catch (Exception e) { onRemoved.accept(t); throw e; } return () -> { onRemoved.accept(t); terminator.run(); }; })); } default OSGi effects(Effect effect) { return effects(effect.getOnIncoming(), effect.getOnLeaving()); } default OSGi filter(Predicate predicate) { return fromOsgiRunnable((bundleContext, op) -> run( bundleContext, t -> { if (predicate.test(t)) { return op.apply(t); } else { return NOOP; } } )); } default OSGi flatMap(Function> fun) { return fromOsgiRunnable((bundleContext, op) -> run(bundleContext, t -> fun.apply(t).run(bundleContext, op)) ); } default OSGi foreach(Consumer onAdded) { return foreach(onAdded, __ -> {}); } default OSGi foreach( Consumer onAdded, Consumer onRemoved) { return ignore(effects(onAdded, onRemoved)); } default OSGi map(Function function) { return fromOsgiRunnable((bundleContext, op) -> run(bundleContext, t -> op.apply(function.apply(t))) ); } default OSGi recover(BiFunction onError) { return fromOsgiRunnable((bundleContext, op) -> run( bundleContext, t -> { try { return op.apply(t); } catch (Exception e) { return op.apply(onError.apply(t, e)); } } )); } default OSGi recoverWith(BiFunction> onError) { return fromOsgiRunnable((bundleContext, op) -> run( bundleContext, t -> { try { return op.apply(t); } catch (Exception e) { return onError.apply(t, e).run(bundleContext, op); } } )); } default OSGi splitBy( Function> mapper, BiFunction, OSGi> fun) { return fromOsgiRunnable((bundleContext, op) -> { HashMap> pads = new HashMap<>(); OSGiResult result = run( bundleContext, t -> mapper.apply(t).run( bundleContext, k -> pads.computeIfAbsent( k, __ -> new Pad<>( bundleContext, ___ -> fun.apply(k, ___), op) ).publish(t) ) ); return () -> { pads.values().forEach(Pad::close); result.close(); }; }); } default public OSGi then(OSGi next) { return flatMap(__ -> next); } default OSGi transform(Transformer fun) { return fromOsgiRunnable( (bundleContext, op) -> run(bundleContext, fun.transform(op))); } static OSGi fromOsgiRunnable(OSGiRunnable runnable) { return getOsgiFactory().create(runnable); } static OSGiFactory getOsgiFactory() { return OSGiImpl::create; } }









p) { return p.applyTo(combine((A aa, B bb, C cc, D dd, E ee, F ff, G gg, H hh, I ii, J jj, K kk, L ll, M mm, N nn, O oo) -> fun.curried().apply(aa).apply(bb).apply(cc).apply(dd).apply(ee).apply(ff).apply(gg).apply(hh).apply(ii).apply(jj).apply(kk).apply(ll).apply(mm).apply(nn).apply(oo), a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)); } static OSGi combine(Function17 fun, OSGi a, OSGi b, OSGi c, OSGi d, OSGi e, OSGi f, OSGi g, OSGi h, OSGi i, OSGi j, OSGi k, OSGi l, OSGi m, OSGi n, OSGi o, OSGi