generics

Java Generics Question

Alright, I thought I understood generics pretty well, but for some reason I can't get my head wrapped around why this doesn't work. I have two classes, or I should say that Google has two classes (I'm trying to implement their Contacts API). They have a ContactEntry class (abbreviated below): package com.google.gdata.data.contacts; pub...

Constructor constraint on generic types or simply check for constraint within my generic type constructor?

I have a generic class DirectorySource<T> which depends on an interface IDirectorySearch<T>. Both are generics and have the same type constraint: public class DirectorySource<T> where T : IDirectoryEntry { } public interface IDirectorySearcher<T> where T : IDirectoryEntry { } So, for instance, when I want a source to manipulate grou...

java syntax question

What do these lines of code do? private interface ComparisonCallback<ComparisonT> { public ComparisonT getComparisonValue(CVRDataElement e); } followed by this method declaration: public <ComparisonType> List<MyDataTable> getGenericSubTable(ComparisonCallback<ComparisonType> cc) Specifically, I don't understand the ComparisonTy...

Java code reuse through Inheritance when generics are involved (List in particular)?

I have very little knowledge of generics other than how to 'use them'. In my app I have various ListView Activity that seem to share similar methods and variables and have the exact 'algorithm' in the sense that the steps performed are all very much the same. For example in my onCreate method I call a method to add a footer, start a pr...

Avoiding compiler warning when implementing interface with bounded return type

How can I implement the ContactService interface without getting warning about unchecked conversions from the compiler: interface ContactsService { <T extends Response> T execute(Action<T> action); } interface Action<T extends Response> { } interface Response{ } class GetDetailsResponse implements Response {} If I return an ins...

Using generic collections in Silverlight user controls

There is an interface: public interface IFoo { } A Silverlight user control has a collection of IFoo instances: public ObservableCollection<IFoo> Items { get; set; } There is an abstract class that implements the interface: abstract public class Foo : IFoo {} And a class that further derives from that: public class DerivedFoo :...

Java Generics: Cannot cast List<SubClass> to List<SuperClass>?

Just come across with this problem: List<DataNode> a1 = new ArrayList<DataNode>(); List<Tree> b1 = a1; // compile error: incompatible type Where the type DataNode is a subtype of Tree. public class DataNode implements Tree To my surprise, this works for array: DataNode[] a2 = new DataNode[0]; Tree[] b2 = a2; // this is okay T...

Stacking generics

Is this bad practice? ArrayList<ArrayList<ArrayList<Double>>> list = new ArrayList<ArrayList<ArrayList<Double>>>(); ...

Generating methods with generic types with Asm bytecode generator (ClassWriter)

Defining simple getters and setters is easy using Asm (and fortunately it is even explained in their FAQ). But one thing that is not mentioned, and for which I have been unable to find documentation, is how to implement these using generic type information. I am actually able to determine generic type information itself quite easily (si...

Using generic arguments on WPF Window defined in XAML

I'm trying to create a Window-derived class in XAML which can take a generic argument, but I can't seem to define the generic argument in the XAML so that it generates the partial class matching my code-behind file. What I'm trying to accomplish is a replacement for all the MessageBox calls for asking the user questions, where I can giv...

Anti-constraint on C# generics

Inspired by Phil Haack's attempt on null or empty coalescing, I'm trying to write a couple of extension methods for the string object, as well as on the IEnumerable<T> interface, to simplify null or emtpy ckecking. However, I'm running into problems: when I'm attempting to call the string version of AsNullIsEmpty, the compiler treats my ...

Java Generics - type declaration

class Response<T> { ... } Response response = new Response(); The above code compiles. I don't understand what's implied though. Shouldn't the compiler require a type specification for 'T'? e.g. something like the following: Response<String> response = new Response<String>(); ...

Feature or Bug:Why does this Java code compile?

Possible Duplicate: Is this valid Java? I was surprised to discover the Java class below compiles. It has several method, with the same name, number of arguments and following type-erasure types of argument. Yet, it compiles and works as expected, on windows using various versions of the Sun JDK 1.6 compiler. So if this is a ...

How to EasyMock a call to a method that returns a wildcarded generic?

We're looking at switching to Spring 3.0 and running into problems with the intersection of Spring 3.0, EasyMock, and Java Generics. In one place, we're mocking a Spring 3.0 AbstractBeanFactory, specifically this method: public Class<?> getType(String name) throws NoSuchBeanDefinitionException { ... } Under earlier versions of Spring...

Does List<T> create garbage in C# in foreach

Correct me if im wrong but doing a foreaver an IEnumerable<T> creates garbage no matter what T is. But I'm wondering if you have a List<T> where T is Entity. Then say there is a derived class in the list like Entity2D. Will it have to create a new enumerator for each derived class? Therefore creating garbage? Also does having an interf...

Determine generic type parameters at run-time in Java

class Json<T> { @SerializedName( "T's type here" ) private final ArrayList<T> _bucket = new ArrayList<T>( 5 ); ... } I'd like to know how (if possible) the generic parameters of a class can be determined at run-time. From what I've read this is possible with sub-classes of generic types, but I haven't been able to find out how ...

Is there a threadsafe and generic IList<T> in c#?

Is List<T> or HashSet<T> or anything else built in threadsafe for addition only? My question is similar to http://stackoverflow.com/questions/1278010/threadsafe-and-generic-arraylist-in-c but I'm only looking for safety to cover adding to this list threaded, not removal or reading from it. ...

C# Generics and abstract factory pattern - or some way of doing something similar

I am trying to write a sort of extendable data layer for my application One of the "repositories" is an in-memory implementation of my abstract store class public abstract class Store<TEntity, TIdentifier> : IStore<TEntity, TIdentifier> where TEntity : StorableEntity<TIdentifier> { //abstract methods here public abs...

"Equivalent" is equivalent to... in concept definitions

What does "equivalent" mean in the definition of concepts, such as http://www.boost.org/doc/libs/1_43_0/libs/utility/CopyConstructible.html? Shallow copy? Deep copy? Either? ...

Can I cast from a Generic type to an Enum in C#?

I'm looking to write a utility function that gets a integer from the database and returns a typed Enum to the application. Here is what I tried to do (note I pass in a datareader and columnname instead of the int in my real function)... public static T GetEnum<T>(int enumAsInt) { Type enumType = typeof(T); Enum...