Solved

Recursive Generics

Posted on 2013-06-13
8
290 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 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 85
maven module vs maven project 3 73
Is Applet the way to go for my drag and drop system? 8 35
junit example issue 2 27
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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