generics

Generic enum JPA AttributeConverter implementation

╄→гoц情女王★ 提交于 2021-02-18 07:39:28
问题 Problem I am trying to solve I am trying to implement enum mapping for Hibernate. So far I have researched available options, and both the @Enumerated(EnumType.ORDINAL) and @Enumerated(EnumType.STRING) seemed inadequate for my needs. The @Enumerated(EnumType.ORDINAL) seems to be very error-prone, as a mere reordering of enum constants can mess the mapping up, and the @Enumerated(EnumType.STRING) does not suffice too, as the database I work with is already full of values to be mapped, and

The type T is not generic; it cannot be parameterized with arguments <?> error in a generic function

 ̄綄美尐妖づ 提交于 2021-02-18 06:05:33
问题 I want to create a generic function that takes any Map & a String key, if the key is not present in the map, then it should create a new instance of the Value Type (which is passed) & put it in the map & then return it. Here is my implementation public <T> T getValueFromMap(Map<String, T> map, String key, Class<T> valueClass){ T value = map.get(key); if (value == null){ try { value = valueClass.newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e

Java thenComparing wildcard signature

牧云@^-^@ 提交于 2021-02-17 19:14:27
问题 Why does the declaration look like this: default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor) I understand most of it. It makes sense that U can be anything as long as it's comparable to a superclass of itself, and thus also comparable to itself. But I don't get this part: Function<? super T, ? extends U> Why not just have: Function<? super T, U> Can't the U just parameterize to whatever the keyExtractor returns, and still

Java thenComparing wildcard signature

社会主义新天地 提交于 2021-02-17 19:14:01
问题 Why does the declaration look like this: default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor) I understand most of it. It makes sense that U can be anything as long as it's comparable to a superclass of itself, and thus also comparable to itself. But I don't get this part: Function<? super T, ? extends U> Why not just have: Function<? super T, U> Can't the U just parameterize to whatever the keyExtractor returns, and still

Is there a difference when specifying upper bounds for wildcards explicitly?

醉酒当歌 提交于 2021-02-17 08:55:34
问题 Suppose I have a generic class Generic<A extends BaseType> . Is there a notable difference, as far as the Java Language Specification is concerned, between the following two type declarations? Generic<?> Generic<? extends BaseType> What about nested wildcards? List<Generic<?>> List<Generic<? extends BaseType>> Thinking about this, I would assume these to be equivalent. Generic specifies that the type parameter A has BaseType for an upper bound. Thus, the wildcard should always be

How to conditionally invoke a generic method with constraints? [duplicate]

让人想犯罪 __ 提交于 2021-02-17 08:27:34
问题 This question already has answers here : How do I use reflection to call a generic method? (8 answers) Closed 6 years ago . Suppose I have an unconstrained generic method that works on all types supporting equality. It performs pairwise equality checks and so works in O(n 2 ) : public static int CountDuplicates<T>(IList<T> list) { /* ... */ } I also have a constrained generic method that only works with types supporting sorting. It starts from sorting the list in O(n log n) , and then counts

Resolve a generic type without specifying it on the interface

╄→гoц情女王★ 提交于 2021-02-17 05:30:10
问题 I have the following hierarchy of interfaces: public interface IEntity { object Key; } public interface IEntity<TKey> { TKey TypedKey; } In my repository layer, I have a GetOne method currently defined as such: public class Repository<TEntity> : IRepository<TEntity> where TEntity : IEntity { public TEntity GetOne(object key) { ... } } I would like to constrain the argument on the repository to the TKey type specified by the generic interface of the entity. The obvious solution would be:

Why this works in C# (generic class and self-reference)?

为君一笑 提交于 2021-02-17 04:59:29
问题 I have class X<T> : Base { //For exemple: static T something(); } And I can have class A : X <A> { } To logically have something like this: class A : Base { static A something(); } This works and works well. But in my comprehension, it's kind of self-reference (A is the children of X, while X doesn't exists before A...), which is breaks the foundation of computer science, so I want to know what's wrong with my comprehension?? 回答1: It's totally fine. You can do similar without generics too:

Trying to write a generic function for parsing JSON into codable Structs

泪湿孤枕 提交于 2021-02-17 04:45:43
问题 I'm currently parsing JSON like this struct ExampleStruct : Codable { init() { } // implementation } if let jsonData = jsonString.data(using: .utf8) { do { let decoder = JSONDecoder() let object = try decoder.decode(ExampleStruct.self, from: jsonData) } catch { print("Coding error - \(error)") } } This works fine, however I've been trying to learn about Generics over the weekend. I'm trying to write a method which I pass in a Codable struct type and a string of JSON which returns the object

Trying to write a generic function for parsing JSON into codable Structs

徘徊边缘 提交于 2021-02-17 04:45:22
问题 I'm currently parsing JSON like this struct ExampleStruct : Codable { init() { } // implementation } if let jsonData = jsonString.data(using: .utf8) { do { let decoder = JSONDecoder() let object = try decoder.decode(ExampleStruct.self, from: jsonData) } catch { print("Coding error - \(error)") } } This works fine, however I've been trying to learn about Generics over the weekend. I'm trying to write a method which I pass in a Codable struct type and a string of JSON which returns the object