Пример #1
0
  /**
   * If pictures is not null, add a list of images of the given primary type to the described
   * object. If no images of the primary type are found, fall back to the first, or optional second
   * type. Only one (the first) fallback image will be used. Ordering is preserved for images. If
   * pictures is null, this method does nothing.
   *
   * @param pictures The picture collection to use
   * @param described The object that will have images added
   * @param primaryImageType The primary image type to add
   * @param firstFallbackType The preferred fallback image type to add
   * @param secondFallbackType The optional fallback image type to add
   */
  void selectImages(
      Pictures pictures,
      Described described,
      String primaryImageType,
      String firstFallbackType,
      Maybe<String> secondFallbackType) {
    if (pictures != null) {
      Set<Image> images = Sets.newLinkedHashSet();
      Image fallbackImage = null;
      boolean hasFirstFallbackType = false;

      for (PictureUsage picture : pictures.getPictureUsage()) {
        Image image = createImage(picture);

        if (secondFallbackType.hasValue()
            && picture.getType().equals(secondFallbackType.requireValue())
            && images.isEmpty()
            && fallbackImage == null) {
          setPrimaryImage(described, image, picture);
          fallbackImage = image;
        }
        if (picture.getType().equals(firstFallbackType)
            && images.isEmpty()
            && !hasFirstFallbackType) {
          setPrimaryImage(described, image, picture);
          fallbackImage = image;
          hasFirstFallbackType = true;
        }
        if (picture.getType().equals(primaryImageType)) {
          if (images.size() == 0) {
            setPrimaryImage(described, image, picture);
          }
          images.add(image);
        }
      }

      if (!images.isEmpty()) {
        described.setImages(images);
      } else if (fallbackImage != null) {
        described.setImages(ImmutableSet.of(fallbackImage));
      }
    }
  }