Implementing Places that share all but have different prefix

182 views
Skip to first unread message

tanteanni

unread,
Mar 8, 2012, 6:37:19 AM3/8/12
to google-we...@googlegroups.com
Sometimes i have two places that differ only in thier prefix. For example a "SelectionPlace" and a "ResultOfSelectionPlace". So both places share all code but differ in @Prefix(value"..."). At the moment i implemented this by using an abstract class but i have to copy the code of the tokenizer to get tokenizers with different prefixes.

Is there a way to let the concrete class only set the prefix of a shared tokenizer class? Or is there probably a complete different way to achieve this?

thx in advance

Thomas Broyer

unread,
Mar 8, 2012, 7:10:09 AM3/8/12
to google-we...@googlegroups.com


On Thursday, March 8, 2012 12:37:19 PM UTC+1, tanteanni wrote:
Sometimes i have two places that differ only in thier prefix. For example a "SelectionPlace" and a "ResultOfSelectionPlace". So both places share all code but differ in @Prefix(value"..."). At the moment i implemented this by using an abstract class but i have to copy the code of the tokenizer to get tokenizers with different prefixes.

Is there a way to let the concrete class only set the prefix of a shared tokenizer class? Or is there probably a complete different way to achieve this?

I believe it's possible using a parameterized tokenizer, a PlaceHistoryMapperWithFactory and using the @Prefix on the factory methods rather than on the tokenizer class:

class FooPlace extends AbstractMyPlace { }
class BarPlace extends AbstractMyPlace { }
abstract class AbstractMyPlace extends Place {
    static class Tokenizer<T extends AbstractMyPlace> extends PlaceTokenizer<T> { … }
    …
}

class MyFactory {
  @Prefix("foo")
  public PlaceTokenizer<FooPlace> foo() { return new AbstractMyPlace.Tokenizer<FooPlace>(); }
  @Prefix("bar")
  public PlaceTokenizer<BarPlace> bar() { return new AbstractMyPlace.Tokenizer<BarPlace>(); }
}

tanteanni

unread,
Mar 8, 2012, 9:28:18 AM3/8/12
to google-we...@googlegroups.com
thx thomas (you seem to answer all "a&p" questions of the world)

so you mean i have to provide an implementation (rather then an extension) of "PlaceHistoryMapper"? will @WithTokenizers work on that? Or do i have to implement all stuff for all places? or to make it simple (at least for me ;-)) could you probably  give me a draft of " PlaceHistoryMapperWithFactory".

Thomas Broyer

unread,
Mar 8, 2012, 9:50:01 AM3/8/12
to google-we...@googlegroups.com

On Thursday, March 8, 2012 3:28:18 PM UTC+1, tanteanni wrote:
thx thomas (you seem to answer all "a&p" questions of the world)

so you mean i have to provide an implementation (rather then an extension) of "PlaceHistoryMapper"? will @WithTokenizers work on that? Or do i have to implement all stuff for all places? or to make it simple (at least for me ;-)) could you probably  give me a draft of " PlaceHistoryMapperWithFactory".


You use it the exact same way as PlaceHistoryMapper (you can use @WithTokenizers too) except you have to provide a factory via setFactory() before you can use it. the generated implementation will call methods of the factory instead of instantiating tokenizers, when needed. This is mostly useful when you have to somehow inject dependencies into your tokenizers (and note that the factory could be your Ginjector; the generator only looks at zero-arg methods returning subtypes of PlaceTokenizer)

tanteanni

unread,
Mar 8, 2012, 10:34:38 AM3/8/12
to google-we...@googlegroups.com
thx again - didn't know about that.
(i can't shake the feeling that google has some good engineers)
Reply all
Reply to author
Forward
0 new messages