package com.mindprod.example;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import static java.lang.System.*;
interface Dog
{
/**
* what is this dog called?
*/
String getName();
}
/**
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
* <p/>
* Convert a Set.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2012-02-02
* @since 2012-02-02
*/
public final class TestConvertSet
{
/**
* copy elements of set from to set to
* Would let you copy Set<Dalmatian> to Set<Dog> or Set<Dog> to Set<Mammal>
*
* @param from source Set
* @param to target Set
*/
private static void copy( Set<? extends Dog> from, Set<? super Dog> to )
{
to.addAll( from );
}
/**
* list a Set of Dogs
*/
private static void showDogs1( Set<Dog> dogs )
{
out.println( "--showdogs1--" );
for ( Dog dog : dogs )
{
out.println( dog.getName() );
}
}
/**
* list a Set of Dogs, alternate
*/
private static void showDogs2( Set<? extends Dog> dogs )
{
out.println( "--showdogs2--" );
for ( Dog dog : dogs )
{
out.println( dog.getName() );
}
}
/**
* Demonstrate two ways to change the type of a generic set.
*
* @param args not used
*/
public static void main( String[] args )
{
Set<Dalmatian> someDalmatians = new HashSet<>( Arrays.asList(
new Dalmatian( "Spot", 45 ),
new Dalmatian( "Sheldon", 149 ),
new Dalmatian( "Peter", 17 ) ) );
Set<Dog> someDogs1 = new HashSet<Dog>( someDalmatians );
showDogs1( someDogs1 );
Set<? extends Dog> someDogs2 = someDalmatians;
showDogs2( someDogs2 );
showDogs2( someDogs1 );
showDogs2( someDalmatians );
Set<Dalmatian> from = someDalmatians;
Set<Dog> to = new HashSet<>( 5 );
copy( from, to );
showDogs1( to );
}
}
class Dalmatian implements Dog
{
/**
* what is this dog called?
*/
private final String name;
/**
* how many spots does this Dalmatian have?
*/
private final int spots;
/**
* constructor
*
* @param spots how many spots the dog has
*/
Dalmatian( String name, int spots )
{
this.name = name;
this.spots = spots;
}
/**
* how many spots does this Dalmatian have?
*/
int getSpots()
{
return spots;
}
/**
* What is this dog called? Implements pubic Dog.getName method.
*/
public String getName()
{
return name;
}
}