Solved

Recursive Generics

Posted on 2013-06-13
8
274 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
Comment Utility
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
Comment Utility
@CPColin

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

RecursiveMap<String,?> map
0
 
LVL 16

Expert Comment

by:Valeri
Comment Utility
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
 
LVL 3

Author Comment

by:modsiw
Comment Utility
@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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:CPColin
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now