/**
   * Indicates if the current variant includes the given variant.
   *
   * @param other The other variant.
   * @return True if the current variant includes the other.
   */
  public boolean includes(Variant other) {
    boolean result = other != null;

    // Compare the character set
    if (result) {
      result = (getCharacterSet() == null) || getCharacterSet().includes(other.getCharacterSet());
    }

    // Compare the media type
    if (result) {
      result = (getMediaType() == null) || getMediaType().includes(other.getMediaType());
    }

    // Compare the languages
    if (result) {
      result =
          (getLanguages().isEmpty())
              || getLanguages().contains(Language.ALL)
              || getLanguages().containsAll(other.getLanguages());
    }

    // Compare the encodings
    if (result) {
      result =
          (getEncodings().isEmpty())
              || getEncodings().contains(Encoding.ALL)
              || getEncodings().containsAll(other.getEncodings());
    }

    return result;
  }
Beispiel #2
0
  /**
   * Scores a variant relatively to enriched client preferences.
   *
   * @param variant The variant to score.
   * @return The enriched client preferences.
   */
  public float scoreVariant(Variant variant) {
    float result = -1.0F;
    float languageScore = scoreLanguages(variant.getLanguages());

    if (languageScore != -1.0F) {
      float mediaTypeScore = scoreMediaType(variant.getMediaType());

      if (mediaTypeScore != -1.0F) {
        float characterSetScore = scoreCharacterSet(variant.getCharacterSet());

        if (characterSetScore != -1.0F) {
          float encodingScore = scoreEncodings(variant.getEncodings());

          if (encodingScore != -1.0F) {
            // Return the weighted average score
            result =
                ((languageScore * 4.0F)
                        + (mediaTypeScore * 3.0F)
                        + (characterSetScore * 2.0F)
                        + encodingScore)
                    / 9.0F;
          }
        }
      }
    }

    return result;
  }
 /**
  * Constructor from a variant.
  *
  * @param variant The variant to copy.
  * @param modificationDate The modification date.
  * @param tag The tag.
  */
 public Representation(Variant variant, Date modificationDate, Tag tag) {
   setCharacterSet(variant.getCharacterSet());
   setEncodings(variant.getEncodings());
   setLocationRef(variant.getLocationRef());
   setLanguages(variant.getLanguages());
   setMediaType(variant.getMediaType());
   setModificationDate(modificationDate);
   setTag(tag);
 }
  /**
   * Indicates if the current variant is equal to the given variant.
   *
   * @param other The other variant.
   * @return True if the current variant includes the other.
   */
  @Override
  public boolean equals(Object other) {
    boolean result = (other instanceof Variant);

    if (result && (other != this)) {
      Variant otherVariant = (Variant) other;

      // Compare the character set
      if (result) {
        result =
            ((getCharacterSet() == null) && (otherVariant.getCharacterSet() == null)
                || (getCharacterSet() != null)
                    && getCharacterSet().equals(otherVariant.getCharacterSet()));
      }

      // Compare the media type
      if (result) {
        result =
            ((getMediaType() == null) && (otherVariant.getMediaType() == null)
                || (getMediaType() != null) && getMediaType().equals(otherVariant.getMediaType()));
      }

      // Compare the languages
      if (result) {
        result = getLanguages().equals(otherVariant.getLanguages());
      }

      // Compare the encodings
      if (result) {
        result = getEncodings().equals(otherVariant.getEncodings());
      }

      // Compare the location URI
      if (result) {
        result =
            ((getLocationRef() == null) && (otherVariant.getLocationRef() == null)
                || (getLocationRef() != null)
                    && getLocationRef().equals(otherVariant.getLocationRef()));
      }
    }

    return result;
  }