frjaeger220 / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Convenience methods for type literals for collections #123

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Suppose you could write:

TypeLiteral.setOf(String.class);
TypeLiteral.listOf(String.class);

There's a limit to how much we can do, but we could avoid horrible
TypeLiteral syntax for common cases.  (Of course, you can easily do this
yourself.)

Original issue reported on code.google.com by bslesinsky on 29 Jun 2007 at 5:34

GoogleCodeExporter commented 9 years ago
It turned out not to be so easy, but this seems to do it, assuming an 
implementation
of TypeWithArgument:

  public static <T> TypeLiteral<List<T>> listOf(Class<T> parameterType) {
    TypeWithArgument type = new TypeWithArgument(List.class, parameterType);
    return (TypeLiteral<List<T>>) TypeLiteral.get(type);
  }

Original comment by bslesinsky on 29 Jun 2007 at 7:35

GoogleCodeExporter commented 9 years ago
Great idea; also take a look at the FactoryMethods class I made for issue #18.
Perhaps we should put 'em there. Like: typeForListOf(String.class);

Original comment by robbie.v...@gmail.com on 10 Aug 2007 at 11:03

GoogleCodeExporter commented 9 years ago
Something like this?

Original comment by earwin@gmail.com on 1 Nov 2007 at 8:26

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the code.

I think this is a good idea, and a good example of the kind of stuff that 
should go
in the new com.google.inject.util package.

Original comment by kevin...@gmail.com on 1 Nov 2007 at 10:44

GoogleCodeExporter commented 9 years ago

Original comment by bslesinsky on 18 Nov 2007 at 3:38

GoogleCodeExporter commented 9 years ago
Let's do this. The methods I think we should make public:
  newParameterizedType(Class rawType, Class... args)
  newGenericArrayType(Class componentType)
What should we call the factory class? com.google.inject.util.Types?

I don't think we want to make these ones public:
  toString(Type type)
  equals(Type a, Type b);
  hashCode(Type type);
  canonicalize(Type type);
  wrapPrimitives(Type type);
  getRawType(Type type);

Original comment by limpbizkit on 5 Jun 2008 at 7:39

GoogleCodeExporter commented 9 years ago
My suggestions:

Types.newParameterizedType(Class rawType, Class...)
Types.arrayOf(Class componentType)
// convenience
Types.setOf(Class componentType)
Types.listOf(Class componentType)

Original comment by bslesinsky on 5 Jun 2008 at 4:00

GoogleCodeExporter commented 9 years ago
I second #7 for convenience methods. Why not supply them for all jdk collection
interfaces, as in my code above?

Also, I want this so be somehow possible:
qwe = listOf(setOf(SomeClass.class)); // yields TypeLiteral of 
List<Set<SomeClass>>

Original comment by earwin@gmail.com on 5 Jun 2008 at 4:10

GoogleCodeExporter commented 9 years ago
Checked in the fix. The methods I decided to go with (as inspired by bslesinsky 
and earwin):
 - newParameterizedType
 - newParameterizedTypeWithOwner
 - arrayOf
 - listOf
 - setOf
 - mapOf
 - providerOf

Original comment by limpbizkit on 10 Jun 2008 at 6:39

GoogleCodeExporter commented 9 years ago
Can you please also add collectionOf()?
I ran into a situation where I want to bind a class under the base Collection
interface. Sometimes you want to expose only the minimal possible information 
about a
 collection and hide the fact that it is ordered. Also this will make the collections
support more compelte.
Also "iterableOf()" might not be a bad idea.

Original comment by Rinsvind@gmail.com on 1 Jan 2009 at 4:46

GoogleCodeExporter commented 9 years ago
Any types we don't support out-of-the-box you can create yourself with 
Types.newParameterizedType().

For example,
  Type collectionOfString = Types.newParameterizedType(Collection.class, String.class);
  Type iterableOfShort = Types.newParameterizedType(Iterable.class, Short.class);

Original comment by limpbizkit on 1 Jan 2009 at 5:39