Skip to content

Commit

Permalink
Beans in a bean container are now only observed if there is one exter…
Browse files Browse the repository at this point in the history
…nal observer at least
  • Loading branch information
SimonDan committed Feb 15, 2020
1 parent 70f0932 commit 7c24c8a
Showing 1 changed file with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.*;
import java.util.stream.IntStream;

/**
* The encapsulated bean container data implementation based on a data source.
Expand All @@ -30,6 +30,7 @@ class EncapsulatedBeanContainerData<BEAN extends IBean<BEAN>> extends AbstractEn
private final Optional<IStatisticData<Integer>> statisticData;
private final Map<BEAN, Disposable> beanDisposableMapping = new ConcurrentHashMap<>();
private final IndexChecker indexChecker = IndexChecker.create(this::size);
private boolean observingActive = false;

/**
* Creates the encapsulated bean container data core.
Expand All @@ -42,8 +43,6 @@ class EncapsulatedBeanContainerData<BEAN extends IBean<BEAN>> extends AbstractEn
super(pDataSource);
beanType = pBeanType;
statisticData = _tryCreateStatisticData();
//Observe all initial beans in the container
StreamSupport.stream(pDataSource.spliterator(), false).forEach(this::_observeBean);
}

@Override
Expand All @@ -65,7 +64,10 @@ public void addBean(BEAN pBean, int pIndex)
pIndex--;
}

getDatasource().addBean(_observeBean(pBean), pIndex);
if (observingActive)
_observeBean(pBean);

getDatasource().addBean(pBean, pIndex);
}

@Override
Expand All @@ -79,7 +81,9 @@ public BEAN replaceBean(BEAN pReplacement, int pIndex)
@Override
public boolean removeBean(BEAN pBean)
{
beanDisposableMapping.remove(pBean).dispose();
if (observingActive)
beanDisposableMapping.remove(pBean).dispose();

return getDatasource().removeBean(pBean);
}

Expand Down Expand Up @@ -122,6 +126,7 @@ public void setLimit(int pMaxCount, boolean pEvicting)
if (diffToMany > 0)
IntStream.range(0, diffToMany).forEach(pIndex -> removeBean(0));
}

limitInfo = pMaxCount < 0 ? null : new _LimitInfo(pMaxCount, pEvicting);
}

Expand All @@ -138,6 +143,19 @@ public Iterator<BEAN> iterator()
return getDatasource().iterator();
}

@Override
public <EVENT extends IEvent<?>> Observable<EVENT> observeByType(Class<EVENT> pEventType)
{
if (!observingActive)
{
//Observe all beans in the container
stream().forEach(this::_observeBean);
observingActive = true;
}

return super.observeByType(pEventType);
}

/**
* Tries to create the statistic data for this encapsulated data core.
* This data is an amount of timestamps with an associated number,
Expand All @@ -164,6 +182,7 @@ private BEAN _observeBean(BEAN pBean)
//noinspection unchecked
final Disposable disposable = combinedObservables
.subscribe(pChangeEvent -> getEventObserverFromType((Class<IEvent<BEAN>>) pChangeEvent.getClass()).onNext(pChangeEvent));

beanDisposableMapping.put(pBean, disposable);
return pBean;
}
Expand Down

0 comments on commit 7c24c8a

Please sign in to comment.