Solved

Recursive Generics

Posted on 2013-06-13
8
284 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 52
web application structure 18 77
check java version using powershell 13 91
What's wrong with this code? 4 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
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.

770 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