generics

Functional depedency analysis

Developers who have used eclipse cannot miss out the Cntrl+Shift+G combo - the easiest way to find all references to a particular member/method/class in your workspace. Consider a scenario where you are a new guy maintaining a web application written in java. Now, you are about to change a method signature, and you do a Cntl+Shift+G to ...

Visual Studio Intellisense not showing methods on generic overload

Given the following two interfaces (these are small examples, not my actual implementation): public interface IAssertion<T> { IAssertion<T> IsNotNull(); IAssertion<T> Evaluate(Predicate<T> predicate) } public interface IStringAssertion : IAssertion<string> { IStringAssertion IsNotNullOrEmpty(); } and a static factory t...

Java reflection: What does my Collection contain?

Hi guys, I've defined a method in a class: public void setCollection(Collection<MyClass>); and in another class public void setCollection(Collection<OtherClass>); (and really, lots of similar classes) All are in classes with the same superclass, and I have a method in a support-class where I want to call this method and set it wit...

Is it better to have code duplication and have it be very simple/readable, or have no duplication (using generics) but be much more complicated?

In general I come across this a lot. Some of my co-workers prefer very simple, easy to read classes even if that means that there is some code duplication, whereas I do everything in my power to avoid code duplication, even if it means making more a complicated architecture. What is the best practice? I work exclusively in Java. ...

Java: upcasting Collection<T> to Collection<U super T>

I have a Collection<T>. I have a class TManager implementing an interface UManager which has a method getCollection() that needs to return a Collection<U> where U is an interface, and T is a class that implements U. Aside from just casting it, e.g. return (Collection<U>)Tcoll;, is there a more correct way to handle this? I control all ...

How to code a truly generic tree using Generics

Lets say I have a Node class as follows: class Node<T> { T data; List<Node<T>> children; internal Node(T data) { this.data = data; } List<Node<T>> Children { get { if (children == null) children = new ...

Java: Why isn't autoboxing happening here?

This gives me an error: int[] l = new int[] {0, 2, 192, -1, 3, 9, 2, 2}; int[] l2 = new int[] {9001, 7, 21, 4, -3, 11, 10, 10}; int[] l3 = new int[] {5, 5, 5, 64, 21, 12, 13, 200}; Set<List<Integer>> lists = new HashSet<List<Integer>>(); lists.add(Arrays.asList(l)); Eclipse: The method add(List<Integer>) in the type Set<List<Inte...

Java Generics: compareTo and "capture#1-of ?"

The following gives me an error message: public static List<Comparable<?>> merge(Set<List<Comparable<?>>> lists) { List<Comparable<?>> result = new LinkedList<Comparable<?>>(); HashBiMap<List<Comparable<?>>, Integer> location = HashBiMap.create(); int totalSize; for (List<Comparable<?>> l : lists) { location.put(l, 0); totalSiz...

Is List<List<String>> an instance of Collection<Collection<T>>?

I wrote this handy, generic function for converting a collection of collections into a single set: public static <T> Set<T> makeSet(Collection<Collection<T>> a_collection) { Iterator<Collection<T>> it = a_collection.iterator(); Set<T> result = new HashSet<T>(); while (it.hasNext()) { result.addAll(it.next()); } return result; } ...

Java bounded wilcard type

Hi, I need to define a generic class, and the type parameter must be an enum. I think it should look something like public class <T> MyClass<T extends Enum<T>> { } But I can't seem to figure out the exact syntax. I should mention that I need a way to refer to the type (within MyClass) that it is instantiated with. Thanks! ...

Java generics/abstract/innerclass syntax question

I've got the following: public abstract class Foo<T>{ //contents of Foo // ... public class Bar<Q> extends Foo<T>{ //contents of Foo.Bar // ... } } Later, in another class and java file, I am trying to construct an instance of the inner Bar class above, using the outer abstract class as a supertype. ...

Hibernate Component with Generics

Ok, this question is best explained in code. So will try to present the most succinct example I can. Timestamped.java @Embeddable public class Timestamped<E> { private E value; private Date timestamp; ... } Foo.java @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="TYPE") public class...

Casting from Object in Java without getting an unchecked warning

I wrote a class that has a map of <String, Object>. I need it to hold arbitrary objects, but at the same time sometimes I need to cast some of those objects, so I'll do something like HashMap<String, Object> map = new HashMap<String, Object>(); Object foo =...

Why does a string INotifyPropertyChanged property update but not a List<string>?

In the following WPF application, when you click the button, why does TheTitle TextBlock update but FilesCopied ListBox not update? XAML: <Window x:Class="TestList3433.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300...

Learning new languages: Programming Quizzes

Possible duplicates: http://stackoverflow.com/questions/24692/where-can-you-find-fun-educational-programming-challenges http://stackoverflow.com/questions/3088/best-ways-to-teach-a-beginner-to-program http://stackoverflow.com/questions/6327/what-are-your-programming-exercises etc... Hello, I like to learn new programming languages ...

A question about Generic inheritance

public class Leaf : IComponent<Leaf> { //... } Does this type of generic inheritance mechanism have any specific name? What is the benefit of this type of usage of Generics? ...

java: HashMap<String, int> not working

HashMap<String, int> doesn't seem to work but HashMap<String, Integer> does work. Any ideas why? ...

Converting Linq to XML result to generic list in VB.Net. Odd error.

I have a function that works great in C# that I'm converting to VB.Net. I'm having an issue converting the result set to a generic list in VB.net. The code: Public Function GetCategories() As List(Of Category) Dim xmlDoc As XDocument = XDocument.Load("http://my_xml_api_url.com") Dim categories = (From category In xmlDoc.Descendan...

How do I test a generic type variable for equality with Default(T) in Delphi?

I'm trying to write a generic cached property accessor like the following but am getting a compiler error when trying to check whether the storage variable already contains a value: function TMyClass.GetProp<T>(var ADataValue: T; const ARetriever: TFunc<T>): T; begin if ADataValue = Default(T) then // <-- compiler error on this line ...

How to access derived class members from an interface?

I have three classes; Stamp, Letter and Parcel that implement an interface IProduct and they also have some of their own functionality. public interface IProduct { string Name { get; } int Quantity { get; set; } float Amount { get; } } public class Stamp : IProduct { public string Name { get { return "Stamp"; } } ...