?
Solved

Recursive Generics

Posted on 2013-06-13
8
Medium Priority
?
301 Views
Last Modified: 2013-06-13
I have these classes:

interface RecursiveMap<T> extends Map<T,RecursiveMap<T>>
class RecursiveTreeMap<T> extends TreeMap<T,RecursiveMap<T>> implements RecursiveMap<T>

I'd like to have this:

interface RecursiveMap<T> extends Map<T,RecursiveMap<T>>
class RecursiveTreeMap<T> extends TreeMap<T,RecursiveTreeMap<T>> implements RecursiveMap<T>

But this violates the generics on java.util.Map. The idea is that I want a workable interface for the RecursiveMaps, but I want to restrict any given implementation of RecursiveMap to only contain itself.

Is there a way to achieve this?
0
Comment
Question by:modsiw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 2000 total points
ID: 39244983
This looks a bit clumsy, but it seems to work:

import java.util.Map;
import java.util.TreeMap;

interface RecursiveMap<T, U extends RecursiveMap<T, U>> extends Map<T, U>
{
	
}

class RecursiveTreeMap<T>
   extends TreeMap<T, RecursiveTreeMap<T>>
   implements RecursiveMap<T, RecursiveTreeMap<T>>
{
	
}

Open in new window

0
 
LVL 3

Author Comment

by:modsiw
ID: 39245020
@CPColin

With you example, is this the best way to declare pointers:

RecursiveMap<String,?> map
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39245059
probably you need this:

interface RecursiveMap<K, V> extends Map<K, RecursiveMap<K, V>> {
}

class RecursiveTreeMap<K, V> implements RecursiveMap<K, RecursiveTreeMap<K, V>> {
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:modsiw
ID: 39245097
@Valeri

RecursiveTreeMap needs to extend TreeMap. Using your interface and following definition for RecursiveTreeMap leads to the same problem as in the original post.

class RecursiveTreeMap<K,V> extends TreeMap<K, RecursiveTreeMap<K,V>> implements RecursiveMap<K, RecursiveTreeMap<K,V>>

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 39245159
I guess my example makes it pretty tough to use the interface, doesn't it. Using RecursiveTreeMap as the variable type works fine, but that's not the point of an interface.

This might work, as long as RecursiveTreeMap doesn't really need to know it has another RecursiveTreeMap inside of it:

import java.util.Map;
import java.util.TreeMap;

interface RecursiveMap<T> extends Map<T, RecursiveMap<T>>
{
   
}

class RecursiveTreeMap<T>
   extends TreeMap<T, RecursiveMap<T>>
   implements RecursiveMap<T>
{
   public static void main(String[] args)
   {
      RecursiveMap<String> map = new RecursiveTreeMap<String>();
   }
}

Open in new window

0
 
LVL 16

Expert Comment

by:Valeri
ID: 39245176
if you want the functionality of TreeMap, then you don't need additional interface at all.
Just declare the Map you want in this way:

class RecursiveTreeMap<K, V> extends TreeMap<K, RecursiveTreeMap<K, V>> {
}
0
 
LVL 3

Author Comment

by:modsiw
ID: 39245224
@CPColin
Both of your solutions work with the caveat that even with best case generic declarations, there is no way to enforce what type of RecursiveMap can be inserted. Of course, in java, you can always do this by assigning a non-generic alias, so maybe it's not that big of a deal.

Unless a better solution comes along, I prefer your first suggestion and will use it with concrete pointers in most places and interace pointers for api/utility type stuff.


@Valeri
RecursiveTreeMap is one of many. Some of it's peers are RecursiveHashMap, RecursiveLinkedHashMap, RecursiveConcurrentHashMap, as well as, various proxys/mocks for testing.

Methods need to be able to operate on any of these without being concerned with which implementation it is dealing with.

Also, your last suggestion can be reduced to a single generic parmeter:
class RecrusiveTreeMap<T> extends TreeMap<T,RecrusiveTreeMap<T>>{}

Open in new window

0
 
LVL 16

Expert Comment

by:Valeri
ID: 39246312
does it work?! what kind of Map is this?!
you always will get RecursiveTreeMap<String> as a value, but how you will get the real values passed?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question