Solved

Recursive Generics

Posted on 2013-06-13
8
288 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
sql import cannot be resolved jsp 3 47
Java Restore security prompts not working 10 56
Is there a simpler dropbox system? 10 34
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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