Java 5 introduces several new features that offer significant improvements over older Java technology. In this paper we consider the new enum construct, which provides language support for enumerated types. Prior to Java 5, programmers needed to employ various patterns (e.g., the weak enum pattern) to compensate for the absence of enumerated types in Java. Unfortunately, these compensation patterns lack several highly-desirable properties of the enum construct, most notably, type safety. We present a novel fully-automated approach for transforming legacy Java code to use the new enumeration construct. This semantics-preserving approach increases type safety, produces code that is easier to comprehend, removes un- necessary complexity, and eliminates brittleness problems due to separate compilation. At the core of the proposed approach is an interprocedural type inferencing algorithm which tracks the flow of enumerated values. The algorithm was implemented as an Eclipse plug-in and evaluated experimentally on 17 large Java benchmarks. Our results indicate that analysis cost is practical and the algorithm can successfully refactor a substantial number of fields to enumerated types. This work is a significant step towards providing automated tool support for migrating legacy Java software to modern Java technologies.
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Automated Refactoring of Legacy Java Software to Enumerated Types
1. Automated Refactoring of Legacy
Java Software to Enumerated Types*
Raffi Khatchadourian, Jason Sawin, and Atanas Rountev
PRESTO: Program Analyses and Software Tools Research
Group, Ohio State University
* Work supported in part by NSF
3. Introduction
•Java 1.5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and type-safe enumerations.
4. Introduction
•Java 1.5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
5. Introduction
•Java 1.5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
•Present experimental results from research prototype
as an Eclipse IDE plug-in.
6. Introduction
•Java 1.5 introduced a rich set of new features such as
generics, metadata annotations, boxing/unboxing,
and
•Highlight an automated semantics-preserving
approach for migrating legacy Java code to take
advantage of the new language enumerated type
constructs.
•Present experimental results from research prototype
as an Eclipse IDE plug-in.
•In progress to be included with the standard
distribution of Eclipse.
7. Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
8. Motivating Example
Weak Enum
Pattern
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
9. Motivating Example
Type Safety
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
10. Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Manual
Enumeration
11. Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Namespacing
12. Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Brittle
13. Motivating Example
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
14. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
15. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
16. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
17. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
18. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
19. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Language Enum
20. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Type Safety
21. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Singletons in
Natural Order
22. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Prefixed
23. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
Supports
Separate
Compilation
24. Motivating Example Revisited
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
private static final int MAX_SPEED = 140;
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public enum Color {RED,
YELLOW,
GREEN};
/* Current color of the traffic signal, initially red by default */
private Color color = Color.RED;
/* Accessor for the light’s current color */
public Color getColor() {return this.color;}}
class Automobile {
private enum Action {IDLE,
INCREASE_SPEED,
DECREASE_SPEED,
STOP};
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
class TrafficSignal {
public static final int RED = 0;
public static final int YELLOW = 1;
public static final int GREEN = 2;
/ Current color of the traffic signal, initially red by default /
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
25. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
26. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
27. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
28. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
29. Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Automobile
Action Enum
30. Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Named-
Constant
31. Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Promising...
32. Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
Definitely not
enumerizable
33. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
34. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
35. Traffic Signal Client
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
36. / The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
37. Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
38. Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
39. /* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
40. /* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
Traffic Signal Client
private int color = RED;
/ Accessor for the light’s current color /
public int getColor() {return this.color;}}
class Automobile {
private static final int IDLE = 0;
private static final int INCREASE_SPEED = 1;
private static final int DECREASE_SPEED = 2;
private static final int STOP = 3;
private static final int MAX_SPEED = 140;
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
this.currentAction &&
INCREASE_SPEED ||
tSpeed <= MAX_SPEED))
rmAction(reaction);}
rmAction(int action) {...}}
eger constants for enumerated types.
38 (reacti
39 this.c
40 this.
41
42 private void
(b) Im
Figure 1. Running example: a hypothetical drive-by-w
ch machinery. Third, the weak enum
ogrammer to manually enumerate the
ts, which increases the likelihood of
different enum constants may be unin-
he same internal value. Finally, the
rittle [?]: since the values are com-
at compile time they are inlined into
new constants are added in between
example, in which
by language enum
and Automobile.
ations of these ne
through compile-tim
between the named
ated values. It is als
is an Action, whic
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/ The action this automobile is currently performing, idle by default /
private int currentAction = IDLE;
/ The current speed of the automobile, initially 5 mph. /
private int currentSpeed = 5;
private int react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return INCREASE_SPEED;
else return STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
int reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
/* The action this automobile is currently performing, idle by default */
private Action currentAction = Action.IDLE;
/* The current speed of the automobile, initially 5 mph. */
private int currentSpeed = 5;
private Action react(TrafficSignal signal) {
switch(signal.getColor()) {
case TrafficSignal.RED: return Action.STOP;
case TrafficSignal.YELLOW:
// decide whether to stop or go
if (this.shouldGo())
return Action.INCREASE_SPEED;
else return Action.STOP;
case TrafficSignal.GREEN: // no change
return this.currentAction;
default: throw new IllegalArgumentException
("Invalid traffic color");}} // required
public void drive() {
TrafficSignal aSignal = ... ;
Action reaction = this.react(aSignal);
41. function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach
42. function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Subset of static
final fields
Enumerization Approach
43. function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Abstract Syntax Tree (AST)
Enumerization Approach
44. function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
Output:
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach
45. • Partitioning of program entities:
• Fields
• Method declarations (return types)
• Local variables (including formal parameters)
function Enumerizable(C)
1: W ← C /* seed the worklist with the input constants */
2: N ← ∅ /* the non-enumerizable set list, initially empty */
3: for all c ∈ C do
4: MakeSet(c) /* init the union-find data structure */
5: end for
6: while W ̸= ∅ do
7: /* remove an element from the worklist */
8: α ← e | e ∈ W
9: W ← W {α}
10: for all αctxt ∈ Contexts(α, P) do
11: if ¬isEnumerizableContext(α, αctxt ) then
Output:
α variable, eld, method
αctxt context in which α may occur
Figure 2. Formalism notation.
procedure Enumerize(F, P)
1: R ← Enumerizable(F)
2: R ← Unique(R) ∩ Distinct(R) ∩ Consistent(R)
3: for all T ∈ R do
4: Transform(T)
5: end for
Figure 3. Top-level enumerization algorithm.
refactoring. We also assume that we are able to statica
identify all references to candidate elds and transitively d
pendent program entities. This assumption could be inva
dated through the use of reection and custom class loade
Enumerization Approach