If we reimplement PetCreator using lcass literals, the result is cleaner in many ways:
// typeinfo/pets/LiteralPetCreator.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Using class literals
// {java typeinfo.pets.LiteralPetCreator}
package typeinfo.pets;
import java.util.*;
public class LiteralPetCreator extends PetCreator {
// No try block needed.
@SuppressWarnings("unchecked")
public static final List<Class<? extends Pet>> ALL_TYPES =
Collections.unmodifiableList(
Arrays.asList(
Pet.class,
Dog.class,
Cat.class,
Rodent.class,
Mutt.class,
Pug.class,
EgyptianMau.class,
Manx.class,
Cymric.class,
Rat.class,
Mouse.class,
Hamster.class));
// Types for random creation:
private static final List<Class<? extends Pet>> TYPES =
ALL_TYPES.subList(ALL_TYPES.indexOf(Cat.class), ALL_TYPES.size());
@Override
public List<Class<? extends Pet>> types() {
return TYPES;
}
public static void main(String[] args) {
System.out.println(TYPES);
}
}
/* Output:
[class typeinfo.pets.Cat, class typeinfo.pets.Rodent, class typeinfo.pets.Mutt, class typeinfo.pets.Pug, class typeinfo.pets.EgyptianMau, class typeinfo.pets.Manx
, class typeinfo.pets.Cymric, class typeinfo.pets.Rat, class typeinfo.pets.Mouse, class typeinfo.pets.Hamster]
*/
run it by in OnJava8-Examples directory:
% javac typeinfo/pets/LiteralPetCreator.java
% java typeinfo.pets.LiteralPetCreator
/**
* Returns an unmodifiable view of the specified list. This method allows
* modules to provide users with "read-only" access to internal
* lists. Query operations on the returned list "read through" to the
* specified list, and attempts to modify the returned list, whether
* direct or via its iterator, result in an
* <tt>UnsupportedOperationException</tt>.<p>
*
* The returned list will be serializable if the specified list
* is serializable. Similarly, the returned list will implement
* {@link RandomAccess} if the specified list does.
*
* @param <T> the class of the objects in the list
* @param list the list for which an unmodifiable view is to be returned.
* @return an unmodifiable view of the specified list.
*/
public static <T> List<T> unmodifiableList(List<? extends T> list) {
return (list instanceof RandomAccess ?
new UnmodifiableRandomAccessList<>(list) :
new UnmodifiableList<>(list));
}
// typeinfo/pets/Pets.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Facade to produce a default PetCreator
package typeinfo.pets;
import java.util.*;
import java.util.stream.*;
public class Pets {
public static final PetCreator CREATOR =
new LiteralPetCreator();
public static Pet get() {
return CREATOR.get();
}
public static Pet[] array(int size) {
Pet[] result = new Pet[size];
for(int i = 0; i < size; i++)
result[i] = CREATOR.get();
return result;
}
public static List<Pet> list(int size) {
List<Pet> result = new ArrayList<>();
Collections.addAll(result, array(size));
return result;
}
public static Stream<Pet> stream() {
return Stream.generate(CREATOR);
}
}
// typeinfo/PetCount2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import typeinfo.pets.*;
public class PetCount2 {
public static void main(String[] args) {
PetCount.countPets(Pets.CREATOR);
}
}
/* Output:
Mouse Manx Pug Rodent Rodent Hamster Mouse Cat Mutt Rat Mouse Mouse Rodent Hamster Hamster Mouse Mouse Rodent Cat Mouse
{Pug=1, Mouse=7, Rat=1, Cat=3, Manx=1, Rodent=15, Mutt=1, Dog=2, Pet=20, Hamster=3}
*/
run PetCount2 please use
./gradlew :typeinfo:PetCount2
and use
./gradlew :typeinfo:PetCount
compare the output results.
next article
refrences:
1. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/typeinfo/pets/LiteralPetCreator.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/typeinfo/pets/PetCount.java
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/typeinfo/pets/PetCount2.java