Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package animaltype;
  2.  
  3. import static org.junit.Assert.*;
  4. import org.junit.Assert;
  5. import org.junit.Test;
  6.  
  7. import java.util.Arrays;
  8. import java.util.LinkedHashMap;
  9. import java.util.LinkedHashSet;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Objects;
  13. import java.util.Set;
  14. import java.util.Spliterator;
  15. import java.util.function.Function;
  16. import java.util.stream.Collectors;
  17. import java.util.stream.IntStream;
  18. import java.util.stream.Stream;
  19.  
  20. public class AnimalTest {
  21.     @Test
  22.     public void testPublish() {
  23.        
  24.         List<Animal> animals = Arrays
  25.             .asList(
  26.                 new Animal( "first", AnimalType.HUMAN ),
  27.                 new Animal( "first", AnimalType.DOG ),
  28.                 new Animal( "second", AnimalType.HUMAN ),
  29.                 new Animal( "first", AnimalType.CAT )
  30.             )
  31.         ;
  32.        
  33.                
  34.         Stream<Integer> infiniteStreamOfInts = Stream.iterate( 0, i -> i+1 );
  35.  
  36.         Stream<Animal> infiniteStreamOfAnimals = infiniteStreamOfInts
  37.             .map(
  38.                 i->animals.get(
  39.                     i % animals.size()
  40.                 )
  41.             )
  42.         ;
  43.  
  44.         Set<Animal> setOfAnimals = InfiniteStream.firstFrom( infiniteStreamOfAnimals, AnimalType.values().length );
  45.        
  46.         Assert.assertTrue( setOfAnimals.size() == AnimalType.values().length );
  47.        
  48.         System.out.println( setOfAnimals );
  49.        
  50.         String actual = setOfAnimals.toString();
  51.         String expected = "[first HUMAN animal, first DOG animal, first CAT animal]";
  52.         String message = System.lineSeparator() +
  53.             expected + " <- expected" + System.lineSeparator() +
  54.             actual + " <- actual" + System.lineSeparator()
  55.         ;
  56.         Assert.assertEquals(message, expected, actual);
  57.     }
  58.    
  59. }
  60.  
  61. // Displays
  62. // [HUMAN animal 1, DOG animal 1, CAT animal 1]
  63.  
  64. class InfiniteStream {
  65.     public static <T> Set<T> firstFrom( Stream<T> infiniteStream, int size ) {
  66.         Set<T> set = new LinkedHashSet<>();
  67.  
  68.         infiniteStream
  69.             .takeWhile( x->set.size() < size )
  70.             .collect(
  71.                 Collectors.toCollection(
  72.                     ()->set
  73.                 )
  74.             )
  75.         ;
  76.        
  77.         return set;
  78.     }
  79. }
  80.  
  81. enum AnimalType{ HUMAN, DOG, CAT }
  82.  
  83. class Animal {
  84.  
  85.     public Animal( String tag, AnimalType type ) {
  86.         this.tag = tag;
  87.         this.type = type;
  88.     }
  89.    
  90.     public AnimalType getAnimalType() { return type; }
  91.  
  92.     public String toString() { return tag + " " + type + " animal"; }
  93.  
  94.     @Override
  95.     public int hashCode() { return Objects.hashCode( type ); }
  96.  
  97.     @Override
  98.     public boolean equals( Object that ) {        
  99.         return that != null
  100.             && that.getClass() == this.getClass()    
  101.             && ( (Animal) that ).type == this.type
  102.         ;
  103.     }
  104.    
  105.     private String tag;
  106.     private AnimalType type;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement