AMAttributeDistanceComparator implements AMDistanceComparator<AdaptiveMedia<AMImageProcessor>> { @Override public long compare( AdaptiveMedia<AMImageProcessor> adaptiveMedia1, AdaptiveMedia<AMImageProcessor> adaptiveMedia2) { for (Map.Entry <AMAttribute<AMImageProcessor, Object>, AMImageQueryBuilder.SortOrder> sortCriterion : _sortCriteria.entrySet()) { AMAttribute<AMImageProcessor, Object> amAttribute = sortCriterion.getKey(); Optional<?> valueOptional1 = adaptiveMedia1.getValueOptional( amAttribute); Optional<?> valueOptional2 = adaptiveMedia2.getValueOptional( amAttribute); Optional<Long> valueOptional = valueOptional1.flatMap( value1 -> valueOptional2.map( value2 -> amAttribute.compare(value1, value2))); AMImageQueryBuilder.SortOrder sortOrder = sortCriterion.getValue(); long result = valueOptional.map( sortOrder::getSortValue ).orElse( 0L ); if (result != 0) { return result; } } return 0L; } AMAttributeDistanceComparator(
		AMAttribute<AMImageProcessor, ?> amAttribute);  AMAttributeDistanceComparator(
		AMAttribute<AMImageProcessor, ?> amAttribute,
		AMImageQueryBuilder.SortOrder sortOrder);  AMAttributeDistanceComparator(
		Map<AMAttribute<AMImageProcessor, ?>, AMImageQueryBuilder.SortOrder>
			sortCriteria); @Override long compare(
		AdaptiveMedia<AMImageProcessor> adaptiveMedia1,
		AdaptiveMedia<AMImageProcessor> adaptiveMedia2);  }
@Test public void testSortDifferentMediaByMultipleAttributes() { AdaptiveMedia<AMImageProcessor> adaptiveMedia1 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L, AMAttribute.getFileNameAMAttribute(), "zzz"); AdaptiveMedia<AMImageProcessor> adaptiveMedia2 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L, AMAttribute.getFileNameAMAttribute(), "aaa"); long result = _multiAMAttributeDistanceComparator.compare( adaptiveMedia1, adaptiveMedia2); Assert.assertEquals(-25, result); }
@Test public void testSortDifferentMediaByMultipleAttributesInverse() { AdaptiveMedia<AMImageProcessor> adaptiveMedia1 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L, AMAttribute.getFileNameAMAttribute(), "zzz"); AdaptiveMedia<AMImageProcessor> adaptiveMedia2 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L, AMAttribute.getFileNameAMAttribute(), "aaa"); long result = _multiAMAttributeDistanceComparator.compare( adaptiveMedia2, adaptiveMedia1); Assert.assertEquals(25, result); }
@Test public void testSortDifferentMediaByOneAttribute() { AdaptiveMedia<AMImageProcessor> adaptiveMedia1 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L); AdaptiveMedia<AMImageProcessor> adaptiveMedia2 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 20L); long result = _singleAMAttributeDistanceComparator.compare( adaptiveMedia1, adaptiveMedia2); Assert.assertEquals(-10, result); }
@Test public void testSortDifferentMediaByOneAttributeInverse() { AdaptiveMedia<AMImageProcessor> adaptiveMedia1 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L); AdaptiveMedia<AMImageProcessor> adaptiveMedia2 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 20L); long result = _singleAMAttributeDistanceComparator.compare( adaptiveMedia2, adaptiveMedia1); Assert.assertEquals(10, result); }
@Test public void testSortEqualMediaByMultipleAttributes() { AdaptiveMedia<AMImageProcessor> adaptiveMedia1 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L, AMAttribute.getFileNameAMAttribute(), "aaa"); AdaptiveMedia<AMImageProcessor> adaptiveMedia2 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L, AMAttribute.getFileNameAMAttribute(), "aaa"); long result = _singleAMAttributeDistanceComparator.compare( adaptiveMedia1, adaptiveMedia2); Assert.assertEquals(0, result); }
@Test public void testSortEqualMediaByOneAttribute() { AdaptiveMedia<AMImageProcessor> adaptiveMedia1 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L); AdaptiveMedia<AMImageProcessor> adaptiveMedia2 = _createAdaptiveMedia( AMAttribute.getContentLengthAMAttribute(), 10L); long result = _singleAMAttributeDistanceComparator.compare( adaptiveMedia1, adaptiveMedia2); Assert.assertEquals(0, result); }