Merge pull request #30 from ruslanpa/master

Makes a few improvements, basically unnecessary declarations, formatting
This commit is contained in:
Ilkka Seppälä 2015-02-10 19:09:47 +02:00
commit e01c852ee0
21 changed files with 297 additions and 272 deletions

View File

@ -2,11 +2,16 @@ package com.iluwatar;
public enum HairType { public enum HairType {
BALD, SHORT, CURLY, LONG_STRAIGHT, LONG_CURLY; BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
@Override private String title;
HairType(String title) {
this.title = title;
}
@Override
public String toString() { public String toString() {
return name().toLowerCase().replaceAll("_", " "); return title;
} }
} }

View File

@ -3,8 +3,8 @@ package com.iluwatar;
public class Goblin extends Target { public class Goblin extends Target {
public Goblin() { public Goblin() {
this.setSize(Size.NORMAL); setSize(Size.NORMAL);
this.setVisibility(Visibility.VISIBLE); setVisibility(Visibility.VISIBLE);
} }
@Override @Override

View File

@ -4,10 +4,6 @@ public class InvisibilitySpell extends Command {
private Target target; private Target target;
public InvisibilitySpell() {
target = null;
}
@Override @Override
public void execute(Target target) { public void execute(Target target) {
target.setVisibility(Visibility.INVISIBLE); target.setVisibility(Visibility.INVISIBLE);

View File

@ -3,14 +3,8 @@ package com.iluwatar;
public class ShrinkSpell extends Command { public class ShrinkSpell extends Command {
private Size oldSize; private Size oldSize;
private Target target; private Target target;
public ShrinkSpell() {
oldSize = null;
target = null;
}
@Override @Override
public void execute(Target target) { public void execute(Target target) {
oldSize = target.getSize(); oldSize = target.getSize();

View File

@ -5,9 +5,8 @@ public class Wizard extends Target {
private Command previousSpell; private Command previousSpell;
public Wizard() { public Wizard() {
this.setSize(Size.NORMAL); setSize(Size.NORMAL);
this.setVisibility(Visibility.VISIBLE); setVisibility(Visibility.VISIBLE);
previousSpell = null;
} }
public void castSpell(Command command, Target target) { public void castSpell(Command command, Target target) {

View File

@ -1,6 +1,7 @@
package com.iluwatar; package com.iluwatar;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
@ -15,7 +16,7 @@ import java.util.List;
*/ */
public class DwarvenGoldmineFacade { public class DwarvenGoldmineFacade {
List<DwarvenMineWorker> workers; private final List<DwarvenMineWorker> workers;
public DwarvenGoldmineFacade() { public DwarvenGoldmineFacade() {
workers = new ArrayList<>(); workers = new ArrayList<>();
@ -25,23 +26,20 @@ public class DwarvenGoldmineFacade {
} }
public void startNewDay() { public void startNewDay() {
for (DwarvenMineWorker worker : workers) { makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE);
worker.wakeUp();
worker.goToMine();
}
} }
public void digOutGold() { public void digOutGold() {
for (DwarvenMineWorker worker : workers) { makeActions(workers, DwarvenMineWorker.Action.WORK);
worker.work();
}
} }
public void endDay() { public void endDay() {
for (DwarvenMineWorker worker : workers) { makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
worker.goHome();
worker.goToSleep();
}
} }
private void makeActions(Collection<DwarvenMineWorker> workers, DwarvenMineWorker.Action... actions) {
for (DwarvenMineWorker worker : workers) {
worker.action(actions);
}
}
} }

View File

@ -23,8 +23,40 @@ public abstract class DwarvenMineWorker {
System.out.println(name() + " goes to the mine."); System.out.println(name() + " goes to the mine.");
} }
private void action(Action action) {
switch (action) {
case GO_TO_SLEEP:
goToSleep();
break;
case WAKE_UP:
wakeUp();
break;
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
System.out.println("Undefined action");
break;
}
}
public void action(Action... actions) {
for (Action action : actions) {
action(action);
}
}
public abstract void work(); public abstract void work();
public abstract String name(); public abstract String name();
static enum Action {
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
}
} }

View File

@ -7,12 +7,22 @@ package com.iluwatar;
*/ */
public enum Action { public enum Action {
HUNT("hunted a rabbit"), TALE("tells a tale"), GOLD("found gold"), ENEMY("spotted enemies"), NONE(""); HUNT("hunted a rabbit", "arrives for dinner"),
TALE("tells a tale", "comes to listen"),
GOLD("found gold", "takes his share of the gold"),
ENEMY("spotted enemies", "runs for cover"),
NONE("", "");
private String title; private String title;
private String description;
Action(String title) { Action(String title, String description) {
this.title = title; this.title = title;
this.description = description;
}
public String getDescription() {
return description;
} }
public String toString() { public String toString() {

View File

@ -10,7 +10,7 @@ import java.util.List;
*/ */
public class PartyImpl implements Party { public class PartyImpl implements Party {
private List<PartyMember> members; private final List<PartyMember> members;
public PartyImpl() { public PartyImpl() {
members = new ArrayList<>(); members = new ArrayList<>();

View File

@ -17,24 +17,7 @@ public abstract class PartyMemberBase implements PartyMember {
@Override @Override
public void partyAction(Action action) { public void partyAction(Action action) {
String s = this + " "; System.out.println(this + " " + action.getDescription());
switch (action) {
case ENEMY:
s = s + "runs for cover";
break;
case GOLD:
s = s + "takes his share of the gold";
break;
case HUNT:
s = s + "arrives for dinner";
break;
case TALE:
s = s + "comes to listen";
break;
default:
break;
}
System.out.println(s);
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package com.iluwatar.generic; package com.iluwatar.generic;
public interface Observer<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> { public interface Observer<S extends Observable<S, O, A>, O extends Observer<S, O, A>, A> {
public void update(S subject, A argument);
void update(S subject, A argument);
} }

View File

@ -10,44 +10,42 @@ import java.util.ArrayList;
* *
*/ */
public class App { public class App {
static Servant jenkins = new Servant("Jenkins"); static Servant jenkins = new Servant("Jenkins");
static Servant travis = new Servant("Travis"); static Servant travis = new Servant("Travis");
public static void main( String[] args ){ public static void main(String[] args) {
scenario(jenkins, 1); scenario(jenkins, 1);
scenario(travis, 0); scenario(travis, 0);
} }
/* /*
* Can add a List with enum Actions for variable scenarios * Can add a List with enum Actions for variable scenarios
* */ * */
public static void scenario(Servant servant, int compliment){ public static void scenario(Servant servant, int compliment) {
King k = new King(); King k = new King();
Queen q = new Queen(); Queen q = new Queen();
ArrayList<Royalty> guests = new ArrayList<>(); ArrayList<Royalty> guests = new ArrayList<>();
guests.add(k); guests.add(k);
guests.add(q); guests.add(q);
//feed //feed
servant.feed(k); servant.feed(k);
servant.feed(q); servant.feed(q);
//serve drinks //serve drinks
servant.giveWine(k); servant.giveWine(k);
servant.giveWine(q); servant.giveWine(q);
//compliment //compliment
servant.GiveCompliments( guests.get(compliment) ); servant.GiveCompliments(guests.get(compliment));
//outcome of the night
for(Royalty r : guests)
r.changeMood();
//check your luck
if( servant.checkIfYouWillBeHanged(guests) )
System.out.println(servant.name + " will live another day");
else
System.out.println("Poor " + servant.name + ". His days are numbered");
}
//outcome of the night
for (Royalty r : guests)
r.changeMood();
//check your luck
if (servant.checkIfYouWillBeHanged(guests))
System.out.println(servant.name + " will live another day");
else
System.out.println("Poor " + servant.name + ". His days are numbered");
}
} }

View File

@ -1,31 +1,33 @@
package com.iluwatar; package com.iluwatar;
public class King implements Royalty{ public class King implements Royalty {
private boolean isDrunk = false, isHungry = true, isHappy = false; private boolean isDrunk;
private boolean complimentReceived = false; private boolean isHungry = true;
private boolean isHappy;
private boolean complimentReceived;
@Override @Override
public void getFed() { public void getFed() {
isHungry = false; isHungry = false;
} }
@Override @Override
public void getDrink() { public void getDrink() {
isDrunk = true; isDrunk = true;
} }
public void receiveCompliments(){ public void receiveCompliments() {
complimentReceived = true; complimentReceived = true;
} }
@Override @Override
public void changeMood() { public void changeMood() {
if(!isHungry && isDrunk) isHappy = true; if (!isHungry && isDrunk) isHappy = true;
if( complimentReceived ) isHappy = false; if (complimentReceived) isHappy = false;
} }
@Override @Override
public boolean getMood() { public boolean getMood() {
return isHappy; return isHappy;
} }
} }

View File

@ -1,35 +1,38 @@
package com.iluwatar; package com.iluwatar;
public class Queen implements Royalty{ public class Queen implements Royalty {
private boolean isDrunk = true, isHungry = false, isHappy = false; private boolean isDrunk = true;
private boolean isFlirty = true, complimentReceived = false; private boolean isHungry;
private boolean isHappy;
private boolean isFlirty = true;
private boolean complimentReceived;
@Override @Override
public void getFed() { public void getFed() {
isHungry = false; isHungry = false;
} }
@Override @Override
public void getDrink() { public void getDrink() {
isDrunk = true; isDrunk = true;
} }
public void receiveCompliments(){ public void receiveCompliments() {
complimentReceived = true; complimentReceived = true;
} }
@Override @Override
public void changeMood() { public void changeMood() {
if( complimentReceived && isFlirty && isDrunk ) isHappy = true; if (complimentReceived && isFlirty && isDrunk) isHappy = true;
} }
@Override @Override
public boolean getMood() { public boolean getMood() {
return isHappy; return isHappy;
} }
public void setFlirtiness(boolean f){ public void setFlirtiness(boolean f) {
this.isFlirty = f; this.isFlirty = f;
} }
} }

View File

@ -1,9 +1,14 @@
package com.iluwatar; package com.iluwatar;
interface Royalty { interface Royalty {
public void getFed();
public void getDrink(); void getFed();
public void changeMood();
public void receiveCompliments(); void getDrink();
public boolean getMood();
void changeMood();
void receiveCompliments();
boolean getMood();
} }

View File

@ -1,20 +1,20 @@
package com.iluwatar; package com.iluwatar;
/** /**
* Service locator pattern, used to lookup jndi services * Service locator pattern, used to lookup jndi services
* and cache them for subsequent requests. * and cache them for subsequent requests.
* @author saifasif
* *
* @author saifasif
*/ */
public class App { public class App {
public static void main(String[] args) { public static void main(String[] args) {
Service service = ServiceLocator.getService("jndi/serviceA"); Service service = ServiceLocator.getService("jndi/serviceA");
service.execute(); service.execute();
service = ServiceLocator.getService("jndi/serviceB"); service = ServiceLocator.getService("jndi/serviceB");
service.execute(); service.execute();
service = ServiceLocator.getService("jndi/serviceA"); service = ServiceLocator.getService("jndi/serviceA");
service.execute(); service.execute();
service = ServiceLocator.getService("jndi/serviceA"); service = ServiceLocator.getService("jndi/serviceA");
service.execute(); service.execute();
} }
} }

View File

@ -3,27 +3,27 @@ package com.iluwatar;
/** /**
* For JNDI lookup of services from the web.xml. Will match name of the service name that * For JNDI lookup of services from the web.xml. Will match name of the service name that
* is being requested and return a newly created service object with the name * is being requested and return a newly created service object with the name
* @author saifasif
* *
* @author saifasif
*/ */
public class InitContext { public class InitContext {
/** /**
* Perform the lookup based on the service name. The returned object will need to be * Perform the lookup based on the service name. The returned object will need to be
* casted into a {@link Service} * casted into a {@link Service}
* @param serviceName *
* @return * @param serviceName a string
*/ * @return an {@link Object}
public Object lookup(String serviceName){ */
if( serviceName.equals("jndi/serviceA") ){ public Object lookup(String serviceName) {
System.out.println("Looking up service A and creating new service for A"); if (serviceName.equals("jndi/serviceA")) {
return new ServiceImpl("jndi/serviceA"); System.out.println("Looking up service A and creating new service for A");
} else if( serviceName.equals("jndi/serviceB") ){ return new ServiceImpl("jndi/serviceA");
System.out.println("Looking up service B and creating new service for B"); } else if (serviceName.equals("jndi/serviceB")) {
return new ServiceImpl("jndi/serviceB"); System.out.println("Looking up service B and creating new service for B");
} else { return new ServiceImpl("jndi/serviceB");
return null; } else {
} return null;
} }
}
} }

View File

@ -14,16 +14,15 @@ public interface Service {
/* /*
* The human readable name of the service * The human readable name of the service
*/ */
public String getName(); String getName();
/* /*
* Unique ID of the particular service * Unique ID of the particular service
*/ */
public int getId(); int getId();
/* /*
* The workflow method that defines what this service does * The workflow method that defines what this service does
*/ */
public void execute(); void execute();
} }

View File

@ -8,39 +8,40 @@ import java.util.Map;
* On first hit, the cache will be empty and thus any service that is being requested, will be * On first hit, the cache will be empty and thus any service that is being requested, will be
* created fresh and then placed into the cache map. On next hit, if same service name will * created fresh and then placed into the cache map. On next hit, if same service name will
* be requested, it will be returned from the cache * be requested, it will be returned from the cache
* @author saifasif
* *
* @author saifasif
*/ */
public class ServiceCache { public class ServiceCache {
private Map<String, Service> serviceCache; private final Map<String, Service> serviceCache;
public ServiceCache() { public ServiceCache() {
serviceCache = new HashMap<String, Service>(); serviceCache = new HashMap<String, Service>();
} }
/** /**
* Get the service from the cache. null if no service is found matching the * Get the service from the cache. null if no service is found matching the name
* name *
* @param serviceName * @param serviceName a string
* @return {@link Service} * @return {@link Service}
*/ */
public Service getService(String serviceName){ public Service getService(String serviceName) {
Service cachedService = null; Service cachedService = null;
for (String serviceJndiName : serviceCache.keySet()){ for (String serviceJndiName : serviceCache.keySet()) {
if( serviceJndiName.equals( serviceName ) ){ if (serviceJndiName.equals(serviceName)) {
cachedService = serviceCache.get(serviceJndiName); cachedService = serviceCache.get(serviceJndiName);
System.out.println("(cache call) Fetched service " + cachedService.getName() + "("+cachedService.getId()+") from cache... !"); System.out.println("(cache call) Fetched service " + cachedService.getName() + "(" + cachedService.getId() + ") from cache... !");
} }
} }
return cachedService; return cachedService;
} }
/** /**
* Adds the service into the cache map * Adds the service into the cache map
* @param newService *
*/ * @param newService a {@link Service}
public void addService(Service newService){ */
serviceCache.put(newService.getName(), newService); public void addService(Service newService) {
} serviceCache.put(newService.getName(), newService);
}
} }

View File

@ -4,34 +4,34 @@ package com.iluwatar;
* This is a single service implementation of a sample service. This is the actual * This is a single service implementation of a sample service. This is the actual
* service that will process the request. The reference for this service is to * service that will process the request. The reference for this service is to
* be looked upon in the JNDI server that can be set in the web.xml deployment descriptor * be looked upon in the JNDI server that can be set in the web.xml deployment descriptor
* @author saifasif
* *
* @author saifasif
*/ */
public class ServiceImpl implements Service { public class ServiceImpl implements Service {
private String serviceName; private final String serviceName;
private int id; private final int id;
public ServiceImpl(String serviceName) { public ServiceImpl(String serviceName) {
// set the service name // set the service name
this.serviceName = serviceName; this.serviceName = serviceName;
// Generate a random id to this service object // Generate a random id to this service object
this.id = (int)Math.floor(Math.random()*1000)+1; this.id = (int) Math.floor(Math.random() * 1000) + 1;
} }
@Override @Override
public String getName() { public String getName() {
return serviceName; return serviceName;
} }
@Override @Override
public int getId() { public int getId() {
return id; return id;
} }
@Override @Override
public void execute() { public void execute() {
System.out.println("Service " + getName() + " is now executing with id " + getId()); System.out.println("Service " + getName() + " is now executing with id " + getId());
} }
} }

View File

@ -5,33 +5,32 @@ package com.iluwatar;
* Will fetch service from cache, otherwise creates a fresh service and update cache * Will fetch service from cache, otherwise creates a fresh service and update cache
* *
* @author saifasif * @author saifasif
*
*/ */
public class ServiceLocator { public class ServiceLocator {
private static ServiceCache serviceCache = new ServiceCache(); private static ServiceCache serviceCache = new ServiceCache();
/** /**
* Fetch the service with the name param from the cache first, * Fetch the service with the name param from the cache first,
* if no service is found, lookup the service from the {@link InitContext} and * if no service is found, lookup the service from the {@link InitContext} and
* then add the newly created service into the cache map for future requests. * then add the newly created service into the cache map for future requests.
* @param serviceJndiName *
* @return {@link Service} * @param serviceJndiName a string
*/ * @return {@link Service}
public static Service getService(String serviceJndiName){ */
Service serviceObj = serviceCache.getService(serviceJndiName); public static Service getService(String serviceJndiName) {
if ( serviceObj != null ){ Service serviceObj = serviceCache.getService(serviceJndiName);
return serviceObj; if (serviceObj != null) {
} else { return serviceObj;
/* } else {
* If we are unable to retrive anything from cache, then /*
* If we are unable to retrive anything from cache, then
* lookup the service and add it in the cache map * lookup the service and add it in the cache map
*/ */
InitContext ctx = new InitContext(); InitContext ctx = new InitContext();
serviceObj = (Service) ctx.lookup(serviceJndiName); serviceObj = (Service) ctx.lookup(serviceJndiName);
serviceCache.addService(serviceObj); serviceCache.addService(serviceObj);
return serviceObj; return serviceObj;
} }
} }
} }