generics

How to use interfaces in exception handling

Hi, I'm working on the exception handling layer for my application. I have read few articles on interfaces and generics. I have used inheritance before quite a lot and I'm comfortable with in that area. I have a very brief design that I'm going to implement: public interface IMyExceptionLogger { public void LogException(); // ...

Java generics question with wildcards

Just came across a place where I'd like to use generics and I'm not sure how to make it work the way I want. I have a method in my data layer that does a query and returns a list of objects. Here's the signature. public List getList(Class cls, Map query) This is what I'd like the calling code to look like. List<Whatever> list = get...

Question marks in Java generics.

I tried to make sure this wasn't a duplicate post, sorry if I was blind. This is a small snippet of code taken from some of the examples that accompany the Stanford Parser. I've been developing in Java for about 4 years, but have never had a very strong understanding of what this style of code is supposed to indicate. List<? e...

C# Generic List constructor gives me a MethodAccessException

Hi, I make a list in my code like so: List<IConnection> connections = new List<IConnection>(); where IConnection is my own interface. This is in a .NET 2.0 executable. If I run the code on my machine (with lots of .Net versions installed) it works fine. If I run it on my test machine (which only has .NET 3.5 SP1 installed) then I get...

C5 Generics Collection IntervalHeap<T> -- getting an IPriorityQueueHandle from a T for Replace or Delete op

I'm using the Generics Collection library C5 (server down :-( ) and I have an IntervalHeap(T) and I need to Delete or Replace a T that is not the Max or Min. How do I get an IPriorityQueueHandle from my T? The C5 library source code shows that IPriorityQueueHandle(T) has no methods or properties to implement and the compiler thinks my im...

Java-syntax for explicitly specifiying generic arguments in method calls

What is the syntax for explicitly giving the type parameters for a generic Java method? ...

Java noob: generics over objects only?

Hi all, I'm new to Java. While coding a Map<>, I found out that declaring Map<int, int> is a syntax error while Map<Integer, Integer> is OK. Is it only possible in Java to instantiate generics over object types, as opposed to primitives? If so, is there a noticeable performance penalty for boxing/unboxing of primitives? ...

Explicit method tables in C# instead of OO - good? bad?

Hi! I hope the title doesn't sound too subjective; I absolutely do not mean to start a debate on OO in general. I'd merely like to discuss the basic pros and cons for different ways of solving the following sort of problem. Let's take this minimal example: you want to express an abstract datatype T with functions that may take T as inp...

Is this a limitation with Generics in Java?

I want to define the following class as such: public class CollectionAttribute<E extends Collection<T>> { private String name; private E value; public CollectionAttribute(String name, E value) { this.name = name; this.value = value; } public E getValue() { return value; } public void addValue(T ...

Java generics question

So I have 3 classes. Abstract class A Class B extends class A independent Class C In class D that contains the main method, I create a list of instances of class B List<B> b = methodCall(); // the method returns a list of instances of class B Now in class C I have one method that is common to both A and B, and hence I don't wa...

Help Fluent NHibernate Mapping a Generic Class

Hi, I'm hoping somebody can help me with this, I'm trying to write the mapping classes for a class that is subclassed into a generic class. Its easier to descibe with code, so here is my model... public abstract class TagBase { public virtual int Id { get; private set; } public virtual TagTypeEnum TagType { get; set; } publ...

Catch a generic exception in Java?

We use JUnit 3 at work and there is no ExpectedException annotation. I wanted to add a utility to our code to wrap this: try { someCode(); fail("some error message"); } catch (SomeSpecificExceptionType ex) { } So I tried this: public static class ExpectedExceptionUtility { public static <T extends Exception> void check...

Java. Best procedure to de-serialize a Java generic object?

What is the best procedure for storing and retrieving, using native Java serialization, generic objects like ArrayList<String>? Edit: To clarify. When I serialize an object of type ArrayList<String> I'd like to de-serialize to the same type of object. However, I know of no way to cast back to this generic object without causing warni...

XML Serializing a class with a Dictionary<string, List<string>> object

Is it possible to implement IXmlSerializable and in my XML file capture an object of type Dictionary> ? I have the following public class coolio : IXmlSerializable { private int a; private bool b; private string c; private Dictionary<string, List<string>> coco; public coolio(int _a, bool _b, string _c, Dictionary<string, List<string>>...

Annotation to make available generic type

Given an generic interface like interface DomainObjectDAO<T> { T newInstance(); add(T t); remove(T t); T findById(int id); // etc... } I'd like to create a subinterface that specifies the type parameter: interface CustomerDAO extends DomainObjectDAO<Customer> { // customer-specific queries - incidental....

How to properly mix generics and inheritance to get the desired result?

My question is not easy to explain using words, fortunately it's not too difficult to demonstrate. So, bear with me: public interface Command<R> { public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command } public abstract class BasicCommand<R> implements Command<R> ...

Testing a Generic Class

More than a question, per se, this is an attempt to compare notes with other people. I wrote a generic History class that emulates the functionality of a browser's history. I am trying to wrap my head around how far to go when writing unit tests for it. I am using NUnit. Please share your testing approaches below. The full code for the ...

Namespace constraint with generic class declaration

I would like to know if (and if so how) it is possible to define a namespace as a constraint parameter in a generic class declaration. What I have is this: namespace MyProject.Models.Entities <-- Contains my classes to be persisted in db namespace MyProject.Tests.BaseTest <-- Obvious i think Now the decleration of my 'BaseTest' cla...

Getting the type of a parametrized class parameter?

I have the following class public class MyClass<T> { public Class<T> getDomainClass() { GET THE CLASS OF T } } I've googled this problem and all the answers I could find told me to use getGenericSuperClass(), but the problem of this method is that I must have a second class that extends MyClass and I don't want to do...

Why can't I pass an object of type T to a method on an object of type <? extends T>?

In Java, assume I have the following class Container that contains a list of class Items: public class Container<T> { private List<Item<? extends T>> items; private T value; public Container(T value) { this.value = value; } public void addItem(Item<? extends T> ite...