Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
		| @@ -4,46 +4,45 @@ import java.util.Stack; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * The Memento pattern is a software design pattern that provides the ability to restore  | ||||
|  * an object to its previous state (undo via rollback). | ||||
|  * The Memento pattern is a software design pattern that provides the ability to restore an object | ||||
|  * to its previous state (undo via rollback). | ||||
|  * <p> | ||||
|  * The Memento pattern is implemented with three objects: the originator, a caretaker and  | ||||
|  * a memento. The originator is some object that has an internal state. The caretaker is  | ||||
|  * going to do something to the originator, but wants to be able to undo the change. The  | ||||
|  * caretaker first asks the originator for a memento object. Then it does whatever operation  | ||||
|  * (or sequence of operations) it was going to do. To roll back to the state before the  | ||||
|  * operations, it returns the memento object to the originator. The memento object itself  | ||||
|  * is an opaque object (one which the caretaker cannot, or should not, change). When using  | ||||
|  * this pattern, care should be taken if the originator may change other objects or  | ||||
|  * resources - the memento pattern operates on a single object. | ||||
|  * The Memento pattern is implemented with three objects: the originator, a caretaker and a memento. | ||||
|  * The originator is some object that has an internal state. The caretaker is going to do something | ||||
|  * to the originator, but wants to be able to undo the change. The caretaker first asks the | ||||
|  * originator for a memento object. Then it does whatever operation (or sequence of operations) it | ||||
|  * was going to do. To roll back to the state before the operations, it returns the memento object | ||||
|  * to the originator. The memento object itself is an opaque object (one which the caretaker cannot, | ||||
|  * or should not, change). When using this pattern, care should be taken if the originator may | ||||
|  * change other objects or resources - the memento pattern operates on a single object. | ||||
|  * <p> | ||||
|  * In this example the object ({@link Star}) | ||||
|  * gives out a "memento" ({@link StarMemento}) that contains the state of the object. | ||||
|  * Later on the memento can be set back to the object restoring the state. | ||||
|  * In this example the object ({@link Star}) gives out a "memento" ({@link StarMemento}) that | ||||
|  * contains the state of the object. Later on the memento can be set back to the object restoring | ||||
|  * the state. | ||||
|  *  | ||||
|  */ | ||||
| public class App { | ||||
|  | ||||
| 	public static void main(String[] args) { | ||||
| 		Stack<StarMemento> states = new Stack<>(); | ||||
|   public static void main(String[] args) { | ||||
|     Stack<StarMemento> states = new Stack<>(); | ||||
|  | ||||
| 		Star star = new Star(StarType.SUN, 10000000, 500000); | ||||
| 		System.out.println(star); | ||||
| 		states.add(star.getMemento()); | ||||
| 		star.timePasses(); | ||||
| 		System.out.println(star); | ||||
| 		states.add(star.getMemento()); | ||||
| 		star.timePasses(); | ||||
| 		System.out.println(star); | ||||
| 		states.add(star.getMemento()); | ||||
| 		star.timePasses(); | ||||
| 		System.out.println(star); | ||||
| 		states.add(star.getMemento()); | ||||
| 		star.timePasses(); | ||||
| 		System.out.println(star); | ||||
| 		while (states.size() > 0) { | ||||
| 			star.setMemento(states.pop()); | ||||
| 			System.out.println(star); | ||||
| 		} | ||||
| 	} | ||||
|     Star star = new Star(StarType.SUN, 10000000, 500000); | ||||
|     System.out.println(star); | ||||
|     states.add(star.getMemento()); | ||||
|     star.timePasses(); | ||||
|     System.out.println(star); | ||||
|     states.add(star.getMemento()); | ||||
|     star.timePasses(); | ||||
|     System.out.println(star); | ||||
|     states.add(star.getMemento()); | ||||
|     star.timePasses(); | ||||
|     System.out.println(star); | ||||
|     states.add(star.getMemento()); | ||||
|     star.timePasses(); | ||||
|     System.out.println(star); | ||||
|     while (states.size() > 0) { | ||||
|       star.setMemento(states.pop()); | ||||
|       System.out.println(star); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -7,99 +7,98 @@ package com.iluwatar.memento; | ||||
|  */ | ||||
| public class Star { | ||||
|  | ||||
| 	private StarType type; | ||||
| 	private int ageYears; | ||||
| 	private int massTons; | ||||
|   private StarType type; | ||||
|   private int ageYears; | ||||
|   private int massTons; | ||||
|  | ||||
| 	public Star(StarType startType, int startAge, int startMass) { | ||||
| 		this.type = startType; | ||||
| 		this.ageYears = startAge; | ||||
| 		this.massTons = startMass; | ||||
| 	} | ||||
|   public Star(StarType startType, int startAge, int startMass) { | ||||
|     this.type = startType; | ||||
|     this.ageYears = startAge; | ||||
|     this.massTons = startMass; | ||||
|   } | ||||
|  | ||||
| 	public void timePasses() { | ||||
| 		ageYears *= 2; | ||||
| 		massTons *= 8; | ||||
| 		switch (type) { | ||||
| 		case RED_GIANT: | ||||
| 			type = StarType.WHITE_DWARF; | ||||
| 			break; | ||||
| 		case SUN: | ||||
| 			type = StarType.RED_GIANT; | ||||
| 			break; | ||||
| 		case SUPERNOVA: | ||||
| 			type = StarType.DEAD; | ||||
| 			break; | ||||
| 		case WHITE_DWARF: | ||||
| 			type = StarType.SUPERNOVA; | ||||
| 			break; | ||||
| 		case DEAD: | ||||
| 			ageYears *= 2; | ||||
| 			massTons = 0; | ||||
| 			break; | ||||
| 		default: | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
|   public void timePasses() { | ||||
|     ageYears *= 2; | ||||
|     massTons *= 8; | ||||
|     switch (type) { | ||||
|       case RED_GIANT: | ||||
|         type = StarType.WHITE_DWARF; | ||||
|         break; | ||||
|       case SUN: | ||||
|         type = StarType.RED_GIANT; | ||||
|         break; | ||||
|       case SUPERNOVA: | ||||
|         type = StarType.DEAD; | ||||
|         break; | ||||
|       case WHITE_DWARF: | ||||
|         type = StarType.SUPERNOVA; | ||||
|         break; | ||||
|       case DEAD: | ||||
|         ageYears *= 2; | ||||
|         massTons = 0; | ||||
|         break; | ||||
|       default: | ||||
|         break; | ||||
|     } | ||||
|   } | ||||
|  | ||||
| 	StarMemento getMemento() { | ||||
|   StarMemento getMemento() { | ||||
|  | ||||
| 		StarMementoInternal state = new StarMementoInternal(); | ||||
| 		state.setAgeYears(ageYears); | ||||
| 		state.setMassTons(massTons); | ||||
| 		state.setType(type); | ||||
| 		return state; | ||||
|     StarMementoInternal state = new StarMementoInternal(); | ||||
|     state.setAgeYears(ageYears); | ||||
|     state.setMassTons(massTons); | ||||
|     state.setType(type); | ||||
|     return state; | ||||
|  | ||||
| 	} | ||||
|   } | ||||
|  | ||||
| 	void setMemento(StarMemento memento) { | ||||
|   void setMemento(StarMemento memento) { | ||||
|  | ||||
| 		StarMementoInternal state = (StarMementoInternal) memento; | ||||
| 		this.type = state.getType(); | ||||
| 		this.ageYears = state.getAgeYears(); | ||||
| 		this.massTons = state.getMassTons(); | ||||
|     StarMementoInternal state = (StarMementoInternal) memento; | ||||
|     this.type = state.getType(); | ||||
|     this.ageYears = state.getAgeYears(); | ||||
|     this.massTons = state.getMassTons(); | ||||
|  | ||||
| 	} | ||||
|   } | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return String.format("%s age: %d years mass: %d tons", type.toString(), | ||||
| 				ageYears, massTons); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| 	 *  | ||||
| 	 * StarMemento implementation | ||||
| 	 *  | ||||
| 	 */ | ||||
| 	private static class StarMementoInternal implements StarMemento { | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return String.format("%s age: %d years mass: %d tons", type.toString(), ageYears, massTons); | ||||
|   } | ||||
|  | ||||
| 		private StarType type; | ||||
| 		private int ageYears; | ||||
| 		private int massTons; | ||||
|   /** | ||||
|    *  | ||||
|    * StarMemento implementation | ||||
|    *  | ||||
|    */ | ||||
|   private static class StarMementoInternal implements StarMemento { | ||||
|  | ||||
| 		public StarType getType() { | ||||
| 			return type; | ||||
| 		} | ||||
|     private StarType type; | ||||
|     private int ageYears; | ||||
|     private int massTons; | ||||
|  | ||||
| 		public void setType(StarType type) { | ||||
| 			this.type = type; | ||||
| 		} | ||||
|     public StarType getType() { | ||||
|       return type; | ||||
|     } | ||||
|  | ||||
| 		public int getAgeYears() { | ||||
| 			return ageYears; | ||||
| 		} | ||||
|     public void setType(StarType type) { | ||||
|       this.type = type; | ||||
|     } | ||||
|  | ||||
| 		public void setAgeYears(int ageYears) { | ||||
| 			this.ageYears = ageYears; | ||||
| 		} | ||||
|     public int getAgeYears() { | ||||
|       return ageYears; | ||||
|     } | ||||
|  | ||||
| 		public int getMassTons() { | ||||
| 			return massTons; | ||||
| 		} | ||||
|     public void setAgeYears(int ageYears) { | ||||
|       this.ageYears = ageYears; | ||||
|     } | ||||
|  | ||||
| 		public void setMassTons(int massTons) { | ||||
| 			this.massTons = massTons; | ||||
| 		} | ||||
| 	} | ||||
|     public int getMassTons() { | ||||
|       return massTons; | ||||
|     } | ||||
|  | ||||
|     public void setMassTons(int massTons) { | ||||
|       this.massTons = massTons; | ||||
|     } | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -7,16 +7,17 @@ package com.iluwatar.memento; | ||||
|  */ | ||||
| public enum StarType { | ||||
|  | ||||
| 	SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD("dead star"), UNDEFINED(""); | ||||
|   SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD( | ||||
|       "dead star"), UNDEFINED(""); | ||||
|  | ||||
|     private String title; | ||||
|   private String title; | ||||
|  | ||||
|     StarType(String title) { | ||||
|         this.title = title; | ||||
|     } | ||||
|   StarType(String title) { | ||||
|     this.title = title; | ||||
|   } | ||||
|  | ||||
|     @Override | ||||
| 	public String toString() { | ||||
| 		return title; | ||||
| 	} | ||||
|   @Override | ||||
|   public String toString() { | ||||
|     return title; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -11,9 +11,9 @@ import com.iluwatar.memento.App; | ||||
|  */ | ||||
| public class AppTest { | ||||
|  | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		String[] args = {}; | ||||
| 		App.main(args); | ||||
| 	} | ||||
|   @Test | ||||
|   public void test() { | ||||
|     String[] args = {}; | ||||
|     App.main(args); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user