generics

Using generics and jpa EntityManager methods

Deadly 提交于 2020-01-13 06:06:32
问题 Can I use generics and JPA together? I am trying to persist objects of four classes to my db. Here's my PersistService class: public class PersistService<T> { private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("fileUploadProject"); public static EntityManager getEntityManager() { return emf.createEntityManager(); } // Write Client to Database public static <T> void persist(T obj) { EntityManager em = getEntityManager(); EntityTransaction et = em.getTransaction();

How do I solve the “cannot implicitly convert [type] to [interface]” error when the interface has multiple generic parameters?

落花浮王杯 提交于 2020-01-13 05:16:49
问题 I have the following interfaces: public interface IQueryHandler<in TQuery, out TResult> where TResult : class where TQuery : IQuery<TResult> { TResult Handle(TQuery query); } public interface IQuery<TResult> // Doesn't require anything special, just used to guarantee that IQueryHandlers get the right parameters. { } It's intended to be used by IQueryHandlers which will take in an IQuery<TResult> which defines a query that returns an object of type TResult . The IQueryHandler 's Handle method

Proguard is ruining my cleanliness. Gson and generics

丶灬走出姿态 提交于 2020-01-13 03:12:18
问题 I have a function that loads information from persistence, and I just tell the type it is in a really simple way. The class is called SharedPreferencesHelper.kt so it is a really life problem solver: fun <T> loadList(context: Context, fileName: String, key: String, defValue: ArrayList<T>) : ArrayList<T> { val gson = MyGsonDependency.getInstance() val json = getWithFileName(context, fileName).getString(key, gson.toJson(defValue)) return gson.fromJson(json, object : TypeToken<ArrayList<T>>() {}

Why can't Java convert an ArrayList<TreeSet<Integer>> into a List<Set<Object>>?

别来无恙 提交于 2020-01-13 02:55:27
问题 I'm attempting to do something that seems rather simple: sum the sizes of a list of sets. Netbeans gives the following warning/error: actual argument java.util.ArrayList<java.util.TreeSet<java.lang.Integer>> cannot be converted to java.util.List<java.util.Set<java.lang.Object>> by method invocation conversion for the following two pieces of code: /** * Sums the sizes of all sets in a list. Note that while there will be * no duplicate elements in a single set, "sister" sets may contain *

Abstract generic classes taking type parameters that are themselves derived from that class

妖精的绣舞 提交于 2020-01-13 02:46:11
问题 Do you consider it an acceptable or a bad practice to create an abstract generic class that takes as a type parameter a class that derives from itself? This allows the abstract generic class to manipulate instances of the derived class and in particular the ability to create new() instances of the derived class as necessary and can help avoid repeat code in the concrete classes that derive from it. If 'bad' what alternative do you prefer to handle such situations and how would you structure

C#: Is a private inner interface possible?

无人久伴 提交于 2020-01-12 19:09:47
问题 I have a generic class X<T> ; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T> . However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private . I.e., inside the class itself, I can upcast to IX<T> and use it covariantly. E.g.: class X<T> : IX<T> { private interface IX<out T>{ // the private covariant interface void foo(); } // It grants access to

C#: Is a private inner interface possible?

房东的猫 提交于 2020-01-12 19:09:28
问题 I have a generic class X<T> ; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T> . However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private . I.e., inside the class itself, I can upcast to IX<T> and use it covariantly. E.g.: class X<T> : IX<T> { private interface IX<out T>{ // the private covariant interface void foo(); } // It grants access to

Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet<E> but does not override it

痴心易碎 提交于 2020-01-12 15:26:20
问题 import java.util.*; class A extends HashSet<Integer> { public boolean add(Object obj){ //compiler error return true; } } or class Abc <T> { public void add(T t){} //compiler error public void add(Object i){} //compiler error (can't overload?) } Error:Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet but does not override it i do not know what is the concept behind above error can any one suggest where i can study this concept? 回答1: The concept at

Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet<E> but does not override it

一笑奈何 提交于 2020-01-12 15:24:04
问题 import java.util.*; class A extends HashSet<Integer> { public boolean add(Object obj){ //compiler error return true; } } or class Abc <T> { public void add(T t){} //compiler error public void add(Object i){} //compiler error (can't overload?) } Error:Name clash: The method add(Object) of type test2 has the same erasure as add(E) of type HashSet but does not override it i do not know what is the concept behind above error can any one suggest where i can study this concept? 回答1: The concept at

Generics with interfaces in F#

主宰稳场 提交于 2020-01-12 15:14:25
问题 In C# one can state that a generic parameter must implement a certain interface like so: public class Something<T> where T : IComparable { ... } How does one specify this in F#? 回答1: Generic constraints use "when" in F#: type Foo<'a when 'a :> IComparable> = member x.Bla = 0 来源: https://stackoverflow.com/questions/770143/generics-with-interfaces-in-f