📄 animals.java
字号:
package net.jcip.examples;import java.util.*;/** * Animals * <p/> * Thread confinement of local primitive and reference variables * * @author Brian Goetz and Tim Peierls */public class Animals { Ark ark; Species species; Gender gender; public int loadTheArk(Collection<Animal> candidates) { SortedSet<Animal> animals; int numPairs = 0; Animal candidate = null; // animals confined to method, don't let them escape! animals = new TreeSet<Animal>(new SpeciesGenderComparator()); animals.addAll(candidates); for (Animal a : animals) { if (candidate == null || !candidate.isPotentialMate(a)) candidate = a; else { ark.load(new AnimalPair(candidate, a)); ++numPairs; candidate = null; } } return numPairs; } class Animal { Species species; Gender gender; public boolean isPotentialMate(Animal other) { return species == other.species && gender != other.gender; } } enum Species { AARDVARK, BENGAL_TIGER, CARIBOU, DINGO, ELEPHANT, FROG, GNU, HYENA, IGUANA, JAGUAR, KIWI, LEOPARD, MASTADON, NEWT, OCTOPUS, PIRANHA, QUETZAL, RHINOCEROS, SALAMANDER, THREE_TOED_SLOTH, UNICORN, VIPER, WEREWOLF, XANTHUS_HUMMINBIRD, YAK, ZEBRA } enum Gender { MALE, FEMALE } class AnimalPair { private final Animal one, two; public AnimalPair(Animal one, Animal two) { this.one = one; this.two = two; } } class SpeciesGenderComparator implements Comparator<Animal> { public int compare(Animal one, Animal two) { int speciesCompare = one.species.compareTo(two.species); return (speciesCompare != 0) ? speciesCompare : one.gender.compareTo(two.gender); } } class Ark { private final Set<AnimalPair> loadedAnimals = new HashSet<AnimalPair>(); public void load(AnimalPair pair) { loadedAnimals.add(pair); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -