Results 1 to 11 of 11
Thread: Immutable Lines test
- 03-28-2011, 04:39 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Immutable Lines test
So I have this method:
And I have this test: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; } }
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.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) { } }
- 03-28-2011, 04:51 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-28-2011, 06:26 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
- 03-29-2011, 08:24 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-29-2011, 09:00 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-29-2011, 06:54 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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?
- 03-29-2011, 07:22 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
- 03-29-2011, 08:32 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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);
- 03-29-2011, 09:50 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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?
- 03-29-2011, 09:52 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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?
- 03-30-2011, 12:48 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
String Immutable
By jomypgeorge in forum New To JavaReplies: 18Last Post: 12-17-2010, 11:54 AM -
Strings and Immutable
By al_Marshy_1981 in forum New To JavaReplies: 19Last Post: 06-18-2010, 07:22 AM -
What is Immutable in String
By elektronika in forum New To JavaReplies: 4Last Post: 12-10-2009, 12:58 PM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 PM -
Strings are immutable yet they can be changed ?
By anjanesh in forum New To JavaReplies: 4Last Post: 05-19-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks