6 Replies - 457 Views - Last Post: 15 December 2011 - 04:30 PM Rate Topic: -----

#1 Beweren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 11

How to check if a method is invoked

Posted 15 December 2011 - 12:32 PM

Hi everybody :)

I ran into a problem I just couldn't figure out, so I was confident you hardcore's in here could help me out.
I am writing a generic subclass to the AbstractCollection class that is located in the java.util library. To do this, I need to override two methods - iterator() that will return an iterator<E> (E is the element of random type) - an the method size() - which is not the problem.

the method "public Iterator<E> iterator()" is the problem. I made it like this:

public Iterator<E> iterator() {
		i =  (Iterator<E>) map.keySet().iterator();
		return i;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: How to check if a method is invoked

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,522
  • Joined: 05-May 05

Re: How to check if a method is invoked

Posted 15 December 2011 - 12:55 PM

First, a map ADT should extend AbstractMap<K,V>. Second, you need to post your entire implementation. Your code leaves much to be desired. It appears that your wrapping a map in this subclass, which is not sensible.
Was This Post Helpful? 0
  • +
  • -

#3 Beweren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 11

Re: How to check if a method is invoked

Posted 15 December 2011 - 02:10 PM

I dont really know why this was posted before I fisnished the article. it must be an error - heres the entire post : http://www.dreaminco...hod-is-invoked/ :

"
The map is used to load in a Hashmap.

However - the method above is illegal because the returned iterator has a method called remove(), which needs to throw an UnsupportedOperationException. I just dont know how to check if the i.remove() method is called to make it throw the exception and get passed the compiler error

I tried making a try-catch relay that caught the exception - but it does not seem to have an effect, so I left it out.
"

the map is a field variable which is filled in by the overridden add method I have in my class.. like this:

private map<String, E> map;

// other methods and field variables applied here...

public boolean add(E e) // this method returns TRUE if the element is added without error!
	{
		boolean elementIsPut = false;
		elements.add(e);
		keys.add(""+keys.size()+1);
		map = new HashMap<String, E>();
		
		int i = 0;
		while(elements.size() > 0)
		{
			try{
			map.put(keys.get(i), elements.get(i));
			}catch(Exception ex){
				System.out.println("Object could not be saved to hashmap!");
				elementIsPut = false;
			}
			elementIsPut = true;
		}
		
		return elementIsPut;
	}



I hope it makes more sence.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,522
  • Joined: 05-May 05

Re: How to check if a method is invoked

Posted 15 December 2011 - 02:34 PM

I don't understand your problem. Here is basically what you have, and it compiles just fine. All Map implementation iterators throw UnsupportedOperationException. On another note, why would you wrap a map in a Collection? What's the high-level objective of your class?
Was This Post Helpful? 0
  • +
  • -

#5 Beweren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-September 11

Re: How to check if a method is invoked

Posted 15 December 2011 - 03:20 PM

This is exactly right. Truthfully - it's an assignment I've been given. The purpose of this subclass is the fact that a collection like [34,12,3,12,12] could be represented like this : [1 of 3; 3 of 12; 1 of 34].

The Map should do the job here. Wierd enough I have to make the i.remove() throw an exception. I later dicovered that this method throws this exception anyways. This suggest that it is an old book I'm studying from.

Even though It made me curious to find out how to check if a method is invoked. This could be useful in many situations and it is also quite interesting. How can I check if a method(I did not design myself) is called and use this in an if-sentence? this is in fact my question.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to check if a method is invoked

Posted 15 December 2011 - 03:41 PM

You should make a Map<E, Integer>

When you want to insert test if there is a <E> in the Map
if there is not add <E, 1>
if there is one fetch the entry, add 1 to the Integer and update it

*Edited: <E,1> not <1, E>

This post has been edited by pbl: 15 December 2011 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,522
  • Joined: 05-May 05

Re: How to check if a method is invoked

Posted 15 December 2011 - 04:30 PM

Quote

Even though It made me curious to find out how to check if a method is invoked. This could be useful in many situations and it is also quite interesting. How can I check if a method(I did not design myself) is called and use this in an if-sentence? this is in fact my question.


What method are you talking about? What is the name of the method? If your asking about an API method, you need to look at the source code, but I doubt that's necessary for your assignment.

I feel like your confusing yourself.

i =  (Iterator<E>) map.keySet().iterator();


keySet().iterator() returns some implementation of the Iterator interface. Iterator.remove() won't throw an UnsupportedOperationException, even though it declares it. This particular implementation supports removing elements but some don't.

Given that you've got a collection and not a map, you should return an iterator over the values, not the keys.

public Iterator<E> iterator() {
        return elements.iterator();
}



Quote

Wierd enough I have to make the i.remove() throw an exception.


If it's a requirement that your iterator doesn't support removing elements, you need to create an Adapter.

public Iterator<E> iterator() {
    return new NonRemovableIterator<E>();
}

private class NonRemovableIterator<E> implements Iterator<E> {

    private Iterator<E> itr = (Iterator<E>) elements.iterator();
    
    @Override
    public boolean hasNext() {
        return itr.hasNext();
    }

    @Override
    public E next() {
        return itr.next();
    }

    @Override
    public void remove() {
        //I don't want to support removing elements
        throw new UnsupportedOperationException();
    }
    
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1