public class AppPlace extends Place {
// You can reference places without using the constructor ie. AppPlace.UnitCostPlace
public static AppPlace UnitCostPlace = new AppPlace(Location.unitCost);
public static AppPlace Home = new AppPlace(Location.home);
// The token names match the enum names
private static enum Location {
home,
unitCost;
}
private Location location;
private AppPlace(Location location) {
this.location = location;
}
@Prefix("")
public static class Tokenizer implements PlaceTokenizer<AppPlace> {
public AppPlace getPlace(String token) {
Location location = Location.valueOf(token);
switch(location) {
case home:
return Home;
case unitCost:
return UnitCostPlace;
default:
return null;
}
}
public String getToken(AppPlace place) {
return place.location.name();
}
}
}
The nice thing about this solution is that you replace strings with enum constants so it is more type safe. Also you don't have to construct Places, you just reference them.
Each time you need to add a new place, you just and a new enum constant and a public static AppPlace member variable.
You also get the benefit of using the == operator in your ActivityMapper instead of instanceof.
public class AppActivityMapper implements ActivityMapper {
public Activity getActivity(Place place) {
if(place == AppPlace.Home) {
return new HomeActivity();
} else if(place == AppPlace.UnitCostPlace) {
return new UnitCostActivity();
} else
return null;
}
}
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment