generics

Can unchecked warnings be avoided when overriding a method with raw type parameters?

I am extending a class defined in a library which I cannot change: public class Parent { public void init(Map properties) { ... } } If I am defining a class 'Child' that extends Parent and I am using Java 6 with generics, what is the best way to override the init method without getting unchecked warnings? public class Child exten...

Get the type of the Generic parameter

I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase { } public class ImplementB:InterfaceBase { } public void TestImplementType<T>(T obj) where T: InterfaceBase { } How to infer what the T is whether ImplementA or ImplementB? I tried to use typeof(T) is ImplementA but this expre...

Builder design pattern with inheritance: is there a better way?

I'm creating a series of builders to clean up the syntax which creates domain classes for my mocks as part of improving our overall unit tests. My builders essentially populate a domain class (such as a Schedule) with some values determined by invoking the appropriate WithXXX and chaining them together. I've encountered some commonalit...

Why is SomeClass<? super T> not equivalent to SomeClass<T> in Java generic types?

I noticed the specificaition for Collections.sort: public static <T> void sort(List<T> list, Comparator<? super T> c) Why is the "? super" necessary here? If ClassB extends ClassA, then wouldn't we have a guarantee that a Comparator<ClassA> would be able to compare two ClassB objects anyway, without the "? super" part? In other word...

Why does this generic method require T to have a public, parameterless constructor?

public void Getrecords(ref IList iList,T dataItem) { iList = Populate.GetList<dataItem>() // GetListis defined as GetList<T> } dataItem can be my order object or user object which will be decided at run time.The above does not work as it gives me this error The type 'T' must have a public parameterless constructor in order to use i...

string.split returns a string[] I want a List<string> is there a one liner to convert an array to a list?

Lists in c# have the .toArray() function. I want the inverse, where an array is transformed into a list. I know how to create a list and loop through it but i would like a one liner to swap it back. I am using the string.split function in the .NET 2.0 environment so Linq etc. is not available to me. ...

Java Generics (Wildcards)

I have a couple of questions about java generics wildcards: 1) whats the difference between List<? extends T> and List<? super T> 2) What is a bounded wildcard and what is an unbounded wildcard? Thanks! ...

Advanced Java Generics question: why do we need to specify redundant information

Hi, I've got some generic class for my JPA model POJO that goes like this: public interface Identifiable<PK extends Serializable> { PK getUniqueId(); } public interface GenericDao<T extends Identifiable<PK>> { public T findById(PK id); } This code won't compile. For this to work, I need to specify public interface GenericD...

Map of collections

I wanted to make Map of Collections in Java, so I can make something like public void add(K key, V value) { if (containsKey(key)) { get(key).add(value); } else { Collection c = new Collection(); c.add(value); put(key, value); } } I've tried to make it with something like public class ...

Type mismatch with generics

Here's an interface: public interface Foo<T> extends Comparable<Foo<T>> { ... } And there are some classes implementing this interface: public class Bar extends Something implements Foo<Something> { public Vector<Foo<Bar>> giveBar() { ... } } public class Boo extends SomethingElse implements Foo<SomethingElse> { ...

Is this a bug in Delphi 2009?

It certainly looks like a bug, but I only have the trial version so it may have been fixed. ITestInterface = interface ['{9445CED8-4DBA-4EDB-9897-60980B438BE4}'] procedure Foo1; procedure Foo2; end; TTest = class(TInterfacedObject, ITestInterface) end; The above will rightly not compile. but the following does! ITestInterface...

How do I handle Delphi Simple types when using generics?

A little example TTest<T> = class private f : T; public function ToString : string; end; If is an object then this should work TTest<T>.ToString; begin Result := f.ToString; end; But what happens when is say an integer? This would be ok in .net. of course. I know it won't work, but how do I code this to work with objects A...

Generic Parent For Generic Class

I am using a fictional example for this. Say, I have a Widget class like: abstract class Widget { Widget parent; } Now, my other classes would be derived from this Widget class, but suppose I want to put some constraint in the class while defining the derived types such that only a particular "type" of widget can be parent to a partic...

"Is a" vs "Has a" : which one is better?

Portfolio A -> Fund 1 Portfolio A -> Fund 2 Portfolio A -> Fund 3 I couldn't frame my sentence without not using is/has. But between 1 & 2, 1) has a: class PortfolioA { List<Fund> obj; } 2) is a: class PortfolioA : List<Fund>{...} which one do you think is better from the point of extensibility, usableness, and what have you...

HashMap default types for K and V

I usually type my map declarations but was doing some maint and found one without typing. This got me thinking (Oh No!). What is the default typing of a Map declaration. Consider the following: Map map = new HashMap(); map.put("one", "1st"); map.put("two", new Integer(2)); map.put("three", "3rd"); for ( Map.Entry entry : map.entrySet...

Pass An Instantiated System.Type as a Type Parameter for a Generic Class

The title is kind of obscure. What I want to know is if this is possible: string typeName = <read type name from somwhere>; Type myType = Type.GetType(typeName); MyGenericClass<myType> myGenericClass = new MyGenericClass<myType>(); Obviously, MyGenericClass is described as: public class MyGenericClass<T> Right now, the compiler co...

Using generic parameters with static compareObject method

I'd like to remove all "unchecked" warnings from this general utility method (part of a larger class with a number of similar methods). In a pinch, I can use @SuppressWarnings("unchecked") but I'm wondering if I can use generics properly to avoid the warning. The method is intended to be allow callers to compare two objects by passing...

Get a generic method without using GetMethods

I want to get the method System.Linq.Queryable.OrderyBy(the IQueryable source, Expression> keySelector) mehthod, but I keep coming up with nulls. var type = typeof(T); var propertyInfo = type.GetProperty(group.PropertyName); var propertyType = propertyInfo.PropertyType; var sorterType = typeof(Func<,>).MakeGenericType(type, propertyTyp...

What are alternatives to generic collections for COM Interop?

I am attempting to return a collection of departments from a .NET assembly to be consumed by ASP via COM Interop. Using .NET I would just return a generic collection, e.g. List<Department>, but it seems that generics don't work well with COM Interop. So, what are my options? I would like to both iterate over the list and be able to acce...

Iterate through generic list of unknown type at runtime (VB.Net)

Does anyone know how to iterate over a generic list if the type of that list isn't known until runtime? For example, assume obj1 is passed into a function as an Object: Dim t As Type = obj1.GetType If t.IsGenericType Then Dim typeParameters() As Type = t.GetGenericArguments() Dim typeParam As Type = typeParamete...