19.8 使用EnumSet代替标志

package chapter19._8;

public enum AlarmPoints {

    STAIR1, STAIR2, LOBBY,
    OFFICE1, OFFICE2, OFFICE3, OFFICE4,
    BATHROOM, UTILITY, KITCHEN

}
package chapter19._8;

import java.util.EnumSet;
import static chapter19._8.AlarmPoints.*;

public class EnumSets {

    public static void main(String[] args) {
        EnumSet points = EnumSet.noneOf(AlarmPoints.class);
        System.out.println(points);

        points.add(BATHROOM);
        System.out.println(points);

        points.addAll(EnumSet.of(STAIR1, STAIR2, KITCHEN));
        System.out.println(points);

        points = EnumSet.allOf(AlarmPoints.class);
        System.out.println(points);

        points.removeAll(EnumSet.of(STAIR1, STAIR2, KITCHEN));
        System.out.println(points);

        points.removeAll(EnumSet.range(OFFICE1, OFFICE4));
        System.out.println(points);

        points = EnumSet.complementOf(points);
        System.out.println(points);
    }

}

输出:

[]
[BATHROOM]
[STAIR1, STAIR2, BATHROOM, KITCHEN]
[STAIR1, STAIR2, LOBBY, OFFICE1, OFFICE2, OFFICE3, OFFICE4, BATHROOM, UTILITY, KITCHEN]
[LOBBY, OFFICE1, OFFICE2, OFFICE3, OFFICE4, BATHROOM, UTILITY]
[LOBBY, BATHROOM, UTILITY]
[STAIR1, STAIR2, OFFICE1, OFFICE2, OFFICE3, OFFICE4, KITCHEN]

你可能感兴趣的:(19.8 使用EnumSet代替标志)