Results 1 to 11 of 11
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default Immutable Lines test

    So I have this method:

    Java Code:
    @Override
    	public final List<Integer> lines(final String word) {
    		
    		if(this.getMap().containsKey(word)){
    			List <Integer> lines = this.getMap().get(word);
    			return lines;
    		}
    		else{
    			//return new LinkedList<Integer>();
    			return emptyList;
    		}
    		
    	}
    And I have this test:

    Java Code:
    @Test
    	public void testLinesImmutable() throws Exception {
    		try {
    			index.lines("xyz").add(3);
    			fail("lines must return an immutable list");
    		} catch (Exception e) { }
    		index.add("xyz", 5);
    		index.add("xyz", 3);
    		try {
    			index.lines("xyz").add(3);
    			fail("lines must return an immutable list");
    		} catch (Exception e) { }
    	}
    I thought the lines() would become immutable by adding "final" to it but it still does not pass, can anyone help me on why this test would not pass with my method.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,838
    Rep Power
    19

    Default

    That simply says that a child class cannot override the lines method.
    You want to return an unmodifiable copy of the List...see Collections utility class.

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok so I believe I need to use:

    unmodifiableList(List<? extends T> list)
    Returns an unmodifiable view of the specified list.

    Can anyone help me apply this method to my, I tried to add "unmodifiableList" in my method: public List <Integer> lines(final String word) so it was:

    public unmodifiableList List <Integer> lines(final String word)

    but it threw back a run time error. Can anyone guide me on how to format my methods to accept a unmodifiableList on my method. Do I have to completely re-write my method.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,838
    Rep Power
    19

    Default

    It's a method.
    You call it.
    You'll need to pass in the List you are currently returning (before returning it), and return the result.

    You don't need to change your method signature in any way since you will still be returning a List.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The collection framework uses the so called "decorator" pattern to provide unmodifiable collections. The details are covered in the Wrapper Implementations page of Oracle's Tutorial.

  6. #6
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok so I tried to apply this:

    public static <T> List<Integer> unmodifiableList(List<? extends T> emptyList) {
    List <Integer> lines = (List<Integer>) emptyList;
    return lines;
    }

    but the test still does not pass. Am I closer to solving it?

  7. #7
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    I have also tried this:

    public static <T> List<Integer> unmodifiableList(List<? extends T> emptyList) {
    List immutableMyList = Collections.unmodifiableList(emptyList);
    return immutableMyList;
    }

    but still does not pass, can anyone guide me through what I am missing.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You don't need a whole method just a simple assignment. Try

    Java Code:
        // This is just an example
        // list can come from anywhere: a method argument, from your map etc
    List<Integer> list = new ArrayList<Integer>();
    
        // The copy is unmodifiable
    List<Integer> copy = Collections.unmodifiableList(list);

  9. #9
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok so after reading above I tried this:

    @Override
    public List <Integer> lines(final String word) {

    if(this.getMap().containsKey(word)){
    List <Integer> lines = this.getMap().get(word);
    return lines;
    }
    else{
    return copy;
    }

    }

    List<Integer> copy = Collections.unmodifiableList(emptyList);

    But my test still fails, am I closer?

  10. #10
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok so after reading above I tried this:

    private final List <Integer> emptyList = new LinkedList<Integer>();

    @Override
    public List <Integer> lines(final String word) {

    if(this.getMap().containsKey(word)){
    List <Integer> lines = this.getMap().get(word);
    return lines;
    }
    else{
    return copy;
    }

    }

    List<Integer> copy = Collections.unmodifiableList(emptyList);

    But my test still fails, am I closer?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    @Override
        /**
         * Returns an unmodifiable list of Integers associated with a given word.  The 
         * returned list may be empty but won't be null.
         */
    public final List<Integer> lines(final String word) {
    		
        if(this.getMap().containsKey(word)){
            List <Integer> lines = this.getMap().get(word);
            return Collections.unmodifiableList(list);
        }
        else{
            // similar:
            // create an empty list
            // and return an unmodifiable copy of it
        }
    }

Similar Threads

  1. String Immutable
    By jomypgeorge in forum New To Java
    Replies: 18
    Last Post: 12-17-2010, 11:54 AM
  2. Strings and Immutable
    By al_Marshy_1981 in forum New To Java
    Replies: 19
    Last Post: 06-18-2010, 07:22 AM
  3. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 12:58 PM
  4. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •