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

io.github.oliviercailloux.grade.NormalizingStaticWeighter Maven / Gradle / Ivy

The newest version!
package io.github.oliviercailloux.grade;

import static io.github.oliviercailloux.grade.MarkAggregator.checkCanAggregate;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.Set;

/**
 * If some criteria are missing in the given marks, weights are re-normalized accordingly.
 */
public final class NormalizingStaticWeighter extends AbstractStaticWeighter
    implements MarkAggregator {

  public static NormalizingStaticWeighter given(Map weights) {
    return new NormalizingStaticWeighter(weights);
  }

  protected NormalizingStaticWeighter(Map weights) {
    super(weights);
  }

  @Override
  public ImmutableMap weightsFromCriteria(Set criteria)
      throws AggregatorException {
    checkCanAggregate(criteria.stream().allMatch(weights::containsKey), "Unknown criterion");

    final double sum = criteria.stream().mapToDouble(weights::get).sum();
    return criteria.stream()
        .collect(ImmutableMap.toImmutableMap(c -> c, c -> weights.get(c) / sum));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy