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

io.ebeaninternal.api.CacheIdLookupMany Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebeaninternal.api;

import io.ebeaninternal.server.expression.IdInCommon;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Used for bean cache lookup with where ids in expression.
 */
public final class CacheIdLookupMany implements CacheIdLookup {

  private final IdInCommon idInExpression;
  private int remaining;

  public CacheIdLookupMany(IdInCommon idInExpression) {
    this.idInExpression = idInExpression;
  }

  /**
   * Return the Id values for the in expression.
   */
  @Override
  public Collection idValues() {
    return idInExpression.idValues();
  }

  /**
   * Process the hits returning the beans fetched from cache and
   * adjusting the in expression (to not fetch the hits).
   */
  @Override
  public List removeHits(BeanCacheResult cacheResult) {
    Set hitIds = new HashSet<>();
    List beans = new ArrayList<>();
    for (BeanCacheResult.Entry hit : cacheResult.hits()) {
      hitIds.add(hit.key());
      beans.add(hit.bean());
    }
    this.remaining = idInExpression.removeIds(hitIds);
    return beans;
  }

  @Override
  public boolean allHits() {
    return remaining == 0;
  }
}