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

edu.isi.nlp.ExplicitOrderingNonExclusive Maven / Gradle / Ivy

The newest version!
package edu.isi.nlp;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Ordering;
import java.util.List;
import javax.annotation.Nullable;
import org.immutables.value.Value;

/** For documentation, see {@link OrderingUtils#explicitOrderingUnrankedLast(List)} and friends. */
@IsiNlpImmutable
@Value.Immutable
abstract class ExplicitOrderingNonExclusive extends Ordering {

  abstract ImmutableMap rankMap();

  @Value.Default
  boolean unrankedIsFirst() {
    return true;
  }

  @Override
  public final int compare(@Nullable final T left, @Nullable final T right) {
    final Integer leftRank = rankMap().get(left);
    final Integer rightRank = rankMap().get(right);

    if (leftRank != null && rightRank != null) {
      return leftRank - rightRank; // safe because both non-negative
    } else if (leftRank != null) {
      if (unrankedIsFirst()) {
        return 1;
      } else {
        return -1;
      }
    } else if (rightRank != null) {
      if (unrankedIsFirst()) {
        return -1;
      } else {
        return 1;
      }
    } else {
      // if an ordering is not specified between two items, they are considered equal
      return 0;
    }
  }

  @Override
  public final String toString() {
    final String unrankedString = unrankedIsFirst() ? "first" : "last";
    return "explicitOrdering(" + rankMap().keySet() + ", unranked=" + unrankedString + ")";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy