How to interpret Java generators such as <T> T, <T, V> Query <T>, Class <T>?
There is a Java API: Morphia
The Morphia class defines a method of type:
<T> T fromDBObject(Class<T> entityClass, com.mongodb.DBObject dbObject) Can someone explain what <T> T and Class<T> mean? Does the method return a class of type T in the collection?
Another API for Integr DataStore:
<T,V> Query<T> find(Class<T> clazz, String property, V value) What <T,V> Query<T> means? Is the method returning an object of type Query that is then surrounded by collection `<T,V>` and `<T>`. This part is very confusing. Is it right to say when an angle bracket (< > ) is involved, is Java always implied?
Sorry, I forgot to mark some content in this question as code, otherwise SO escaped from changing the entire value of the question, and therefore the answers to 2 @@ Vash and @fiver answer the question before this edit.
Thanks for pointing out some tutorials, but please provide a specific answer from your experience in Java generators to a question that will then help me better understand the tutorials.
Whenever you see <>, this means that the generics class is not just a collection.
A class is just an example of a generic class: http://download.oracle.com/javase/6/docs/api/java/lang/Class.html
T in the declaration means an object of any type (T - type).
Thus, the DBFromObject method is a general method, where the generic type is denoted by T, which returns an object of this generic type T and accepts a parameter of type Class<T> .
Generics are not limited to collections, but collections are good examples to understand why you need generics and how you can use them.
Suppose you create a new List object, and know that it will contain only certain types of objects (for example, MyClass objects). If you can somehow specify it in your code, and you can check it automatically, you can make sure that no other type of object will be added to this list. Moreover, when other developers read your code, they easily understand that this list contains only MyClass objects.
Generalizations allow us to include such information in our code without rewriting the List class. In fact, the functionality of List is independent of the type of objects it includes. So, to create your own List , you write this code:
List<MyClass> myList = new List<MyClass>(); Now you can imagine cases when you can specify more than one type. A good example of this is the Map class. You can define types of keys and values. So, you see <K,V> in the declaration of the Map class ( Map<K,V> ).
In addition to classes, generics can also be used in a method / constructor declaration. In your example:
<T,V> Query<T> find(Class<T> clazz, String property, V value) There is a method that has two generic types T and V, and you can specify them (or they can be output by the compiler) when calling the method. Here <T,V> in the declaration explicitly indicates this method function.
public <T> T fromDBObject(Class<T> entityClass, com.mongodb.DBObject dbObject) is a good example of using Class Literals as Runtime type tokens . The ad is very similar to the example at the bottom of the page . One reading might be: "Given a class literal of type T with the names entityClass and dbObject , the fromDBObject() method returns an object of type T. In this case, the symbol T denotes the type that represents the Class object.