
it.tidalwave.role.ui.impl.PresentationModelComposite Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Stats
* Copyright (C) 2011-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile/
* SCM: http://java.net/hg/bluebill-server~stats-src
*
**********************************************************************************************************************/
package it.tidalwave.role.ui.impl;
import lombok.Getter;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import it.tidalwave.util.As;
import it.tidalwave.util.AsException;
import it.tidalwave.util.Finder;
import it.tidalwave.util.spi.FinderSupport;
import it.tidalwave.role.Composite;
import it.tidalwave.role.SimpleComposite;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.util.NotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.role.ui.Presentable.Presentable;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@RequiredArgsConstructor @Slf4j
/* package */ class PresentationModelComposite implements SimpleComposite
{
private final SortedMap presentationModelMapByIndex = new TreeMap();
@Nonnull
private final Composite> delegateComposite;
@Getter
private boolean countComputed;
private int count;
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override @Nonnull
public Finder findChildren()
{
return new FinderSupport>()
{
@Override
public int count()
{
return Math.min(maxResults, computeCount() - firstResult);
}
@Override @Nonnull
protected List extends PresentationModel> computeNeededResults()
{
final List result = new ArrayList();
int i = 0;
try
{
for (; i < count(); i++)
{
result.add(NotFoundException.throwWhenNull(presentationModelMapByIndex.get(firstResult + i), ""));
}
}
catch (NotFoundException e) // some objects not in the cache
{
final List> objects = delegateComposite.findChildren().from(firstResult).max(maxResults).results();
for (; i < count(); i++)
{
result.add(getPresentationModel(firstResult + i, objects.get(i)));
}
}
return result;
}
};
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private synchronized PresentationModel getPresentationModel (final @Nonnegative int index,
final @Nonnull Object object)
{
PresentationModel presentationModel = presentationModelMapByIndex.get(index);
if (presentationModel == null)
{
presentationModel = createPresentationModelFor(object);
presentationModelMapByIndex.put(index, presentationModel);
}
return presentationModel;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private int computeCount()
{
if (!countComputed)
{
log.debug(">>>> computing children count...");
count = delegateComposite.findChildren().count();
countComputed = true;
}
return count;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private PresentationModel createPresentationModelFor (final @Nonnull Object object)
{
try
{
if (!(object instanceof As))
{
throw new AsException(As.class);
}
return ((As)object).as(Presentable).createPresentationModel();
}
catch (AsException e)
{
return new AsLookupPresentationModel(object); // FIXME: use PresentationModelFactory
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy