Refactors using var

This commit is contained in:
Anurag Agarwal 2020-08-04 21:46:30 +00:00
parent 4b38746ce9
commit a7095602d6
No known key found for this signature in database
GPG Key ID: CF5E14552DA23F13
3 changed files with 16 additions and 16 deletions

View File

@ -34,9 +34,12 @@ Let's first define the types of stars we are capable to handle.
```java ```java
public enum StarType { public enum StarType {
SUN("sun"),
SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD( RED_GIANT("red giant"),
"dead star"), UNDEFINED(""); WHITE_DWARF("white dwarf"),
SUPERNOVA("supernova"),
DEAD("dead star"),
UNDEFINED("");
private final String title; private final String title;
@ -95,8 +98,7 @@ public class Star {
} }
StarMemento getMemento() { StarMemento getMemento() {
var state = new StarMementoInternal();
StarMementoInternal state = new StarMementoInternal();
state.setAgeYears(ageYears); state.setAgeYears(ageYears);
state.setMassTons(massTons); state.setMassTons(massTons);
state.setType(type); state.setType(type);
@ -104,8 +106,7 @@ public class Star {
} }
void setMemento(StarMemento memento) { void setMemento(StarMemento memento) {
var state = (StarMementoInternal) memento;
StarMementoInternal state = (StarMementoInternal) memento;
this.type = state.getType(); this.type = state.getType();
this.ageYears = state.getAgeYears(); this.ageYears = state.getAgeYears();
this.massTons = state.getMassTons(); this.massTons = state.getMassTons();
@ -152,8 +153,8 @@ public class Star {
And finally here's how we use the mementos to store and restore star states. And finally here's how we use the mementos to store and restore star states.
```java ```java
Stack<StarMemento> states = new Stack<>(); var states = new Stack<>();
Star star = new Star(StarType.SUN, 10000000, 500000); var star = new Star(StarType.SUN, 10000000, 500000);
LOGGER.info(star.toString()); LOGGER.info(star.toString());
states.add(star.getMemento()); states.add(star.getMemento());
star.timePasses(); star.timePasses();

View File

@ -70,22 +70,18 @@ public class Star {
} }
StarMemento getMemento() { StarMemento getMemento() {
var state = new StarMementoInternal(); var state = new StarMementoInternal();
state.setAgeYears(ageYears); state.setAgeYears(ageYears);
state.setMassTons(massTons); state.setMassTons(massTons);
state.setType(type); state.setType(type);
return state; return state;
} }
void setMemento(StarMemento memento) { void setMemento(StarMemento memento) {
var state = (StarMementoInternal) memento; var state = (StarMementoInternal) memento;
this.type = state.getType(); this.type = state.getType();
this.ageYears = state.getAgeYears(); this.ageYears = state.getAgeYears();
this.massTons = state.getMassTons(); this.massTons = state.getMassTons();
} }
@Override @Override

View File

@ -27,9 +27,12 @@ package com.iluwatar.memento;
* StarType enumeration. * StarType enumeration.
*/ */
public enum StarType { public enum StarType {
SUN("sun"),
SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD( RED_GIANT("red giant"),
"dead star"), UNDEFINED(""); WHITE_DWARF("white dwarf"),
SUPERNOVA("supernova"),
DEAD("dead star"),
UNDEFINED("");
private final String title; private final String title;